expression must be an lvalue or function designator

Nov 30, 2018 at 7:47pm

FacadeSocket::Con returns a Connection object and

FacadeSocket::Con.getSocket()returns a SOCKET.

Basically, I am trying create a reference to a Socket object which belongs
to FacadeSocket::Con to pass it to the Connect.Connect function .

For the line:

SOCKET & hs = &(FacadeSocket::Con.getSocket());


I am getting this error:
expression must be an lvalue or function designator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

void FacadeSocket::StartConnection(Connection Connect)
{

	
	//SOCKET * h = new SOCKET((FacadeSocket::Con.getSocket())); 

	SOCKET & hs = &(FacadeSocket::Con.getSocket());

	sockaddr_in address = FacadeSocket::Con.getServerAddress();
	sockaddr_in & addr = address; 


	Connect.Connect( hs , addr) ;

	//delete h; 

}



Nov 30, 2018 at 8:02pm
i have also tried it this way:

SOCKET & hs = FacadeSocket::Con.getSocket();

but I am getting initial value of non const must be an lvalue
Nov 30, 2018 at 8:29pm
A reference is basically a disguised pointer, so you are trying to take the address of a temporary (returned) value. Try it as a plain SOCKET, without the &.
Nov 30, 2018 at 10:20pm
thanks for the reply tbp.

I have tried it without the reference:

Connect.Connect(FacadeSocket::Con.getSocket(), FacadeSocket::Con.getServerAddress() ) ;

however FacadeSocket::Con.getSocket() will just make a copy of hSocket which becomes a very large unitiliazed value and pass it into the Connect function which is not what i want :

I have also tried to rewrite the function hSocket so that it returns a reference to a Socket, but that did not work either :

SOCKET & Connection::getSocket()
{
return Connection::hSocket;
}

Nov 30, 2018 at 11:09pm
If you can provide a zip of the whole thing I'll take a look at it.
Nov 30, 2018 at 11:25pm
SOCKET & hs?

What does that mean?

Perhaps you intended SOCKET *hs?
Dec 1, 2018 at 4:48pm

SOCKET & hs is a reference to a SOCKET object.


i have also tried it this way:

SOCKET & hs = FacadeSocket::Con.getSocket();

but I am getting initial value of non const must be an lvalue
Dec 1, 2018 at 4:58pm
I suspect there are a number of problems in your program, but if you refuse to post it I can't help.
Dec 1, 2018 at 5:05pm
I am just curious to as why I cannot define a reference this way:

SOCKET & hs = FacadeSocket::Con.getSocket();

I am getting initial value of non const must be an lvalue
Dec 1, 2018 at 6:39pm
I am just curious to as why I cannot define a reference this way

This (somewhat lengthy) thread contains an explanation:
http://www.cplusplus.com/forum/beginner/222328/
The relevant discussion starts around the middle of the first page.

The rough tl;dr is: FacadeSocket::Con.getSocket() is a "temporary object", and we can't get an alias to that temporary object because it is about to be destroyed.
We can make the "temporary object" last longer (i.e., have its lifetime extended) by "copying" it, by taking a const lvalue reference to it, or by taking a rvalue reference to it.

If you've never heard of lvalues or rvalues (which are value categories) and are interested in the problems they solve, read this famous (slightly dated) article by Thomas Becker:
http://thbecker.net/articles/rvalue_references/section_01.html
Last edited on Dec 1, 2018 at 7:59pm
Dec 1, 2018 at 7:19pm
Does this look like it should work to you? It's basically the same problem.

1
2
int func() { return 42; }
int main() { int &ref = func(); }

Topic archived. No new replies allowed.