声明和访问全局对象
众所周知的方法是声明 int globalVar = 0
然后 - 在哪里使用它 - extern int globalVar;
但这对于我的对象不起作用用户定义的类!
这是我所做的一个示例:
file1.cpp:
#include "file2.h"
class list { ....};
list * x ;
//do something with x`
file2.cpp:
class list;
extern list * x;
//do something with x
这是我得到的错误:
error C2027: use of undefined type 'list'
如果我删除 class list
error C2143: syntax error : missing ';' before '*' // in `extern list *x;`
error C4430: missing type specifier - int assumed. Note: C++ does not support
default-int
感谢您的回答,现在是这样的:
#ifndef _ERRLIST_H_
#define _ERRLIST_H_
#include <queue>
#include <string>
struct errorStruct{
int errLineNum;
int errColNum ;
char * errMessage;
};
queue <errorStruct> errQueue; //error points here
class ErrList
{
public:
void pushError(int line,int col,char * message);
void popError();
void printErrors();
int getSize();
};
#endif
但我得到了这个错误 :
error C2143: syntax error : missing ';' before '<'
The well known method for this is to declare int globalVar = 0
and then -where to use it- extern int globalVar;
but that didn't work with me for objects from user defined class!
This is an example of what I did:
file1.cpp:
#include "file2.h"
class list { ....};
list * x ;
//do something with x`
file2.cpp:
class list;
extern list * x;
//do something with x
and this is the error I get:
error C2027: use of undefined type 'list'
if I remove class list
error C2143: syntax error : missing ';' before '*' // in `extern list *x;`
error C4430: missing type specifier - int assumed. Note: C++ does not support
default-int
thanks for answers , now it's like this :
#ifndef _ERRLIST_H_
#define _ERRLIST_H_
#include <queue>
#include <string>
struct errorStruct{
int errLineNum;
int errColNum ;
char * errMessage;
};
queue <errorStruct> errQueue; //error points here
class ErrList
{
public:
void pushError(int line,int col,char * message);
void popError();
void printErrors();
int getSize();
};
#endif
but I get this error :
error C2143: syntax error : missing ';' before '<'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
与任何类一样,编译器在使用它之前需要查看它的定义。将列表的定义放在标题中,并将其包含在使用列表的类中。这与全局对象无关。
As with any class, the complier needs to see its definition before using it. Put the definition of list in a header and include it in the class which uses the list. This is nothing to do with global objects.
extern
应该在头文件中使用。将extern list *x
放入头文件中,实际上是向所有源文件宣告存在一个全局变量x
,并且它是在其他地方声明的。如果您不遵循这种方法,而是在源文件中写入extern list *x
(就像 @Als 所做的那样),那么您实际上是在隐藏公告,并且没有让所有源都清楚全局变量 x 存在的文件。那么可能会发生什么呢?您稍后可能会创建另一个源文件file3.cpp
,其中您可能错误地声明了名为x
的全局变量,这将导致链接器错误。因此,解决方案是做出明确的声明,而声明的正确位置是头文件 - 因为这是所有其他声明的位置。
因此,您的代码应如下所示:
list.h:
list.cpp:
<前><代码>#include“list.h”
列表 * x; //好了,变量声明就在这里!
//使用x
file1.cpp
<前><代码>#include“list.h”
//使用x - 无需编写:extern list *x
file2.cpp
<前><代码>#include“list.h”
//使用x - 无需编写:extern list *x
至于与
queue
相关的错误,您应该使用定义它的命名空间std
来限定它。写成这样:同样,如果您使用
string
(您已为其添加了头文件),则编写std::string
。extern
should be used in the header file. Puttingextern list *x
in the header file is actually an announcement to all source files that there exists a global variablex
and it is declared somewhere else. If you don't follow this approach, and writeextern list *x
in source file instead (as @Als's did), then you're actually hiding the announcement, and not making it clear to all source files that the global variablex
exists. Then what might happen? It might happen that you later create another source filefile3.cpp
, in which you might mistakenly declare a global variable with namex
, which would cause linker error.So the solution is to make a clear announcement and the correct place of announcement is header file - as that is where all other announcements are made.
Thus, your code should look like this:
list.h:
list.cpp:
file1.cpp
file2.cpp
As for the error related to
queue
, you should qualify it with the namespacestd
in which its defined. Write this:Similarly, if you use
string
(for which you've included the header file), then writestd::string
.您的
file2.cpp
应包含具有类列表
定义的标头。myList.h
file1.cpp
file2.cpp >
Your
file2.cpp
should include header which has definition ofclass list
.myList.h
file1.cpp
file2.cpp
它是“std::queue”,而不是“queue”。
It's "std::queue<errorStruct>", not "queue<errorStruct>".