I am attempting to create a rather rudimentary Class file and I can't figure out how to let it work. I want to be able to use the class file as a class in another, just as simple file.
include <iostream>
class Cat
{
public:
Cat (int initialAge);
~Cat();
int GetAge() const {return itsAge;}
void SetAge() (int age){itsAge=age}
void Meow() const {std::cout << "Meow.\n";}
private:
int itsAge;
};
My compile log is:
Compiler: Default compiler
Executing gcc.exe...
gcc.exe "C:\C++Work\Cat.h" -o "C:\C++Work\Cat.exe" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\C++Work\Cat.h:1:20: iostream: No such file or directory
C:\C++Work\Cat.h:2: error: syntax error before "Cat"
C:\C++Work\Cat.h:3: error: syntax error before '{' token
C:\C++Work\Cat.h: In function `GetAge':
C:\C++Work\Cat.h:7: error: syntax error before '{' token
Execution terminated
Does anyone know what I'm doing wrong?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-07-02
Apart from using C compilation where C++ compilation is required, your log also shows that you are compiling the header file directly. I am not sure how you achieved that trick, but normally only .c/.cpp files are compiled directly, header files are merely #include'd into the target compilation unit. Whatever you are doing there it is likley to be wrong! Post a new log perhaps.
You could not need to call your header's .hpp rather than .h, because it is the including file that selects the compiler not the header. What you have achieved is 'unusual', and as I said probably wrong.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-07-02
Sorry, for
"You could not need to call your header's... "
read
"You should not need to call your headers... "
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Misplaced const, i think. const int GetAge() or int const GetAge() should work better. (those two statements are wildly different though. Read up on "const-correctness" about that)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank you very much, hat should solve about half the problem, but why is it unable to call up iostream? None of my other basic programs have had this problem.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I've looked again and tried a few things, its only when I compile this into a .h file that there comes any problems. The original code worked fine when used in a .cpp program, but it screws up when turned into a .h, does anyone know the reason for this?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank you very much! That entirely solved the problem, I also was able to just change the file extension to .hpp and it worked as well, thanks a ton for the help all.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am using Dev C++ 4.9.9.2
I am attempting to create a rather rudimentary Class file and I can't figure out how to let it work. I want to be able to use the class file as a class in another, just as simple file.
include <iostream>
class Cat
{
public:
Cat (int initialAge);
~Cat();
int GetAge() const {return itsAge;}
void SetAge() (int age){itsAge=age}
void Meow() const {std::cout << "Meow.\n";}
private:
int itsAge;
};
My compile log is:
Compiler: Default compiler
Executing gcc.exe...
gcc.exe "C:\C++Work\Cat.h" -o "C:\C++Work\Cat.exe" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\C++Work\Cat.h:1:20: iostream: No such file or directory
C:\C++Work\Cat.h:2: error: syntax error before "Cat"
C:\C++Work\Cat.h:3: error: syntax error before '{' token
C:\C++Work\Cat.h: In function `GetAge':
C:\C++Work\Cat.h:7: error: syntax error before '{' token
Execution terminated
Does anyone know what I'm doing wrong?
Apart from using C compilation where C++ compilation is required, your log also shows that you are compiling the header file directly. I am not sure how you achieved that trick, but normally only .c/.cpp files are compiled directly, header files are merely #include'd into the target compilation unit. Whatever you are doing there it is likley to be wrong! Post a new log perhaps.
You could not need to call your header's .hpp rather than .h, because it is the including file that selects the compiler not the header. What you have achieved is 'unusual', and as I said probably wrong.
Clifford
Sorry, for
"You could not need to call your header's... "
read
"You should not need to call your headers... "
Clifford
Misplaced const, i think. const int GetAge() or int const GetAge() should work better. (those two statements are wildly different though. Read up on "const-correctness" about that)
> Misplaced const, i think.
Not really: "int GetAge() const" and "const int GetAge()"
have different meaning.
The first method promises not to change the object and returns an int;
the second returns a const int and, in principle, could change the object.
cp
Thank you very much, hat should solve about half the problem, but why is it unable to call up iostream? None of my other basic programs have had this problem.
I've looked again and tried a few things, its only when I compile this into a .h file that there comes any problems. The original code worked fine when used in a .cpp program, but it screws up when turned into a .h, does anyone know the reason for this?
If you're making a .h it has to have two lines before the code:
ifndef __
define __
Fill in the blanks with the name of your header file (for example, "#ifndef CAT_H")Then it has to have this at the end:
endif
Thank you very much! That entirely solved the problem, I also was able to just change the file extension to .hpp and it worked as well, thanks a ton for the help all.
You won't read this because your problem is solved and you no longer need us, but the reason behind that header stuff i just said is:
ifndef CAT_H //If the header CAT_H is not defined
define CAT_H //Define it. The way this is set up, you can include it twice with no worries.
//Header code
endif //End the definition of the header file.
Fyi.