|
|
#1 |
|
question about CPP programing
hi, i tried to compile this program and the compiler gave me this massage: "invalid use of 'class circle1'.
im quiet new at progrming and i have no idea what i did wrong. any help will be appricieted... this is my code: main: Code:
#include <iostream>
#include <fstream>
#include "circle1.h"
#include "tri.h"
using namespace std;
int main()
{
int x,y;
circle1 r;
cout<<"enter numbers";
cin>>x>>y;
r.circle1(x);
return 0;
}
Code:
#ifndef CIRCLE1_H_INCLUDED
#define CIRCLE1_H_INCLUDED
class circle1
{
private:int r;
public:
void clch(int);
void clcs(int);
circle1(int);
circle1();
};
#endif // CIRCLE1_H_INCLUDED
Code:
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include "circle1"
using namespace std;
circle1::circle1(int x)
{
if(x<0)
cout<<"the number you enterd is negative!";
else
{
r=x;
void clch(int x);
void clcs(int x);
}
}
void circle1::clch(int x)
{
x=x*2*M_PI;
cout<<"this is the hikef of the first circle: "<<x<<"\n";
}
void circle1::clcs(int x)
{
x=2*M_PI*(pow(x,2));
cout<<"this is the sheth of the first circle: "<<x<<"\n";
}
Last edited by robbieduncan; Jan 4, 2013 at 06:34 AM. Reason: Please use the nifty code tags! |
|
|
|
0
|
|
|
#2 | |
|
Quote:
|
||
|
|
0
|
|
|
#3 |
|
ok; little update; fresh from Xcode
main.cpp Code:
#include <iostream>
#include <fstream>
#include "circle1.h"
//#include "tri.h"
using namespace std;
int main(int argc, const char * argv[])
{
double x, y;
circle1 *r = NULL; // declare a pointer to a new object
cout<<"enter numbers";
cin>>x>>y;
r = new circle1(x); // here you create dynamic an instance and indirect call you desired constructor
delete r; // cleanup
r = NULL; // to be safe ;-)
return 0;
}
Code:
#ifndef __cppmr__circle1__
#define __cppmr__circle1__
#include <iostream>
class circle1
{
private:
double r;
public:
void clch(double);
void clcs(double);
circle1(double);
circle1();
};
#endif /* defined(__cppmr__circle1__) */
Code:
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include "circle1.h"
using namespace std;
circle1::circle1(double x)
{
if (x < 0)
{
cout<<"the number you enterd is negative!";
}
else
{
r = x;
clch(x);
clcs(x);
}
}
void circle1::clch(double x)
{
double h = x * 2 * M_PI; // wouldn't hurt to declare a local var for the result
cout << "this is the hikef of the first circle: " << h << "\n";
}
void circle1::clcs(double x)
{
double s = 2*M_PI*(pow(x,2));
cout << "this is the sheth of the first circle: " << s << "\n";
}
result in console Code:
enter numbers2 3 this is the hikef of the first circle: 12.5664 this is the sheth of the first circle: 25.1327 Last edited by ChristianJapan; Jan 4, 2013 at 04:55 PM. |
|
|
|
0
|
|
|
#4 |
|
Code:
#include <iostream>
#include <fstream>
#include "circle1.h"
#include "tri.h"
using namespace std;
int main()
{
int x,y;
circle1 r; // default constructor called - do you want to call this constructor and not the other one?
cout<<"enter numbers";
cin>>x>>y;
r.circle1(x); // ERROR - calling the constructor again!
return 0;
}
The other is the following in the file, "circle1.cpp": Code:
#include "circle1" // ERROR - you need the extension... "circle1.h" Code:
void circle1::clcs(int x)
{
x=2*M_PI*(pow(x,2)); // ERROR - the function, pow(), takes a double and an int as arguments
cout<<"this is the sheth of the first circle: "<<x<<"\n";
}
|
|
|
|
0
|
|
|
#5 |
|
It's been few years since I have coded in C++, but I think I see the trouble....
You cannot invoke a constructor as a member on an existing instance. So this line: Code:
r.circle1(x); Here is the body of the main function, following your style: Code:
int main()
{
int x,y;
cout<<"enter numbers";
cin>>x>>y;
// make an instance of circle1 to test it
circle1 r(x);
return 0;
}
- instantiate an object only in the part of the code where you need it. You do not have to instantiate the circle1 before you obtain the input value - you can instantiate it afterwards, using the provided constructor. - use initialiser list in the constructor. That will make the constructor look like this: Code:
circle1::circle1(int x)
:
r ( x ) // this sets up the r member
{
if(x<0)
cout<<"the number you enterd is negative!";
else
{
void clch(int x);
void clcs(int x);
}
}
- do not use the approach with dynamically allocated memory advised in an above post. It is not warranted here. Dynamic memory allocation is needed if you intend to create an object in an function, and have it live outside of the function. This is not the case here. - The member r of class circle1 should probably be of a floating point type, not integer. - why do the member functions clch() and clcs() have an input parameter ? It seems they should work on the member r. If not, then why would you want the r member in first place ? PaulCC Last edited by balamw; Jan 7, 2013 at 10:58 AM. Reason: Please use code tags and trim quotes |
|
|
|
0
|
|
|
#6 | |
|
Quote:
|
||
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 06:30 AM.







I support the
Linear Mode
