CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2011
    Posts
    4

    Unhappy Help, getting really strange errors!

    Hello, I'm taking a C++ class, and I keep running into a strange occurrence. I'm writing a simulation program with people sending emails to one another and one of the requirements is we use abstract base classes when dealing with email forwarding strategies. The two ABC's in my project are EvaluateMessage and EvaluateRecipient. The Person class holds a pointer to each and when a Person is created a child class is chosen (based on an input file) to instantiate the pointers to. EvaluateMessage compiles and works just fine, however EvaluateRecipient is giving me problems and I don't know why. EvaluateRecipient works in other classes without a problem and the code all compiles and the input is all read fine, but when I include it in Person.h its gives me an error list this long in Visual Studio:

    (The actual error list is much much longer but it's repetitive). I'm using guards in my header files, but reading the errors it seems that by add EvaluateRecipient to Person, I'm stripping all references of Person out of the project somehow. I don't know what's going on.

    Errors:

    Code:
    1>evaluaterecipient.h(18): error C2065: 'Person' : undeclared identifier
    1>evaluaterecipient.h(18): error C2059: syntax error : '>'
    1>evaluaterecipient.h(18): error C2065: 'Person' : undeclared identifier
    1>evaluaterecipient.h(18): error C2065: 'person' : undeclared identifier
    1>evaluaterecipient.h(18): error C2143: syntax error : missing ',' before ')'
    1>evaluaterecipient.h(19): error C2143: syntax error : missing ';' before '}'
    1>evaluaterecipient.h(23): error C2143: syntax error : missing ';' before '{'
    1>evaluaterecipient.h(28): error C2065: 'Person' : undeclared identifier
    1>evaluaterecipient.h(28): error C2059: syntax error : '>'
    1>evaluaterecipient.h(28): error C2065: 'Person' : undeclared identifier
    1>evaluaterecipient.h(28): error C2065: 'person' : undeclared identifier
    1>evaluaterecipient.h(28): error C2143: syntax error : missing ',' before ')'
    1>evaluaterecipient.h(29): error C2143: syntax error : missing ';' before '}'
    1>evaluaterecipient.h(33): error C2143: syntax error : missing ';' before '{'
    1>evaluaterecipient.h(38): error C2065: 'Person' : undeclared identifier
    1>evaluaterecipient.h(38): error C2059: syntax error : '>'
    1>evaluaterecipient.h(38): error C2065: 'Person' : undeclared identifier
    1>evaluaterecipient.h(38): error C2065: 'person' : undeclared identifier
    1>evaluaterecipient.h(38): error C2143: syntax error : missing ',' before ')'
    1>  EvaluateRecipient.cpp
    1>person.h(36): error C2143: syntax error : missing ';' before '*'
    1>person.h(36): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>person.h(36): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    EvaluateRecipient.h
    Code:
    #ifndef EVALUATE_RECIPIENT_H
    #define EVALUATE_RECIPIENT_H
    
    #include <list>
    #include "Person.h"
    #include "Message.h"
    
    using namespace std;
    
    class EvaluateRecipient
    {
    public:
    	// Constructor
    	EvaluateRecipient();
    
    	virtual list<Person*> getList(Message* message, Person* person) = 0;
    };
    
    // Forward the message to all members of the contact list
    class RStrategy1: public EvaluateRecipient
    {
    public:
    	// Constructor
    	RStrategy1();
    
    	virtual list<Person*> getList(Message* message, Person* person);
    };
    
    // Forward the message to all members of the contact list except (possibly) the sender
    class RStrategy2: public EvaluateRecipient
    {
    public:
    	// Constructor
    	RStrategy2();
    
    	virtual list<Person*> getList(Message* message, Person* person);
    };
    
    // Forward the message to the "next" member on the person's contact list
    class RStrategy3: public EvaluateRecipient
    {
    public:
    	// Constructor
    	RStrategy3();
    
    	virtual list<Person*> getList(Message* message, Person* person);
    };
    
    #endif // EVALUATE RECIPIENT_H
    Person.h
    Code:
    #ifndef PERSON_H
    #define PERSON_H
    
    #include <map>
    #include <list>
    #include <string>
    #include "EvaluateMessage.h"
    #include "EvaluateRecipient.h"
    
    using namespace std;
    
    class Person
    {
    public:
    	// Constructor
    	Person(string emailAddress);
    
    	// Email of the person
    	string email;
    
    	// A list of strings (email addresses), or the Person object's contact list
    	list<string> contactList;
    
    	// A list of Message objects, the inbox of the Person object
    	list<Message*> inbox;
    
    	// A map that maps a MessageContent to an unsigned int
    	map<MessageContent*, unsigned int> timesSeen;
    
    	// Pointer to strategies
    	// Forwarding strategy
    	EvaluateMessage* mStrategy;
    
    	EvaluateRecipient* rStrategy;
    
    };
    
    #endif // PERSON_H
    Any help would be much appreciated, thanks!

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Help, getting really strange errors!

    Try to replace
    Code:
    #include "EvaluateRecipient.h"
    in the person.h with the forward declaration:
    Code:
    class EvaluateRecipient;
    and then insert
    Code:
    #include "EvaluateRecipient.h"
    in the Person implementation file.
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Help, getting really strange errors!

    Quote Originally Posted by Laketaco View Post
    Hello, I'm taking a C++ class, and I keep running into a strange occurrence.
    There is nothing strange.

    Look at your list of includes. Start with EvaluateRecipient.h:
    Code:
    #include <list>
    #include "Person.h"
    Now look at Person.h
    Code:
    #include <map>
    #include <list>
    #include <string>
    #include "EvaluateMessage.h"
    #include "EvaluateRecipient.h"
    See the problem? In EvaluateRecipient.h, you are including Person.h, which includes EvaluateRecipient.h, which includes Person.h, which includes EvaluateRecipient.h, etc.

    So it is an infinite recursion error. You usually solve this by using pointers to these objects and using forward declaration.

    Secondly, don't put "using namespace std" in a header file. The reason is that if I were to use your header, you are forcing the entire std namespace into my C++ module. What if I don't want std namespace in my module, as it may cause compilation errors with other definitions I may have?

    Instead, either prepend "std::" to the definitions that are in the std namespace (std::vector, std::list, etc), or state with the using clause exactly what you are using out of the std namespace:
    Code:
    using std::vector;
    using std::list; 
    // etc.
    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Nov 2011
    Posts
    4

    Re: Help, getting really strange errors!

    Thank you guys so much, I figured it had something to do with infinite recursion I just couldn't figure out how to resolve it.

    I'm coming from learning C# so I guess I took #include to mean the same as C#'s "using" when I guess C#'s keyword is a combined and simplified form of C++'s "using" and "class" keywords?

  5. #5
    Join Date
    Nov 2011
    Posts
    4

    Re: Help, getting really strange errors!

    Excuse the double post but now I'm getting another error. I suspect it is once again due to me not setting up my hierarchy correctly. (Additionally if you have a link to a good hierarchy article I'd be much appreciated).

    So I changed:
    Code:
     #include "EvaluateRecipient.h"
    to the forward declaration:
    Code:
     class EvaluateRecipient;
    and I recieved error "C2061: syntax error: identfier" in this block of code:

    Code:
    // Get the person's strategy on who to forward to in their contact list
    			inputFile >> tempInt;
    
    			switch(tempInt)
    			{
    			// Forward the message to all members of the contact list
    			case 1:
    				(people.back()->rStrategy) = new RStrategy1();
    				break;
    			// Forward the message to all members of the contact list except (possibly) the sender
    			case 2:
    				(people.back()->rStrategy) = new RStrategy2();
    				break;
    			// Forward the message to the "next" member on the person's contact list
    			case 3:
    				(people.back()->rStrategy) = new RStrategy3();
    				break;
    			// For error catching, sets the person's strategy to type 1
    			default:
    				(people.back()->rStrategy) = new RStrategy1();
    				break;
    			}
    So I changed Person.h once again to include:
    Code:
    class RStrategy1;
    class RStrategy2;
    class RStrategy3;
    and now I'm receiving error "C2512 : no appropriate default constructor available" in that same block instead. Is this because I included all child classes in the same header file as EvaluateRecipient? Also, why does EvaluateMessage get to be an "#include" instead of a forward declaration when its built almost exactly the same as EvaluateRecipient? (Note: EvaluateMessage doesn't compile if I try to make it a forward declaration instead of #include). Thanks for your patience.

  6. #6
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Help, getting really strange errors!

    It is enough with just a forward declaration if you just declare pointers to the type in question but whenever you use the type it has to be fully declared.

    Have you included the needed headers in the cpp-file that doesn't compile?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  7. #7
    Join Date
    Nov 2011
    Posts
    4

    Re: Help, getting really strange errors!

    - And that fixed it! I guess my concern at this point is that I'm taking an intro C++ and the teacher is really failing in all regards, and I really don't know how to set up a hierarchy properly and I know that's going to hurt me down the line. I'm going to Google the topic, but if any of you have a good article or link I'd be really grateful. Thank you all for your help!

  8. #8
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Help, getting really strange errors!

    I don't have any links but the general rule is to try to separate things as much as possible. Also, I guess the wisdom in how to achieve the perfect hierarchy has as many prophets as there are programmers...
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured