开发者

"term does not evaluate to a function taking ..." in a global object

I think this problem is a bit beyond me. I'd appreciate any help.

In UIM_Commander.cpp, I need to use a (static) method from the class UIM_Parser. So, in UIM_Commander.h, I included UIM_Parser.h, like so:

#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

However, this leads to this error:

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[])
{
.....   
}

Naturally, I checked in UIM_Main.h but it seems UIM_Commander.h is already included there:

#ifndef UIM_Main_h
#de开发者_StackOverflow社区fine 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

The global, commandCentre, is only used in UIM_Parser.cpp, so I had it as an extern in 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

If it helps any, changing the order in UIM_Main.h to

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

changes the error to

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

So, what am I doing wrong? Is this some sort of weird cyclic definition that the inclusion guards can't save me from?


EDIT:

Using UIM_Commander commandCentre; changes the error to

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

if #include "UIM_Commander.h" is before #include "UIM_Parser.h" in UIM_main.h. If #include "UIM_Parser.h" is before #include "UIM_Commander.h", then the error stays

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


EDIT AGAIN: I solved it thanks to StevieG's answer. Now UIM_Parser.h looks like this:

#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

Pretty neat, huh.


You could forward declare UIM_Commander in UIM_Parser.h, and keep your includes together..


Edit: Sorry, old answer was BS.

Just say:

UIM_Commander commandCentre;

The point of the default constructor is that it's invoked by default, so you don't need to specify it. But even if you had a non-default constructor, what you did is inefficient; don't use the copy constructor but construct directly:

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


A good friend of mine solved this, I'm the one adding the answer here because he doesn't want to.

The problem is that UIM_Parser.h needs UIM_Commander.h to know what UIM_Commander is. At the same time, UIM_Commander.cpp needs UIM_Parser.h to be able to call its methods.

The solution is, in UIM_Commander.h, to move #include "UIM_Parser.h" at the end of the file, right before #endif, like so:

#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

Alternatively, you can put #include "UIM_Parser.h" in the beginning of 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);
}

.....
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜