Errors in class

Right I don't know why these errors are occuring at all so I'm just gonna post code and errors and hope someone can help me?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Object{
  public:
    //class variables
    //First lot of errors
    static Object* firstObejct;//Start of internal list
    static Object* lastObject;//End of internal list
    Object* prevObject;//Pointer to previous Object
    Object* nextObject;//Pointer to next Object
  public:
    //Second lot of errors
    Object();
    Object(float arg[]);
    Object(Object &c);
    virtual ~Object();
};


//First Errors
5 ...\Project\Classes\Object.cpp ISO C++ forbids declaration of `firstObejct' with no type
6 ...\Project\Classes\Object.cpp ISO C++ forbids declaration of `lastObject' with no type
7 ...\Project\Classes\Object.cpp ISO C++ forbids declaration of `prevObject' with no type
8 ...\Project\Classes\Object.cpp ISO C++ forbids declaration of `nextObject' with no type

//Second Errors
11 ...\Project\Classes\Object.cpp expected unqualified-id before ')' token 
12 ...\Project\Classes\Object.cpp expected unqualified-id before "float"
12 ...\Project\Classes\Object.cpp expected `)' before "float"
13 ...\Project\Classes\Object.cpp expected class-name before '(' token
14 ...\Project\Classes\Object.cpp ISO C++ forbids declaration of `c' with no type
14 ...\Project\Classes\Object.cpp `c' declared as a `virtual' field
Last edited on
constructors cannot be declared virtual.
1
2
3
//Object(&Object c);
Object(Object &c);
Object(const Object &c);


> ISO C++ forbids declaration of `firstObejct' with no type
That's weird, ┬┐what compiler are you using?


I'll recommend you to separate the `node' from the `list'


//First Errors
5 ...\Project\Classes\Object.cpp ISO C++ forbids declaration of `firstObejct' with no type


The 5 suggests that these are not, in fact, your first errors. Given that "first" errors often affect what may be listed as errors after them, the "first" errors are probably the ones you need to correct, and not the ones you have listed here.
Yes silly me, the chapter on polymorphism in my book mentions about calling constructors from derived classes, it only mentions using destructors and other general functions as virtual...
Whoopsy.

I'll correct this when I'm next on my computer and hopefully the pointers will correct too! I'll let you know if it works, cheers!
The 5 suggests that these are not, in fact, your first errors

It's a line number :).

Aside from virtual constructor there is another error: Object(&Object c); should be Object(Object &c);
Ok, I've updated my first post to the corrected code but now all errors still persist. Not sure whats up still -_-
It should compile now.
You mean that second set of errors persist too?
The code snip does not contain errors. It seems thet the invalid code is somewhere above this code snip.
Last edited on
Ok, I didn't think there should be any errors remaining... This is my full class (of declarations, the definitions are outside the class) and there are more errors related to other things but I thought I'd try sort 1 thing out at a time.

@Project/Classes/Object.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Object{//1 ...\Project\\Classes\Object.cpp an anonymous union cannot have function members
  private:
    const int id;
    const string type;
//4 ...\Project\\Classes\Object.cpp member const std::string <anonymous class>::type' with constructor not allowed in anonymous aggregate
//4 ...\Project\\Classes\Object.cpp member const std::string <anonymous class>::type' with destructor not allowed in anonymous aggregate
//4 ...\Project\\Classes\Object.cpp member const std::string <anonymous class>::type' with copy assignment operator not allowed in anonymous aggregate 
    const string name;
//8 ...\Project\\Classes\Object.cpp member const std::string <anonymous class>::name' with constructor not allowed in anonymous aggregate
//8 ...\Project\\Classes\Object.cpp member const std::string <anonymous class>::name' with destructor not allowed in anonymous aggregate
//8 ...\Project\\Classes\Object.cpp member const std::string <anonymous class>::name' with copy assignment operator not allowed in anonymous aggregate
    const string image;
//12 ...\Project\\Classes\Object.cpp member const std::string <anonymous class>::image' with constructor not allowed in anonymous aggregate
//12 ...\Project\\Classes\Object.cpp member const std::string <anonymous class>::image' with destructor not allowed in anonymous aggregate
//12 ...\Project\\Classes\Object.cpp member const std::string <anonymous class>::image' with copy assignment operator not allowed in anonymous aggregate
    const float sizeBounds[2];
    float pos[3];

    static int numObjects;
    static Object* firstObejct;//20 ...\Project\\Classes\Object.cpp ISO C++ forbids declaration of `firstObejct' with no type 
    static Object* lastObject;//21 ...\Project\\Classes\Object.cpp ISO C++ forbids declaration of `lastObject' with no type 
    Object* prevObject;//22 ...\Project\\Classes\Object.cpp ISO C++ forbids declaration of `prevObject' with no type 
    Object* nextObject;//23 ...\Project\\Classes\Object.cpp ISO C++ forbids declaration of `nextObject' with no type 
  public:
    Object();//25 ...\Project\\Classes\Object.cpp expected unqualified-id before ')' token
    Object(float arg[]);
//26 ...\Project\\Classes\Object.cpp expected unqualified-id before "float"
//26 ...\Project\\Classes\Object.cpp expected `)' before "float"
    Object(Object &c);//29 ...\Project\\Classes\Object.cpp expected `)' before "float"
    virtual ~Object();//30 ...\Project\\Classes\Object.cpp expected class-name before '(' token
    float resolveRotation();
    int getId();
    string getType();
    string getName();
    string getImage();
    float getSizeBound(int art);
    float getPos(int arg);
    float getRotation();
    int getNumObjects();
    Object& getFirstObject();//40 ...\Project\\Classes\Object.cpp ISO C++ forbids declaration of `getFirstObject' with no type
    Object& getLastObject();//41 ...\Project\\Classes\Object.cpp ISO C++ forbids declaration of `getLastObject' with no type
    Object& getNextObject();//42 ...\Project\\Classes\Object.cpp ISO C++ forbids declaration of `getNextObject' with no type
    Object& getPrevObject();//43 ...\Project\\Classes\Object.cpp ISO C++ forbids declaration of `getPrevObject' with no type
    
    int setPos(float arg[]);
    void setRotation(float arg);
    void resetRotation();
};


@Project/Headers/Object.h
1
2
3
4
5
#ifndef Object
 #define Object
 #include <string>
 #include "../Classes/Object.cpp"
#endif 


@Project/main.cpp
1
2
3
4
5
6
7
8
#include <stdio>
#include "Headers/Object.h"

int main(){
    printf("\nIn app pause.\nPress enter to continue.\n");
    getchar();
    return 0;
}


As you can see there is literally nothing in the main class of yet, I have done this so that the compile log I get is for the Object class only... If it compiles correctly all of Object.cpp will be ignored anyway as it isn't used and I will get a Dow window prompting me to hit enter.
Last edited on
What compiler do you use?
It's seriosly messed up. Only actual errors there is lines 40-43: It's Object&, not &Object.
!!! It suddenly hits me: Do you have an header guard somewhere like
1
2
#ifndef Object
#define Object 
?
//1 ...\Project\\Classes\Object.cpp an anonymous union cannot have function members


This error implies that there's something wrong in a file that's included by "Object.cpp", which is causing the subsequent errors. Since the compiler is complaining about the class declaration itself, the actual issue is almost definitely before that. Perhaps you've missed a semicolon or a closing brace somewhere?
Have you initialized the const variables ? I think you have forgotten, this maybe one of the errors. Initialize them in the initialize list.
Last edited on
There are no errors appearing at all about constant variables... Also this is supposed to be a abstract class from which I create more things and properly define all the constants as well as the definition in this class.
I've update the above post to add the other files included in compilation.

Oh yeah, I'm using Dev-C++ 4.9.9.2 if that helps at all?
Last edited on
#define Object
That is the problem.
You see, when you do #define Something SomethingElse it will replace all instance of Something with SomethingElse
In your case it is #define Object <nothing> so all instances of "Object" will be deleted. First lines of your file would be like this:
1
2
3
class {  //That is an defiition of anonymous class (or struct)
  private:
//... 

Change your header guard to something else (OBJECT - all uppercase - will be enough)

And I cannot not to tell that you did headers wrong: if you try to use it in more than one file, you will get linker errors.
1
2
3
4
5
#ifndef Object
 #define Object
 #include <string>
 #include "../Classes/Object.cpp"
#endif 

You are also including Object.cpp in Object.h and you shouldn't.
Oh of course! The macros!
Well that's great... Now I'm getting lots and lots of other random errors about constants and undeclared/undefined variables. So at least I've got passed that part, thanks guys!

I'll update anything I can't fix from here on in... Again cheers!

Also how would you suggest creating the header, I didn't fully understand the tutorial on here so I tried to at least make sure I could avoid some errors (the current header should stop including the same file twice and creating infinite loops between includes at least)
Last edited on
Topic archived. No new replies allowed.