It should be obvious why extern vector<int> Obj; is generating an error - you've already defined Obj to be a vector of CObject, not a vector of int.
In any case, using global variables is generally a bad habit, that can lead to problems when you start writing bigger programs. As more than one person has already suggested:
pass that vector to the class as an argument to the constructor
Since i've got many suggestions to access a global variable from a class using the declaration:
extern vector<int> Obj;
Um, no. You were shown an example of doing a similar kind of thing, and that example used a vector of int. Nobody told you that a vector of int was what you should use in your program.
If you're just copying and pasting code into your program without understanding what it does and hoping it will magically work, then you are making a big mistake. You won't actually learn anything, and you'll probably end up breaking your program.
The code changes you show for the constructor of the Animal class look fine to me. Those errors look as though you haven't put multiple inclusion guards in the header that defines the CObject class. Either that, or there's some other reason you have CObject defined twice in the same translation unit.
When i try to pass "that vector to the class as an argument to the constructor" the class Animal should know what a vector<CObject> is, right?
for this reason i include the "object.h" in the include section @ line 4 of animal.h
Yes, that's correct.
i can now declare:
Animal (vector<CObject>, string, string, int);
You can do it that way, but then you'll be passing in the entire vector by value, which may cause you to be unnecessarily creating copies of the vector. To ensure that you're not copying the entire vector, you can pass by const reference, instead:
I have to understand, at this point, where i need to add the include guards as you suggested.
It's rarely a good idea to get creative with include guards. Just use them in exactly the same way as they're used in any other header file, unless you have a very good reason not to.
I started using your suggestions (and suggestion by other user of this forum) and really it's going better (especially about the globals var and so on...)
Just about the Global, it's a bad practice to declare Static vector<CObject> Obj. This will be visible to the main source only and could avoid to pass the vector to every function it's needed.
Moreover, the same rule to avoid Globals, should be applied to ENUM?
Just about the Global, it's a bad practice to declare Static vector<CObject> Obj. This will be visible to the main source only and could avoid to pass the vector to every function it's needed.
Well, if you want your file-scope variable to be a global variable, accessible from other files then, yes, you're right.
Moreover, the same rule to avoid Globals, should be applied to ENUM?
Why would there be something wrong with using enum. ?
Well, if you want your file-scope variable to be a global variable, accessible from other files then, yes, you're right.
In this case i want to make static vector visible only in the actual source file, in order to avoid passing the vector to various function: is this a bad practice?
Why would there be something wrong with using enum. ?
Just asking for this, cause i read some questions about this in some forum and i would like to know your opinion.
In this case i want to make static vector visible only in the actual source file, in order to avoid passing the vector to various function: is this a bad practice?
Defining the vector as static stops it being visible at global scope in other files.
It won't stop you being able to pass it to other functions, even if those functions are defined in other files.
I wouldn't say it's bad practice, but it's the C-style way of doing it. The C++ style way would be to define it in an anonymous namespace.
Just asking for this, cause i read some questions about this in some forum and i would like to know your opinion.
Really? I'm not aware of any reason why it might be bad practice to use an enum. Can you point me to those discussions where this was suggested?
Um, those discussions all seem to be discussing different, specific issues. I don't see anything there that suggests you should avoid using enums. Can you clarify exactly what is you're asking?