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");
}
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?
Have you tried to compile after you added usingnamespace std;? And can you please repost the program using [ code ] [ /code ] tags so we can see the indentation?
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.
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'
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.
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.