Class constructor: no call to methods possible?

K

Karl Ebener

Hi!

Sorry, to bother you with so many questions in a row, but I am
struggling to get into C++ whilst having to complete a quite complex
program... :(

My (current) problem:

When I define a class with constructors like

class c_Message
{
public:
c_Message();
c_Message(string cnt);
....
<snip>
....
};

I am getting the problem, that whenever I instantiate with an argument

c_Message msg("Hallo");

I can use this object.

But when I instantiate

c_Message msg();

and later want to set the content of the message using a method, I get
the following error:

error: request for member `setContent' in `msg ', which is of
non-aggregate type `c_Message ()()'

I cannot use the classes methods, but when using an argument to
instantiate, I can.

What should I do?

Tnx again very much

Karl
 
M

Mike Wahler

Karl Ebener said:
Hi!

Sorry, to bother you with so many questions in a row, but I am
struggling to get into C++ whilst having to complete a quite complex
program... :(

My (current) problem:

When I define a class with constructors like

class c_Message
{
public:
c_Message();
c_Message(string cnt);
...
<snip>
...
};

I am getting the problem, that whenever I instantiate with an argument

c_Message msg("Hallo");

I can use this object.

But when I instantiate

c_Message msg();

This is not an instantiation. It's a function declaration.
(Declares a function named 'msg' which takes no arguments
and returns type 'c_Message').

If you want to create a default-constructed 'c_Message' object, write:

c_Message msg;
and later want to set the content of the message using a method, I get
the following error:

error: request for member `setContent' in `msg ', which is of
non-aggregate type `c_Message ()()'

I cannot use the classes methods, but when using an argument to
instantiate, I can.

What should I do?

See above.

-Mike
 
R

Rob Williscroft

Karl Ebener wrote in online.net in comp.lang.c++:
But when I instantiate

c_Message msg();

This is a function declaration, a function msg that return's
a c_Message and takes no arguments.

BTW it isn't a template so there is no "instantiation" going
on here, you are (attempting) to *define* an object msg of
type c_Message.

Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,528
Latest member
AnaHawley8

Latest Threads

Top