声明和访问全局对象

发布于 2024-11-06 20:17:56 字数 1252 浏览 1 评论 0原文

众所周知的方法是声明 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

嘿咻 2024-11-13 20:17:57

与任何类一样,编译器在使用它之前需要查看它的定义。将列表的定义放在标题中,并将其包含在使用列表的类中。这与全局对象无关。

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.

巨坚强 2024-11-13 20:17:56

extern 应该在头文件中使用。将extern list *x放入头文件中,实际上是向所有源文件宣告存在一个全局变量x,并且它是在其他地方声明的。如果您不遵循这种方法,而是在源文件中写入 extern list *x (就像 @Als 所做的那样),那么您实际上是在隐藏公告,并且没有让所有源都清楚全局变量 x 存在的文件。那么可能会发生什么呢?您稍后可能会创建另一个源文件 file3.cpp,其中您可能错误地声明了名为 x 的全局变量,这将导致链接器错误。

因此,解决方案是做出明确的声明,而声明的正确位置是头文件 - 因为这是所有其他声明的位置。

因此,您的代码应如下所示:

  • list.h:

    类列表 { ....};  
    extern list * x;//extern表示变量声明在其他地方
    
  • 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来限定它。写成这样:

   std::queue <errorStruct> errQueue; 
 //^^^^^ mention the namespace

同样,如果您使用string(您已为其添加了头文件),则编写std::string

extern should be used in the header file. Putting extern list *x in the header file is actually an announcement to all source files that there exists a global variable x and it is declared somewhere else. If you don't follow this approach, and write extern 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 variable x exists. Then what might happen? It might happen that you later create another source file file3.cpp, in which you might mistakenly declare a global variable with name x, 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:

    class list { ....};  
    extern list * x;//extern means the variable declaration is elsewhere
    
  • list.cpp:

    #include "list.h"
    
    list * x;   //alright, the variable declaration is here!
    
    //use x
    
  • file1.cpp

    #include "list.h"
    
    //use x - no need to write : extern list *x
    
  • file2.cpp

    #include "list.h"
    
    //use x  - no need to write : extern list *x
    

As for the error related to queue, you should qualify it with the namespace std in which its defined. Write this:

   std::queue <errorStruct> errQueue; 
 //^^^^^ mention the namespace

Similarly, if you use string (for which you've included the header file), then write std::string.

淑女气质 2024-11-13 20:17:56

您的file2.cpp应包含具有类列表定义的标头。

myList.h

class list { ....};  

file1.cpp

#include "myList.h"
list * x ; 

file2.cpp >

#include "myList.h"
extern list * x;  

Your file2.cpp should include header which has definition of class list.

myList.h

class list { ....};  

file1.cpp

#include "myList.h"
list * x ; 

file2.cpp

#include "myList.h"
extern list * x;  
巴黎夜雨 2024-11-13 20:17:56

错误 C2143:语法错误:缺少“;”在“<”之前

它是“std::queue”,而不是“queue”。

error C2143: syntax error : missing ';' before '<'

It's "std::queue<errorStruct>", not "queue<errorStruct>".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文