Undeclared identifier of an vector< user_class > across files

Apr 19, 2012 at 2:33am
Hello, this will make my first post on the cplusplus forums :D

I have (hopefully) simple questions:

Can I declare an extern variable in the header of the file that the variable is in? Or is that a nono?

When a class uses a set of includes, do I have to include those files in every file that uses that class?

That's all. I have gotten stuck on this problem where the compiler is complaining about C2065 error about the vector I am trying to share across files.

I will be extremely grateful if someone could answer my questions but here is the example if anyone enjoys looking at a beginner's work ;)

Files are setup at follows:
c_zone.cpp -- Where the functions/methods of the zone_CLASS are defined.
hcZone.h -- Header file to declare zone_CLASS skeleton.
zone.cpp -- Functions that handle the zone_CLASS class.
hZone.h -- Header for the functions and the general global variable of zone_CLASS.

hZone.h is where the class is defined and shared across other files.

Now here is the start of the error output (they start to repeat):

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
1>  c_zone.cpp
1>c:\_cplusplus\_fpsserver\server, per client, per operation simple\hzone.h(13): error C2065: 'zone_CLASS' : undeclared identifier
1>c:\_cplusplus\_fpsserver\server, per client, per operation simple\c_zone.cpp(17): warning C4018: '<' : signed/unsigned mismatch
1>c:\_cplusplus\_fpsserver\server, per client, per operation simple\c_zone.cpp(25): warning C4018: '<' : signed/unsigned mismatch
1>  c_player.cpp
1>c:\_cplusplus\_fpsserver\server, per client, per operation simple\hplayer.h(11): error C2143: syntax error : missing ';' before '<'
1>c:\_cplusplus\_fpsserver\server, per client, per operation simple\hplayer.h(11): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_cplusplus\_fpsserver\server, per client, per operation simple\hcinstance.h(15): error C2872: 'vector' : ambiguous symbol
1>          could be 'c:\_cplusplus\_fpsserver\server, per client, per operation simple\hplayer.h(11) : int vector'
1>          or       'c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(480) : std::vector'
1>c:\_cplusplus\_fpsserver\server, per client, per operation simple\hcinstance.h(15): error C2872: 'vector' : ambiguous symbol
1>          could be 'c:\_cplusplus\_fpsserver\server, per client, per operation simple\hplayer.h(11) : int vector'
1>          or       'c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(480) : std::vector'
1>c:\_cplusplus\_fpsserver\server, per client, per operation simple\hcinstance.h(15): error C2872: 'vector' : ambiguous symbol
1>          could be 'c:\_cplusplus\_fpsserver\server, per client, per operation simple\hplayer.h(11) : int vector'
1>          or       'c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(480) : std::vector'
1>c:\_cplusplus\_fpsserver\server, per client, per operation simple\hcinstance.h(15): error C2143: syntax error : missing ';' before '<'
1>c:\_cplusplus\_fpsserver\server, per client, per operation simple\hcinstance.h(15): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_cplusplus\_fpsserver\server, per client, per operation simple\hcinstance.h(15): error C2238: unexpected token(s) preceding ';'
1>c:\_cplusplus\_fpsserver\server, per client, per operation simple\hczone.h(16): error C2872: 'vector' : ambiguous symbol
1>          could be 'c:\_cplusplus\_fpsserver\server, per client, per operation simple\hplayer.h(11) : int vector'
1>          or       'c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(480) : std::vector'
1>c:\_cplusplus\_fpsserver\server, per client, per operation simple\hczone.h(16): error C2872: 'vector' : ambiguous symbol
1>          could be 'c:\_cplusplus\_fpsserver\server, per client, per operation simple\hplayer.h(11) : int vector'
1>          or       'c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(480) : std::vector'
1>c:\_cplusplus\_fpsserver\server, per client, per operation simple\hczone.h(16): error C2872: 'vector' : ambiguous symbol
1>          could be 'c:\_cplusplus\_fpsserver\server, per client, per operation simple\hplayer.h(11) : int vector'
1>          or       'c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(480) : std::vector'
1>c:\_cplusplus\_fpsserver\server, per client, per operation simple\hczone.h(16): error C2143: syntax error : missing ';' before '<'
1>c:\_cplusplus\_fpsserver\server, per client, per operation simple\hczone.h(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\_cplusplus\_fpsserver\server, per client, per operation simple\hczone.h(16): error C2238: unexpected token(s) preceding ';'
1>c:\_cplusplus\_fpsserver\server, per client, per operation simple\hzone.h(13): error C2872: 'vector' : ambiguous symbol
1>          could be 'c:\_cplusplus\_fpsserver\server, per client, per operation simple\hplayer.h(11) : int vector'
1>          or       'c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(480) : std::vector'


Even if you can't decipher the error, answers to the questions would be much appreciated. Thank you.
Apr 19, 2012 at 4:25am
> Can I declare an extern variable in the header of the file that the variable is in?

If there is a variable in an implementation file, that is programmatically visible ouside the file (ie. it has external linkage), it must be declared in the corresponding header file.


> When a class uses a set of includes, do I have to include those files in every file that uses that class?

A header file must be compilable by itself. The set of includes that the header file needs must be included in the header file itself. Then in every file that uses the class, only the header needs to be included. For example:

For example, header a.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef A_H_INCLUDED // include guard
#define A_H_INCLUDED

#include <string> // std::string is used in the header
#include <iosfwd> // declaration of std::ostream

struct A
{
    std::string foo() ;
    void print( std::ostream& stm ) const ;

    // ...
};

#endif // A_H_INCLUDED 


Implementation a.cc
1
2
3
4
5
#include "a.h" // the first include; make sure that the header is compilable by itself
#include <iostream> // definition of std::ostream
#include <algorithm> // if it is required for the implementation

// ... 

Topic archived. No new replies allowed.