Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

(C++) Class function call from within switch not working, need help.

Cade JansenCade Jansen Member Posts: 16
Hello

Got me a little problem with a class that I'm using (or will hopefully use) to control socket/thread flow. For a reason which I cannot guess at, the function Sockets::SwSocks() isn't executing when I put it in my program.

I know to enclose the class function call within its own local brackets, like this

switch (sockState)
case 1:
// code goes here
break;
case 0:
// other code goes here
{Sockets SwSocks; }
break;
default:
std::cout << "Socket error!" << endl;
WSACleanup();
return(SOCKET_FAIL);

but it doesn't work. If I include cout << "##" 's for purposes of debugging, they only show directly before and after the Sockets SwSocks; call.

class Sockets
{
public:
Sockets(){};
Sockets(int){};
virtual void SwSocks();
int Trap();
~Sockets(){};
};

void Sockets::SwSocks()
{
//code to clean up previous work goes here
std::cout << "Sockets switching." << endl;
}

nothing in between like that "Sockets switching" message.

Am I doing something wrong?

Comments

  • VilanyeVilanye Member Posts: 684
    : Hello
    :
    : Got me a little problem with a class that I'm using (or will hopefully use) to control socket/thread flow. For a reason which I cannot guess at, the function Sockets::SwSocks() isn't executing when I put it in my program.
    :
    : I know to enclose the class function call within its own local brackets, like this
    :
    : switch (sockState)
    : case 1:
    : // code goes here
    : break;
    : case 0:
    : // other code goes here
    : {Sockets SwSocks; }
    : break;
    : default:
    : std::cout << "Socket error!" << endl;
    : WSACleanup();
    : return(SOCKET_FAIL);
    :
    : but it doesn't work. If I include cout << "##" 's for purposes of debugging, they only show directly before and after the Sockets SwSocks; call.
    :
    : class Sockets
    : {
    : public:
    : Sockets(){};
    : Sockets(int){};
    : virtual void SwSocks();
    : int Trap();
    : ~Sockets(){};
    : };
    :
    : void Sockets::SwSocks()
    : {
    : //code to clean up previous work goes here
    : std::cout << "Sockets switching." << endl;
    : }
    :
    : nothing in between like that "Sockets switching" message.
    :
    : Am I doing something wrong?
    :


    yes, you need to instantiate Sockets ie Sockets sock, then call SwSocks ie sock.SwSocks();

    Be wary of scoping issues, creating an object in a inner block, might cause so problems for you.
    [italic][blue]Just my 2 bits[/blue][italic]

  • Cade JansenCade Jansen Member Posts: 16
    : yes, you need to instantiate Sockets ie Sockets sock, then call SwSocks ie sock.SwSocks();
    :
    : Be wary of scoping issues, creating an object in a inner block, might cause so problems for you.

    Did that ... working now. Thanks. :)
Sign In or Register to comment.