English 中文(简体)
the compiler doesn t seem to accept Agent class
原标题:

probably the answer is quite silly but I need a pair fresh of eyes to spot the problem, if you will. this is the excerpt from _tmain:

Agent theAgent(void);
int m = theAgent.loadSAG();

and this is agent.h, which I included in _tmain:

#ifndef AGENT_H
#define AGENT_H
class Agent {
public:
Agent(void);
int loadSAG(void);
~Agent(void);
};
#endif

and agent.cpp relevant function:

int Agent::loadSAG(void) {
return 3;
}

so why in the world I get this error: error C2228: left of .loadSAG must have class/struct/union ?

Thanks in advance.

最佳回答
Agent theAgent(void);

This is a function declaration, just change it to:

Agent theAgent;
问题回答

Compiler thinks

Agent theAgent(void);

This to be a function declaration.

Agent theAgent;

The line

Agent theAgent(void);

Is actually viewed by the compiler as declaring the function theAgent that takes no arguments and returns an Agent.

This is explained the the C++ FAQ Lite.

To call the default constructor and set up an object of type Agent (as opposed to the statement above that s interpreted as a function declaration,) you can just declare theAgent without using parentheses at all, as in:

Agent theAgent;

All normal member calls, such as loadSAG will work as expected after this point.

As an alternative, if you must have the object on the heap, use this instead:

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 new

When calling a default (parameterless) constructor you do so without the () . Ie. Try constructing your agent object:

Agent theAgent;

If by Agent theAgent(void); you meant function declaration then you probably forgot to add () to the function call

int m = theAgent().loadSAG();

If you wanted to define a variable with the name theAgent then you have added extra (void). and instead you should wrote

Agent theAgent;





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签