Hey all,
I am just getting into writing code in C++ and have written code in JAVA and FORTRAN a while back - so I am a little rusty.
I am just trying to write a simple Class to get the feel for it all. Basically the class CRectangle has the input of width and length, the area() member returns the area. I have included a number of get functions too, just for practise.
When I run this without seperating the code into a header file, a function file (?) - where you define the construct and member functions - and a main program, the program works fine.
Now that I have seperated it, it is not working, with the errors:
In file included from CRectangle.cpp:2:
CRectangle.h:9: error: expected unqualified-id before ‘int’
CRectangle.h:9: error: expected ‘)’ before ‘int’
CRectangle.h:7: error: an anonymous struct cannot have function members
CRectangle.h:16: error: abstract declarator ‘<anonymous class>’ used as declaration
CRectangle.cpp:5: error: expected identifier before ‘(’ token
CRectangle.cpp:5: error: expected ‘;’ before ‘(’ token
CRectangle.cpp:5: error: expected unqualified-id before ‘int’
CRectangle.cpp:5: error: expected ‘)’ before ‘int’
CRectangle.cpp:10: error: explicit qualification in declaration of ‘int area()’
CRectangle.cpp: In function ‘int area()’:
CRectangle.cpp:11: error: ‘length’ was not declared in this scope
CRectangle.cpp:11: error: ‘width’ was not declared in this scope
CRectangle.cpp: At global scope:
CRectangle.cpp:14: error: explicit qualification in declaration of ‘int getlength()’
CRectangle.cpp: In function ‘int getlength()’:
CRectangle.cpp:15: error: ‘length’ was not declared in this scope
CRectangle.cpp: At global scope:
CRectangle.cpp:18: error: explicit qualification in declaration of ‘int getwidth()’
CRectangle.cpp: In function ‘int getwidth()’:
CRectangle.cpp:19: error: ‘width’ was not declared in this scope
In file included from mainrecttest.cpp:2:
CRectangle.h:9: error: expected unqualified-id before ‘int’
CRectangle.h:9: error: expected ‘)’ before ‘int’
CRectangle.h:7: error: an anonymous struct cannot have function members
CRectangle.h:16: error: abstract declarator ‘<anonymous class>’ used as declaration
mainrecttest.cpp: In function ‘int main()’:
mainrecttest.cpp:34: error: ‘rect’ was not declared in this scope
I know there is a simple mistake somewhere in the syntax, but for the life of me, I cannot find it. Please help!
I am not sure if I attached the source code correctly, I have attempted to do so twice. So here are the text versions just in case
Header file CRectangle.h
The function file for the class, CRectangle.cppCode:#ifndef CRectangle #define CRectangle #include <iostream> using namespace std; class CRectangle { public: CRectangle(int, int); int area(); int getlength(); int getwidth(); private: int length, width; }; #endif
and the main programCode:#include <iostream> #include "CRectangle.h" using namespace std CRectangle::CRectangle(int a,int b){ length = a; width = b; } int CRectangle::area(){ return(length*width); } /*get the goods*/ int CRectangle::getlength(){ return (length); } int CRectangle::getwidth(){ return (width); }
Code:#include <iostream> #include "CRectangle.h" using namespace std; int main () { CRectangle rect (3,4); cout<< "length: " << rect.getlength()<< endl; cout<< "width: " << rect.getwidth()<< endl; cout << "rect area: " << rect.area() << endl; return 0; }
Last edited by Orjan; 02-07-2010 at 08:11 AM. Reason: Please use code tags!
You really need to pay attention to your code...
1º CRectangle is defined first as a macro, so you can't use that name for a class. Change the macro name to CRECTANGLE_H or something else that don't conflict with other names.
2º CRectangle.cpp, line 3 "using namespace std", is missing a ';' in the end.
No, i mean the macro you defined:
That name is reserved for that macro, so you can't use the same name in a class. You need to change the class name or the macro name, so they will not conflict. I suggest you to change the macro name to CRECTANGLE_H.Code:#ifndef CRectangle #define CRectangle ...
Thank you!
You Sir, are a scholar and a gentleman!
All works well.
Can I just ask you if what I think a macro is, is correct?
A macro is the name/handle of the header file?
Macros are user defined symbols meant to be used by pre-processor before the real compilation stage, you can use them for a variety of reasons. Macros are defined as:
#define <identifier> <replacement expression>
The pre-processor will replace every macro symbol by the replacement expression, for example:
before the compilation, ERROR and OK will be replaced by 1 and 0 respectively.Code:#define OK 0 #define ERROR 1 ... some code return (OK); or maybe return (ERROR);
Macros also support conditional expressions, like:
This is used for conditional compilation, before the real compilation stage the pre-processor will define one of the symbols, based on the condition.Code:#ifndef SOME_SYMBOL # define SYMBOL #else # define OTHER_SYMBOL #endif
Now, in a header file, like in your case:
Prevents the file to be included twice, because the symbol CRECTANGLE_H will be defined, and then you include it again, it is already defined, so the code inside the macro condition will not be included again.#ifndef CRECTANGLE_H
#define CRECTANGLE_H
#endif
There are many other reasons for macro usage, i suggest you to read C/C++ pre-processor reference:
C/C++ Preprocessor Reference (C/C++)
Once again, outsid3r you have been a massive help; many thanks!
![]()
I'm glad to help
Now some tips:
You don't need to define #include <iostream> and using namespace std in your CRectangle.cpp and CRectangle.h files, because non of them uses functions related with iostream, so you are just polluting the space with names. Don't define them, specially in header files, because that's those that will be included in another files...
If you really need to define some library in an header file, do it like:
do not include the whole namespace because you're polluting the space with names that may conflict with others.Code:#ifndef CRECTANGLE_H #define CRECTANGLE_H #include <string> class CRectangle { public: std::string getName(); ... } #endif
Another thing is the return 0; in main() function, in C++ you don't need to do that because the compiler puts it implicitly.
Those tips help out a fair bit! It is obviously a good idea to start a good coding practise now.
Cheers outsid3r!
![]()
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks