CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jan 2009
    Posts
    56

    Strange Errors, Possibly Bugged VS

    After creating a new class called Environment I've been getting quite a bit of very strange errors:

    - There were some before where although I included the header file VS would not recognize the symbols of the classes I included, but only for this one file I just created. After I restarted the program the errors went away and VS seemed to again recognize the classes.

    - Now however the errors have seemed to return except in different ways. And restarting did not fix the problem.

    The relevant code:
    Code:
    #include "Environment.h"
    
    #include "../GLAddons/GLAddons.h"
    #include "Avatar.h"
    
    Environment::Environment()
    {
    	salbrisAvatar = Avatar(3, 3, GLAddons::loadTexture("../Textures/Salbris.bmp"), this);
    	humanAvatar = Avatar(6, 6, GLAddons::loadTexture("../Textures/Human.bmp"), this);
    }
    With these errors:
    Error 14 error C2512: 'Avatar' : no appropriate default constructor available
    Error 15 error C2512: 'Avatar' : no appropriate default constructor available
    Error 16 error C2661: 'Avatar::Avatar' : no overloaded function takes 4 arguments
    Error 17 error C2661: 'Avatar::Avatar' : no overloaded function takes 4 arguments




    Here's Environment.h:
    Code:
    #ifndef ENVIRONMENT_H
    #define ENVIRONMENT_H
    
    #include "Avatar.h"
    
    class Environment
    {
    private:
    	static const int GRID_WIDTH = 20;
    	static const int GRID_HEIGHT = 20;
    	char gridSpaces[GRID_WIDTH][GRID_HEIGHT];
    	Avatar salbrisAvatar;
    	Avatar humanAvatar;
    
    	void drawGrid(void) const;
    
    
    public:
    	Environment();
    	~Environment();
    	
    	void draw(void) const;
    };
    
    
    #endif
    With error's on lines: "Avatar salbrisAvatar;" and "Avatar humanAvatar;"
    Error 2 error C2146: syntax error : missing ';' before identifier 'salbrisAvatar'
    Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    Error 4 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    Error 5 error C2146: syntax error : missing ';' before identifier 'humanAvatar'
    Error 6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    Error 7 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int




    Here's Avatar.h:
    Code:
    #ifndef AVATAR_H
    #define AVATAR_H
    
    
    #ifdef __APPLE__
    #include <OpenGL/OpenGL.h>
    #include <GLUT/glut.h>
    #else
    #include <GL/glut.h>
    #endif
    
    #include "Environment.h"
    
    class Avatar
    {
    private:
    	unsigned int positionX;
    	unsigned int positionY;
    
    	unsigned int textureId;
    
    
    public:
    	static const float AvatarSize;
    
    	Environment *parent;
    
    	Avatar(unsigned int, unsigned int, unsigned int, Environment *);
    	~Avatar();
    
    	void draw(void) const;
    	void moveTo(unsigned int, unsigned int);
    };
    
    
    #endif
    Avatar.h also has some errors:
    - Line "Envrionment *parent" has the errors:
    Error 10 error C2143: syntax error : missing ';' before '*'
    Error 11 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    Error 12 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int


    - Line "Avatar(unsigned int, unsigned int, unsigned int, Environment *);" has the error:
    Error 13 error C2061: syntax error : identifier 'Environment'



    I've been working with C++ for a week and I've made many other classes, functions, etc. I've checked over these files many times but I can't seem to figure out what's wrong.

    Any help would be greatly appreciated.

  2. #2
    Join Date
    Jun 2009
    Posts
    14

    Re: Strange Errors, Possibly Bugged VS

    Code:
    #include "Environment.h"
    
    #include "../GLAddons/GLAddons.h"
    #include "Avatar.h"
    
    Environment::Environment()
    {
    	salbrisAvatar = Avatar(3, 3, GLAddons::loadTexture("../Textures/Salbris.bmp"), this);
    	humanAvatar = Avatar(6, 6, GLAddons::loadTexture("../Textures/Human.bmp"), this);
    }
    You already include Avatar.h in Environment.h. That double inclusion could cause some of the errors.

  3. #3
    Join Date
    Jan 2009
    Posts
    56

    Re: Strange Errors, Possibly Bugged VS

    Quote Originally Posted by maglite View Post
    You already include Avatar.h in Environment.h. That double inclusion could cause some of the errors.
    Thanks for catching that. Although this fixed no errors, probably because I have Header Guards to prevent such problems.


    However I researched my problem a bit further and found a solution but not really. It seems my problem is called Circular Dependency, since Environment needs Avatar and vice versa. I'm lucky that Avatar only need pointers to Environment objects so I can use Forward Declaration (just about this as well).

    But I'm wondering what's the solution if both classes need each other's headers but for instances instead of just pointers and references? How could this problem be solved then?

  4. #4
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Strange Errors, Possibly Bugged VS

    Before the declaration of Avatar class, forward declare Environment class without including Environment.h:
    Code:
    class Environment;
     
    class Avatar
    {
    ...
    You cannot do the same for Environment class, since Environment is having data member of type Avatar (instead of pointer or reference). Compiler needs complete type definition to determine class size. Thus only the solution I mentioned above would work.

    Remember pointers are always of same size, no matter what data type.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  5. #5
    Join Date
    Jan 2009
    Posts
    56

    Re: Strange Errors, Possibly Bugged VS

    Quote Originally Posted by Ajay Vijay View Post
    Before the declaration of Avatar class, forward declare Environment class without including Environment.h:
    Code:
    class Environment;
     
    class Avatar
    {
    ...
    You cannot do the same for Environment class, since Environment is having data member of type Avatar (instead of pointer or reference). Compiler needs complete type definition to determine class size. Thus only the solution I mentioned above would work.

    Remember pointers are always of same size, no matter what data type.
    Ya I just found that answer as well. But I'm wonder now how would I solve this issue if both headers needed the other for instances instead of pointers and references?

    Although thank for that tidbit of info, I didn't realize the reason it needs the complete definition to know it's size.

  6. #6
    Join Date
    Aug 2007
    Posts
    858

    Re: Strange Errors, Possibly Bugged VS

    The relevant code:
    Code:
    Code:
    #include "Environment.h"
    
    #include "../GLAddons/GLAddons.h"
    #include "Avatar.h"
    
    Environment::Environment()
    {
    	salbrisAvatar = Avatar(3, 3, GLAddons::loadTexture("../Textures/Salbris.bmp"), this);
    	humanAvatar = Avatar(6, 6, GLAddons::loadTexture("../Textures/Human.bmp"), this);
    }
    With these errors:
    Error 14 error C2512: 'Avatar' : no appropriate default constructor available
    Error 15 error C2512: 'Avatar' : no appropriate default constructor available
    All of an objects members must be constructed in some manner before the opening brace of the constructor. Since you haven't told it to do otherwise, it's trying to default construct the Avatar members, but apparently Avatar does not have a default constructor. You'll either need to use an initialization list to initialize the Avater members, or else supply the class with a default ctor.

    But I'm wondering what's the solution if both classes need each other's headers but for instances instead of just pointers and references? How could this problem be solved then?
    It can't be done. When the compiler reads a class definition, it needs to be able to calculate the size of an instance of that class. In order to do that, it needs to know the size of all the members of the class. So you have something like

    Code:
    struct A
    {
      B b;
    };
    
    struct B
    {
      A a;
    };
    In order to complete the definition of A the compiler needs to know the definition of B. But in order to complete the definition of B the compiler needs to know the definition of A. The only way to break that cycle is to use pointers or references.

  7. #7
    Join Date
    Jan 2009
    Posts
    56

    Re: Strange Errors, Possibly Bugged VS

    Quote Originally Posted by Speedo View Post
    It can't be done. When the compiler reads a class definition, it needs to be able to calculate the size of an instance of that class. In order to do that, it needs to know the size of all the members of the class.
    Are you sure there is absolutely no way? Can anyone else confirm this?

    Not that I don't trust you but I'd just like to be certain. Although, it's not a big deal since this problem is rare, and easy to fix... Just make all the uses pointers or references in one of the classes.

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

    Re: Strange Errors, Possibly Bugged VS

    Are you sure there is absolutely no way? Can anyone else confirm this?
    Once you introduce an object (not a reference or pointer to an object), the compiler must know the sizeof() that object.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Aug 2007
    Posts
    858

    Re: Strange Errors, Possibly Bugged VS

    Quote Originally Posted by Salbrismind View Post
    Are you sure there is absolutely no way?
    It's literally an infinite circle.

    A needs to know B needs to know A needs to know B needs to know A needs to know B needs to know A needs to know B...

    Even if you changed changed the rules of the language around to be less rigid, it just can't work. A can never be completed because it's waiting on B... B can never be completed because it's waiting on A.

  10. #10
    Join Date
    Jan 2009
    Posts
    56

    Re: Strange Errors, Possibly Bugged VS

    Quote Originally Posted by Speedo View Post
    It's literally an infinite circle.

    A needs to know B needs to know A needs to know B needs to know A needs to know B needs to know A needs to know B...

    Even if you changed changed the rules of the language around to be less rigid, it just can't work. A can never be completed because it's waiting on B... B can never be completed because it's waiting on A.
    Quote Originally Posted by Paul McKenzie View Post
    Once you introduce an object (not a reference or pointer to an object), the compiler must know the sizeof() that object.
    Thanks, I'm just not accustomed to things have NO solution. All well, it's not to difficult to "fix".

    Thanks, a lot everyone.

  11. #11
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Strange Errors, Possibly Bugged VS

    A building can contain a floor, a floor can contain an apartment.

    The apartment can refer to the floor, the floor can refer the building in which they reside respectively.

    contain is containment (no pointer/reference).
    refer is to reference (with pointer/reference).

    You see that floor cannot contain the building.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  12. #12
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Strange Errors, Possibly Bugged VS

    Another way to look at it, let's say you have some red boxes and blue boxes

    You can put a red box in all the blue boxes, or a blue box in all the red boxes. However, if you put a red box in all the blue boxes, then try to put a blue box in all the red boxes, you'll end up with either an infinite number of boxes (in which case the outside box will have to be very big :-) ), or you'll end up with a box in the middle which doesn't have another box inside it. This is the infinite circle that speedo mentioned.

    Sorry if this is patronising, just thought I'd try it out as an explanation.

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