| |

VerySource

 Forgot password?
 Register
Search
View: 1216|Reply: 8

Please help me look at the code C ++

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-5 14:30:01
| Show all posts |Read mode
#include <iostream.h>


const float PI = 3.14f;



void main ()
{
class Cricle; // I think something went wrong here
Cricle x;
int r;
cin >> r;
cout << "S's Price is" << x.S_Price (r) << endl;
cout << "C's Price is" << x.C_Price (r) << endl;
}

class Cricle
{
private:
int raidom;
public:
float Get_C (int raidom)
{
return (raidom * PI * 2);
}

float Get_S (int raidom)
{
return (PI * raidom * raidom);
}

float S_Price (int raidom)
{
float Result;
Result = Get_S (raidom) -Get_S (raidom-3);
return (20 * Result);
}

float C_Price (int raidom);
};

float Cricle :: C_Price (int raidom)
{
float Result1;
Result1 = Get_C (raidom);
return (35 * Result1);
}

Always give an error when compiling
D:\Program Files\Microsoft Visual Studio\MyProjects\Cricle\Cricle.cpp (15): error C2228: left of '.C_Price' must have class / struct / union type

D:\Program Files\Microsoft Visual Studio\MyProjects\Cricle\Cricle.cpp (14): error C2228: left of '.S_Price' must have class / struct / union type

D:\Program Files\Microsoft Visual Studio\MyProjects\Cricle\Cricle.cpp (11): error C2065: 'x': undeclared

Please help me see. The class must be written below main ()
Reply

Use magic Report

0

Threads

14

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-5-20 11:15:01
| Show all posts
#include <iostream.h>


const float PI = 3.14f;

class Cricle
{
private:
int raidom;
public:
float Get_C (int raidom)
{
return (raidom * PI * 2);
}

float Get_S (int raidom)
{
return (PI * raidom * raidom);
}

float S_Price (int raidom)
{
float Result;
Result = Get_S (raidom) -Get_S (raidom-3);
return (20 * Result);
}

float C_Price (int raidom);
};


void main ()
{
Cricle x;
int r;
cin >> r;
cout << "S's Price is" << x.S_Price (r) << endl;
cout << "C's Price is" << x.C_Price (r) << endl;
}



float Cricle :: C_Price (int raidom)
{
float Result1;
Result1 = Get_C (raidom);
return (35 * Result1);
}
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-5-21 07:30:01
| Show all posts
Big Brother

The class must be written under main ()
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-5-21 13:15:02
| Show all posts
why
Reply

Use magic Report

0

Threads

36

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-5-21 22:45:02
| Show all posts
The main function main should be placed under the class, otherwise it will not recognize the class or variable or function defined under it
Reply

Use magic Report

1

Threads

39

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 Korea, Republic of

Post time: 2020-5-22 16:00:02
| Show all posts
There is no necessary connection with the main function.
Mainly c ++ is a language that needs to be declared before use. The class is used in main, but there is no declaration before, which will make the compiler
Puzzled.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-5-22 22:00:02
| Show all posts
When the compiler compiles Circle x, it must know how much memory overhead x is, so if there is no class structure declaration, including the definition of member variables, it cannot be compiled, if you must put the declaration behind, only use pointers.
Circle * x = (Circle *) operator new (4); // This is no problem, because the pointer is always four bytes
But even so, when you use X, you still need to declare the prototype of the class.If you write it all to the back, it is like you define a global variable and use it without adding a declaration in front.
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-5-31 01:00:01
| Show all posts
#include <iostream.h>


const float PI = 3.14f;
class Cricle
{
private:
int raidom;

public:
float Get_C (int raidom)
{
return (raidom * PI * 2);
}
The
float Get_S (int raidom)
{
return (PI * raidom * raidom);
}
The
float S_Price (int raidom)
{
float Result;
Result = Get_S (raidom) -Get_S (raidom-3);
return (20 * Result);
}
float C_Price (int raidom);
};

float Cricle :: C_Price (int raidom)
{
float Result1;
Result1 = Get_C (raidom);
return (35 * Result1);
}
void main ()
{
// class Cricle; // I think something went wrong here
Cricle x;
int r;
cout << "please input r:" << endl;
cin >> r;

cout << "S's Price is" << x.S_Price (r) << endl;
cout << "C's Price is" << x.C_Price (r) << endl;
}
Reply

Use magic Report

0

Threads

1

Posts

0.00

Credits

Newbie

Rank: 1

Credits
0.00

 China

Post time: 2020-5-31 07:46:06
| Show all posts
kan manual
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list