Beginning Programmer did something wrong...

I am teaching myself c++ and haven't been able to get my program to run. Here's the project I am trying to program.

Design a class named Fan to represent a fan. The class contains:
- An int data field named speed that specifies the speed of the fan. A fan has three speeds indicated with a value 1, 2, or 3.
- A bool data field named on that specifies whether the fan is on.
- A double data field named radius that specifies the radius of the fan.
- A no-arg constructor that creates a default fan with speed 1, on false, and radius 10.
- The accessor and mutator functions for all the data fields.

Here is my code so far:

#include <iostream>
#include <string>

class Fan
{

private:
int speed;
bool on;
double radius;
string color;

public:
Fan()
{
speed = 1;
on = false;
radius = 5;
color = string("white");
}

int getSpeed()
{
return speed;
}

void setSpeed(int speed)
{
this->speed = speed;
}

bool isOn()
{
return on;
}

void setOn(bool trueOrFalse)
{
this->on = trueOrFalse;
}

double getRadius()
{
return radius;
}

void setRadius(double radius)
{
this->radius = radius;
}

string getColor()
{
return color;
}

void setColor(string color)
{
this->color = color;
}
};



int main()
{
cout << "First fan properties: " << endl;
Fan fan;
fan.setSpeed(3);
fan.setRadius(10);
fan.setOn(true);
fan.setColor("yellow");

cout << "Fan speed: " << fan.getSpeed() << endl;
cout << "Fan radius: " << fan.getRadius() << endl;
cout << "Fan on? " << fan.isOn() << endl;
cout << "Fan color: " << fan.getColor() << endl;

Fan fan1;
fan.setSpeed(2);
fan.setRadius(5);
fan.setOn(false);
fan.setColor("blue");

cout << "\nSecond fan properties: " << endl;
cout << "Fan speed: " << fan1.getSpeed() << endl;
cout << "Fan radius: " << fan1.getRadius() << endl;
cout << "Fan on? " << fan1.isOn() << endl;
cout << "Fan color: " << fan1.getColor() << endl;


return 0;
}
nothing that a simple "using namespace std" can't settle?
ref:
http://www.cplusplus.com/reference/
http://www.cplusplus.com/doc/tutorial/namespaces/
I tried the "using namespace std" and I still get an error everywhere it says "string" in the Fan class and where ever fan is capitalized in the Project class... Any ideas?
Error messages are meaningful. If you don't post them, it's harder to see what's going on.
jonsto wrote:
I tried the "using namespace std"


Where did you put it?
#include <iostream>
#include <string>

using namespace std;
errors are: identifier "string" is undefined
identifier "Fan" is undefined

at least that's what is says when I hold the cursor over the red lines in Visual Studio...
Have you tried to compile after you added using namespace std;? And can you please repost the program using [ code ] [ /code ] tags so we can see the indentation?
Please use code tags...

Error messages are meaningful. If you don't post them, it's harder to see what's going on.


Also I would declare "fan" and "fan1" right after you make The "Fan class"
ex.
1
2
//Fan class information goes here
} fan, fan1;


That doesn't do anything other than make them global (actually I'm not sure about that...), but that makes It easier to read (for me at least) : if only becuase it makes main() a little less cluttered.



Last edited on
1>------ Build started: Project: Project, Configuration: Debug Win32 ------
1> Project.cpp
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(13): error C2065: 'Fan' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(13): error C2146: syntax error : missing ';' before identifier 'fan'
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(13): error C2065: 'fan' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(14): error C2065: 'fan' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(14): error C2228: left of '.setSpeed' must have class/struct/union
1> type is ''unknown-type''
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(15): error C2065: 'fan' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(15): error C2228: left of '.setRadius' must have class/struct/union
1> type is ''unknown-type''
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(16): error C2065: 'fan' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(16): error C2228: left of '.setOn' must have class/struct/union
1> type is ''unknown-type''
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(17): error C2065: 'fan' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(17): error C2228: left of '.setColor' must have class/struct/union
1> type is ''unknown-type''
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(19): error C2065: 'fan' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(19): error C2228: left of '.getSpeed' must have class/struct/union
1> type is ''unknown-type''
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(20): error C2065: 'fan' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(20): error C2228: left of '.getRadius' must have class/struct/union
1> type is ''unknown-type''
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(21): error C2065: 'fan' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(21): error C2228: left of '.isOn' must have class/struct/union
1> type is ''unknown-type''
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(22): error C2065: 'fan' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(22): error C2228: left of '.getColor' must have class/struct/union
1> type is ''unknown-type''
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(24): error C2065: 'Fan' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(24): error C2146: syntax error : missing ';' before identifier 'fan1'
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(24): error C2065: 'fan1' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(25): error C2065: 'fan' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(25): error C2228: left of '.setSpeed' must have class/struct/union
1> type is ''unknown-type''
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(26): error C2065: 'fan' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(26): error C2228: left of '.setRadius' must have class/struct/union
1> type is ''unknown-type''
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(27): error C2065: 'fan' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(27): error C2228: left of '.setOn' must have class/struct/union
1> type is ''unknown-type''
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(28): error C2065: 'fan' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(28): error C2228: left of '.setColor' must have class/struct/union
1> type is ''unknown-type''
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(31): error C2065: 'fan1' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(31): error C2228: left of '.getSpeed' must have class/struct/union
1> type is ''unknown-type''
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(32): error C2065: 'fan1' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(32): error C2228: left of '.getRadius' must have class/struct/union
1> type is ''unknown-type''
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(33): error C2065: 'fan1' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(33): error C2228: left of '.isOn' must have class/struct/union
1> type is ''unknown-type''
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(34): error C2065: 'fan1' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(34): error C2228: left of '.getColor' must have class/struct/union
1> type is ''unknown-type''
1> Fan.cpp
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(8): error C2146: syntax error : missing ';' before identifier 'color'
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(49): error C2146: syntax error : missing ';' before identifier 'getColor'
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(49): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(50): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(52): warning C4183: 'getColor': missing return type; assumed to be a member function returning 'int'
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(54): error C2061: syntax error : identifier 'string'
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(16): error C2065: 'color' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(16): error C3861: 'string': identifier not found
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(51): error C2065: 'color' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(56): error C2039: 'color' : is not a member of 'Fan'
1> h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(2) : see declaration of 'Fan'
Your code in project.cpp has no knowledge of what a Fan class is.

I assume your class definition is in the file named fan.cpp

You must include that file in your project.cpp file, like this:

#include "fan.cpp"

Note that as a general rule, the header file is named *.h, so consider renaming fan.cpp to fan.h

There is more, but once you fix that, some of the errors will be fixed.

I'm guessing that you have not included <string> in your fan.cpp file either, so your fan class has no idea what a string is.

Here is an explanation of what you are doing:
http://www.learncpp.com/cpp-tutorial/19-header-files/

Last edited on
you declare a member variable for fan but dont use it. you use just plain fan. this code is your error

1
2
3
4
5
6

Fan fan1;
fan.setSpeed(2); // should be fan1
fan.setRadius(5);// also here
fan.setOn(false); // and here
fan.setColor("blue"); // and here 


fixed code

1
2
3
4
5
6
Fan fan1;
fan1.setSpeed(2);
fan1.setRadius(5);
fan1.setOn(false);
fan1.setColor("blue");
Last edited on
still getting errors...

1> Project.cpp
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(8): error C2146: syntax error : missing ';' before identifier 'color'
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(49): error C2146: syntax error : missing ';' before identifier 'getColor'
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(49): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(50): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(52): warning C4183: 'getColor': missing return type; assumed to be a member function returning 'int'
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(54): error C2061: syntax error : identifier 'string'
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(16): error C2065: 'color' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(16): error C3861: 'string': identifier not found
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(51): error C2065: 'color' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(56): error C2039: 'color' : is not a member of 'Fan'
1> h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(2) : see declaration of 'Fan'
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(18): error C2660: 'Fan::setColor' : function does not take 1 arguments
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\project.cpp(29): error C2660: 'Fan::setColor' : function does not take 1 arguments
1> Fan.cpp
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(8): error C2146: syntax error : missing ';' before identifier 'color'
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(49): error C2146: syntax error : missing ';' before identifier 'getColor'
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(49): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(50): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(52): warning C4183: 'getColor': missing return type; assumed to be a member function returning 'int'
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(54): error C2061: syntax error : identifier 'string'
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(16): error C2065: 'color' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(16): error C3861: 'string': identifier not found
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(51): error C2065: 'color' : undeclared identifier
1>h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(56): error C2039: 'color' : is not a member of 'Fan'
1> h:\jstone\cs215\programmingprojects\chapter9\project9.2\fan.cpp(2) : see declaration of 'Fan'
it should compile just fine if you make the suggested fixes. fix what i wrote plus add using namespace std; as mentioned earlier by someone else and it compiles fine.

below compiles fine for me with the added fixes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <string>
#include <limits>


using namespace std;

class Fan
{

private:
	int speed;
	bool on;
	double radius;
	string color;

public:
	Fan()
	{
		speed = 1;
		on = false;
		radius = 5;
		color = string("white");
	}

int getSpeed()
{
	return speed;
}

void setSpeed(int speed)
{
	this->speed = speed;
}

bool isOn()
{
	return on;
}

void setOn(bool trueOrFalse)
{
	this->on = trueOrFalse;
}

double getRadius()
{
	return radius;
}

void setRadius(double radius)
{
	this->radius = radius;
}

string getColor()
{
	return color;
}

void setColor(string color)
{
	this->color = color;
}

}; // class Fan



int main()
{
	cout << "First fan properties: " << endl;
	Fan fan;
	fan.setSpeed(3);
	fan.setRadius(10);
	fan.setOn(true);
	fan.setColor("yellow");

	cout << "Fan speed: " << fan.getSpeed() << endl;
	cout << "Fan radius: " << fan.getRadius() << endl;
	cout << "Fan on? " << fan.isOn() << endl;
	cout << "Fan color: " << fan.getColor() << endl;

	Fan fan1;
	fan1.setSpeed(2);
	fan1.setRadius(5);
	fan1.setOn(false);
	fan1.setColor("blue");

	cout << "\nSecond fan properties: " << endl;
	cout << "Fan speed: " << fan1.getSpeed() << endl;
	cout << "Fan radius: " << fan1.getRadius() << endl;
	cout << "Fan on? " << fan1.isOn() << endl;
	cout << "Fan color: " << fan1.getColor() << endl;

        cout << "Press ENTER to continue...";
	cin.ignore(numeric_limits<streamsize>::max(), '\n' );

 
return 0;
}
Last edited on
I got it working. Thank you everyone that helped me out. I had all the #include's on the project.cpp page rather than the Fan.cpp page. As soon as I moved the #include's to the fan.cpp page and changed the Fan fan1 properties all to fan 1 it worked fine. Thanks again.
Topic archived. No new replies allowed.