error: request for member of non-class type

A

Andre

Hi all,

Can someone point out what is wrong with this tiny piece of code? I'm
compiling it with "g++ ./Main.cpp", and get the error message "error:
request for member 'konnect' in 'theSocket', which is of non-class
type 'Socket ()()'"

Thanks in advance,

Andre
#####################################
class Socket
{
public:
void konnect(){};
};

int main(int argc, char ** argv)
{
Socket theSocket();

theSocket.konnect();

return 0;
}
#####################################
 
K

Kai-Uwe Bux

Andre said:
Hi all,

Can someone point out what is wrong with this tiny piece of code? I'm
compiling it with "g++ ./Main.cpp", and get the error message "error:
request for member 'konnect' in 'theSocket', which is of non-class
type 'Socket ()()'"

The error message is surprisingly accurate.
Thanks in advance,

Andre
#####################################
class Socket
{
public:
void konnect(){};
};

int main(int argc, char ** argv)
{
Socket theSocket();

This declares theSocket as an uninitialized variable of function type:

Socket () ( void )


You might want to say

Socket theSocket;

instead.
theSocket.konnect();

A function has no members.
return 0;
}
#####################################


Best

Kai-Uwe Bux
 
J

Jim Langston

Andre said:
Hi all,

Can someone point out what is wrong with this tiny piece of code? I'm
compiling it with "g++ ./Main.cpp", and get the error message "error:
request for member 'konnect' in 'theSocket', which is of non-class
type 'Socket ()()'"

Thanks in advance,

Andre
#####################################
class Socket
{
public:
void konnect(){};
};

int main(int argc, char ** argv)
{
Socket theSocket();

Here you declare a function called "theSocket" taking no parameters and
returning an instance of Socket. Not what you wanted. Change this line to:
Socket theSocket;

and all should be well. In C++ if something can be interpreted as a
function prototype, it is.
 
J

James Kanze

Andre wrote:

[...]
This declares theSocket as an uninitialized variable of function type:

Variables of function type don't exist. This declares theSocket
as an external function. (I'm pretty sure that this is what you
meant, but talking about a variable of function type sounds
strange, at least to me.)
 
K

Kai-Uwe Bux

James said:
Andre wrote:
[...]
int main(int argc, char ** argv)
{
Socket theSocket();
This declares theSocket as an uninitialized variable of function type:

Variables of function type don't exist. This declares theSocket
as an external function.
Right.

(I'm pretty sure that this is what you
meant, but talking about a variable of function type sounds
strange, at least to me.)

Your interpretation is too charitable :)

I was just goofing off.


Thanks

Kai-Uwe Bux
 

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

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top