views:

100

answers:

6

Hey,

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.

+1  A: 

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;

Mykola Golubyev
Isn't that just going to try and call an operator() on the 'theAgent' object, for which there is not one defined, but if there were it would then attempt to use .loadSAG(); on the return value of the () operator...
DeusAduro
That's not helpful to a beginner. If you are going to cheat like that at least explain the mad mumbo jumbo you are doing.
Martin York
Answering the question I thought that in the question it is a function declaration.
Mykola Golubyev
yes, that would be helpful
Radagaisus
+1  A: 

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

Agent theAgent;
DeusAduro
+7  A: 
Agent theAgent(void);

This is a function declaration, just change it to:

Agent theAgent;
AraK
Also, you don't need to write `void` in functions that take no parameters
Charles Salvia
I actually never used void like this before, but I used VC++ generator for the class and I thought what the hell, let's stick with this. so.. solved :)
Radagaisus
+4  A: 

Compiler thinks

Agent theAgent(void);

This to be a function declaration.

Agent theAgent;
BostonLogan
Be warned with the second option however, you will be calling an assignment operator.
DeusAduro
Actually copy constructor. But I removed it anyways, as I do not like this style.
BostonLogan
Copy constructor actually.
Cristián Romo
Actually neither of them (to my surprise). I posted this question, copy constructor is optimized away by NRVO
BostonLogan
+3  A: 

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
Cristián Romo
A: 

try

Agent theAgent = new Agent();
theAgent.loadSAG();

The error message looks like you never instantiated an Agent object before you tried to use it.

ChadNC
It is not a Java!
Mykola Golubyev
Mykonla: This is madness!Madness? THIS IS C++!!!!!!!!
Doug T.
This might work (and indeed will, since you're using the default copy constructor,) but *it will leak memory.* This is because you are assigning the contents of the `new Agent` (allocated on the heap) to `theAgent`. Since C++ isn't garbage collected and you aren't keeping a reference to the allocated memory to delete, this memory will not be reclaimed until the program terminates. Don't do it this way.
Cristián Romo
@Cristian. You are correct but that goes beyond the question he asked.@those who think that there is no new operator in C++ and decided to down vote an answer that was accepted by the person who asked the question. You're wrong.
ChadNC
@ChadNC: I didn't down vote because "I don't think there is a new operator in C++," I down voted because it's being used inappropriately. To get use the `new` operator without leaking memory, you need `theAgent` to be an `Agent*`, and all members would be accessed with the pointer to member operator `->`. This leaves the pointer accessible so that the memory can be `delete`d later.
Cristián Romo
@ChadNC: Never mind memory leaks, `theAgent` needs to be an `Agent*` just to get it to compile in C++.
Troubadour
@Troubadour: Hey, I missed that one. Good catch.
Cristián Romo
you know even if my question seemed like a beginner's one I can handle dynamic allocation.. it's just I haven't used c++ for a long long time and was quite comfortable with the leisure of javascript.
Radagaisus
I'm even more surprised then that you accepted an uncompilable and memory leaking answer when there are correct ones.
Georg Fritzsche
well, I got the point from the answer.. that was enough
Radagaisus