One exception... templates. Although I call those hpp's.Firstly, do not include .cpp files. Remember that as a rule.
One exception... templates. Although I call those hpp's.Firstly, do not include .cpp files. Remember that as a rule.
I am sure that you recall the trick of placing the implementation of the templates into a .cpp file, and then including that in the .h file.Originally Posted by Elysia
Of course, if you were feeling silly enough, you could use ".h" for source file extensions and ".cpp" for header file extensions.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I find it useful to include a separate hpp for template source. IMO, putting it all in the header makes the header ugly and cumbersome and breaks the paradigm that all your other non- template classes follow.
But I digress.....
thanks for all the replies.
if I'm not wrong, I read that i have to use the using statement for I will be using it in loading the file .h.Also, you need some header guards in your .h file and get that using statement out of the header; you should never put a using statement in a header file.
the one in blue uses the class string ( I'm not sure if the string is class) if I will not use the using std::string it will cause a problem on loading the .h from the main file.Code:#include <string> using std::string; class Name { public: Name( string ); void setName( string ); string getName(); void displayName(); private: string name; };
thanks, next time I post a program you will never see void there in the parameter ^_^You don't need void in the parameter list in C++. It's unnecessary - it just takes space with no effect!
thanks for the advice, but I prefer to know how linking works. I kept researching about that but until now I can't find any resource on how to link those two .cpp and use the .h file ro your main program.If this is too difficult for you, use an IDE such as Code::Blocks or Visual Studio. It will do this for you automatically.
You don't need to have a using declaration for using std::string. You can also explicitly qualify it.
Therefore, replace all occurences of 'string' in the class definition (not only the blue one, but also those in the member functions) with 'std::string'.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
See this for more details: Chapter 59. Don't write namespace usings in a header file or before an #include
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
wow, I never knew that, but I just copied the program from the book I read. I will not tell the name of the book to protect their integrity. But how come? they should know that. Anyway, I removed the #include <string> and using namespace.
Code:#ifndef NAME_H #define NAME_H //#include <string> //using std::string; class Name { public: Name(); Name( string ); void setName( string ); string getName(); void displayName(); private: string name; }; #endif
Yes, they should know that. A lot of authors should also know things such as int main() instead of void main(), but I guess these days almost anyone can publish a book full of crap.
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
No, no, now your header has include order dependencies. It requires that someone includes <string> and imports string into the global namespace before it can be included without errors. That's just as bad, if not worse, as having using statements or declarations. The correct form is
The advice about parameter names is good, too.Code:#ifndef NAME_H #define NAME_H #include <string> class Name { public: Name(); Name( std::string ); void setName( std::string ); std::string getName(); void displayName(); private: std::string name; }; #endif
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
I was able to fix the problem. My mistake before was I didn't build a project, instead, I coded all of them in separate files one-by-one but not as a whole project.
Having problem with this at the bottom:
Not grouped as one project
Name.h
Name.cpp
People.pp
When I consolidated all of the files in one project the program compiled successfully.
Grouped in one project
+Name.h
+Name.cpp
+People.cpp
Somehow, I'm happy to fix it but I didn't get the idea on how to link those two manually. So with the compiler I have I don't have any option to link those two files that I wanted to link manually right? even with visual c++?
If you were typing commands, you would just type "gcc -o Name.exe Name.cpp People.cpp" (giving all the files that need to be compiled on one line). Same for Visual Studio's cl, except I forget what the equivalent of -o is.