Jump to content
  • Advertisement
Sign in to follow this  
ceribik

C++: Compiler has gone mad

This topic is 4094 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alright, I'm having a little trouble when i start including a header file (with a class definition) in *any* other files (such as a implementation file). This is the first time it has happened and I am totally clueless to why. Here is the source as well as the errors:
// Employee.h

#ifndef EMPLOYEE_H
#define EMPLOYEE_H

#include <string>

class Employee
{
public:
	Employee(string firstName, string lastName, float salary);

protected:
	string mFirstName;
	string mLastName;
	float mSalary;
};

#endif // EMPLOYEE_H

// Employee.cpp

#include "stdafx.h"
#include "Employee.h"

// removed extra stuff as it is not causing the problem...


Quote:
Error 1 error C2146: syntax error : missing ')' before identifier 'firstName' 11 Error 2 error C2146: syntax error : missing ';' before identifier 'firstName' 11 Error 3 error C2460: 'Employee::string' : uses 'Employee', which is being defined 11 Error 4 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 11 Error 5 error C2146: syntax error : missing ';' before identifier 'lastName' 11 Error 6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 11 Error 7 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 11 Error 8 error C2062: type 'float' unexpected 11 Error 9 error C2327: 'Employee::string' : is not a type name, static, or enumerator 14 Error 10 error C2146: syntax error : missing ';' before identifier 'mFirstName' 14 Error 11 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 14 Error 12 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 14 Error 13 error C2327: 'Employee::string' : is not a type name, static, or enumerator 15 Error 14 error C2146: syntax error : missing ';' before identifier 'mLastName' 15 Error 15 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 15 Error 16 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 15
When i removed the #include "Employee.h" from the cpp file, i get no errors. Also, all the errors specified above are in the header file. Any help would be appreciated. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Oh my. I have been literally trying to figure out what was wrong for the last 5 hours and I rewrote it twice. Thank you so much :)

Share this post


Link to post
Share on other sites
To extend. You could either mangle your namespace with...

using namespace std;

After including <string>, allowing you to use the type string as you have it, or the more preferred option of using...

std::string firstname;


[edit]
boo.. I'm too tired and slow.

Share this post


Link to post
Share on other sites
Quote:
Original post by instinKt
To extend. You could either mangle your namespace with...

using namespace std;



Using directives are generally bad practice in header files, because now any file that includes that header will be using that namespace. This can lead to some hard to track down name collisions if you're not careful.

Share this post


Link to post
Share on other sites
Quote:
Original post by ceribik
Oh my. I have been literally trying to figure out what was wrong for the last 5 hours and I rewrote it twice. Thank you so much :)


Yeah get used to it since the C++ compiler will spit out over a dozen useless error message for something as simple as leaving out a header file!
You'd think the error messages would at point you in the right direction for such a simple mistake?

Share this post


Link to post
Share on other sites
Once you learn how headers really work, the errors make as much sense as they can. A #include statement effectively replaces its self with the contents of the file you are including. So when the compiler finishes doing the pre-processor step (swapping #includes for the included files content and resolving #defines etc.) it just sees a big "file" with all the headers inlined. When you don’t include a file you need to, some of the definitions that are referenced in the original c/cpp file can’t be found in the pre-processed "file". This causes the compiler throws the errors that it does because they make perfect sense. (Even the errors that are caused by a missing semi-colon in a header file, which, appear at first, to make no sense at all)

Share this post


Link to post
Share on other sites
Quote:
Original post by Driv3MeFar
Using directives are generally bad practice in header files, because now any file that includes that header will be using that namespace. This can lead to some hard to track down name collisions if you're not careful.


That's true, but in case anyway wants to know, you can also just use a specific part of a namespace, by having: using std::cout, for example.

Quote:
Original post by daviangel
You'd think the error messages would at point you in the right direction for such a simple mistake?


Yeah, i think they should fix that, but they probably think that if your a programmer then you should be able to realize the mistake. When i saw all the errors, i was like what the hell did i do wrong? My code looked perfectly fine to me.

Quote:
Original post by agh-ha-ha
Once you learn how headers really work, the errors make as much sense as they can. A #include statement effectively replaces its self with the contents of the file you are including. So when the compiler finishes doing the pre-processor step (swapping #includes for the included files content and resolving #defines etc.) it just sees a big "file" with all the headers inlined. When you don’t include a file you need to, some of the definitions that are referenced in the original c/cpp file can’t be found in the pre-processed "file". This causes the compiler throws the errors that it does because they make perfect sense. (Even the errors that are caused by a missing semi-colon in a header file, which, appear at first, to make no sense at all)


Well, I already sort of know the way they work (pretty much similar to the way you just described), but the problem was that errors didn't really relate to a missing include. They mainly just say "missing )", "missing ;", etc.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!