2011-06-23 35 views
1

我觉得这个问题有点超出我的意思。我会很感激任何帮助。“术语不评估为全局对象中的函数......”

UIM_Commander.cpp,我需要使用UIM_Parser类中的(静态)方法。所以,在UIM_Commander.h,我包括UIM_Parser.h,就像这样:

#ifndef UIM_Commander_h 
#define UIM_Commander_h 

#include "..\sysm\SYSM.h" 
#include "..\storm\STORM.h" 
#include "..\ssqlm\SSQLM.h" 
#include "..\inxm\INXM.h" 
#include "UIM_Parser.h" 

class UIM_Commander 
{ 
..... 
}; 

#endif 

然而,这导致这个错误:

Error 8 error C2064: term does not evaluate to a function taking 0 arguments c:\workspace\sirenbase\sirenbase\uim\uim_main.cpp 3

UIM_Main.cpp

#include "UIM_Main.h" 

UIM_Commander commandCentre = UIM_Commander(); // <-- ERROR term does not evaluate to a function taking 0 arguments 
list<string> stash; 

int normal(int argc, char *argv[]) 
{ 
..... 
} 

int main(int argc, char *argv[]) 
{ 
..... 
} 

当然,我检查了UIM_Ma in.h中但似乎UIM_Commander.h已经包含有:

#ifndef UIM_Main_h 
#define UIM_Main_h 

#define NORMAL_MODE true //set this to false to run testmain 

#include "..\storm\STORM.h" 
#include "UIM_Commander.h" 
#include "UIM_tokens.h" 
#include "UIM_Parser.h" 
#include <list> 
#include <string> 
#include <iostream> 
#include <algorithm> 
#include <cctype> 
#include <vector> 
using namespace std; 

int main(int argc, char *argv[]); 
int normal(int argc, char *argv[]); 

#endif 

全球,commandCentre,在仅用于UIM_Parser.cpp,所以我不得不它作为UIM_Parser一个外部。^h

#ifndef UIM_Parser_h 
#define UIM_Parser_h 

#include "UIM_Commander.h" 
#include "UIM_Main.h" 
#include "..\storm\STORM.h" 
#include "UIM_tokens.h" 
#include <list> 
#include <string> 
#include <sstream> 
using namespace std; 

extern UIM_Commander commandCentre; // <-- ALTERNATE ERROR missing ';' before identifier 'commandCentre' 
             //see below 
extern list<string> stash; 

class UIM_Parser 
{ 
..... 
}; 

#endif 

如果它可以帮助任何在改变顺序UIM_Main.h

#include "UIM_tokens.h" 
#include "UIM_Parser.h" 
#include "UIM_Commander.h" 

改变错误

Error 1 error C2146: syntax error : missing ';' before identifier 'commandCentre' c:\workspace\sirenbase\sirenbase\uim\uim_parser.h 13

那么,我究竟做错了什么?这是一种奇怪的循环定义,包容卫兵不能救我?


编辑:

使用UIM_Commander commandCentre;如果#include "UIM_Commander.h"#include "UIM_Parser.h"UIM_main.h改变错误

`Error 7 error C2086: 'int commandCentre' : redefinition c:\workspace\sirenbase\sirenbase\uim\uim_main.cpp 3

。如果#include "UIM_Parser.h"#include "UIM_Commander.h"之前,则错误停留

Error 1 error C2146: syntax error : missing ';' before identifier 'commandCentre' c:\workspace\sirenbase\sirenbase\uim\uim_parser.h 13


再次编辑:我解决这得益于StevieG的答案。现在UIM_Parser.h看起来是这样的:

#ifndef UIM_Parser_h 
#define UIM_Parser_h 

#include "UIM_Main.h" 
#include "..\storm\STORM.h" 
#include "UIM_tokens.h" 
#include <list> 
#include <string> 
#include <sstream> 
using namespace std; 

class UIM_Commander; 

extern UIM_Commander commandCentre; 
extern list<string> stash; 

class UIM_Parser 
{ 
..... 
}; 

#endif 

整齐漂亮,呵呵。

回答

1

你可以向前UIM_Parser.h声明UIM_Commander,并保持您的包括s一起..

0

编辑:对不起,旧的答案是BS。

只是说:

UIM_Commander commandCentre; 

默认构造的一点是,它在默认情况下调用,所以你并不需要指定它。但即使你有一个非默认的构造函数,你所做的也是低效的;不要使用拷贝构造函数,但直接构造:

Foo x = Foo(1,2,3); // silly copy! 
Foo x(1,2,3);  // direct. 
+0

嗨,当我尝试这个,错误是不同的,但代码仍然不能编译。我用更多的信息编辑了我的问题。 – Naurgul

+0

你确定所有'class'和'struct'结构都以分号结束吗? –

+0

是的。问题是循环依赖。我认为现在已经解决了。我自己添加一个答案。无论如何感谢Kerrek! – Naurgul

0

我的一个好朋友解决了这个,我是一个在这里添加的答案,因为他不希望。

的问题是,UIM_Parser.h需要UIM_Commander.h知道什么UIM_Commander是。与此同时,UIM_Commander.cpp需要UIM_Parser.h能够调用其方法。

的解决方案,在UIM_Commander.h,在该文件的末尾移动#include "UIM_Parser.h"#endif前右,像这样:

#ifndef UIM_Commander_h 
#define UIM_Commander_h 

#include "..\sysm\SYSM.h" 
#include "..\storm\STORM.h" 
#include "..\ssqlm\SSQLM.h" 
#include "..\inxm\INXM.h" 

class UIM_Commander 
{ 
..... 
}; 

#include "UIM_Parser.h" 

#endif 

或者,你可以把#include "UIM_Parser.h"开始UIM_Commander.cpp

#include "UIM_Commander.h" 
#include "UIM_Parser.h" 

UIM_Commander::UIM_Commander() 
{ 
    storManager = new STORM_StorageManager(); 
    sysManager = new SYSM_Manager(storManager); 
    sqlManager = new SSQLM_Manager(storManager); 
    indManager = new INXM_IndexManager(storManager); 
} 

..... 
+1

在UIM_Parser.h中将UIM_Commander声明为UIM_Commander并保持包含在一起会更好吗? – StevieG

+0

@StevieG是的,这也可以。我甚至不知道你可以转发声明类。如果您将您的评论转换为完整的答案,我会接受它。 – Naurgul

相关问题