也许答案是相当愚蠢的,但如果你愿意的话,我需要一双新鲜的眼睛来发现问题。以下是_tmain的摘录:
Agent theAgent(void);
int m = theAgent.loadSAG();这是agent.h,我将它包含在_tmain中:
#ifndef AGENT_H
#define AGENT_H
class Agent {
public:
Agent(void);
int loadSAG(void);
~Agent(void);
};
#endif和agent.cpp相关函数:
int Agent::loadSAG(void) {
return 3;
}那么到底为什么我会得到这样的错误: error C2228:'.loadSAG‘的左边必须有类/结构/联合?
提前谢谢。
发布于 2009-11-19 00:20:15
Agent theAgent(void);这是一个函数声明,只需将其更改为:
Agent theAgent;发布于 2009-11-19 00:21:36
编译器认为
Agent theAgent(void);这是一个函数声明。
Agent theAgent;发布于 2009-11-19 00:23:22
这条线
Agent theAgent(void);实际上被编译器视为声明了不带参数并返回代理的函数theAgent。
这在C++ FAQ Lite中有解释。
要调用默认构造函数并设置Agent类型的对象(与上面解释为函数声明的语句相反),您可以完全不使用括号来声明theAgent,如下所示:
Agent theAgent;在此之后,所有正常的成员调用,如loadSAG,都将按预期工作。
另一种方法是,如果必须将对象放在堆上,请使用以下代码:
Agent* theAgent = new Agent(); // Notice the *
theAgent->loadSAG(); // Use -> instead of .
// The code where theAgent is used
delete theAgent; // This frees the memory allocated by newhttps://stackoverflow.com/questions/1757159
复制相似问题