I have an issue with my code, in that I can set member variables, but only in the constructor. I can compile my code, however, it causes an access violation when I try to set a member variable set in the header file. I can get around this by declaring a variable within the cpp file, but that kind of ruins the point of using classes. The class I have problems with is derived from my base class, which is derived from wxAppConsole.
My derived class header:
Code: Select all
#pragma once
// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
//#include <iostream>
#ifdef __BORLANDC__
# pragma hdrstop
#endif
// for all others, include the necessary headers
#ifndef WX_PRECOMP
# include "wx/wx.h"
#endif
#include <wx/socket.h>
#include "TestApplication.h"
class Server : public TestApplication
{
friend class ProxyTestSuite; // for test rig
public:
Server(void);
~Server(void);
void OnServerEvent(wxSocketEvent& event);
void SetServerEvent(int setting);
private:
wxSocketServer* socServer_;
int serverEventResult_; //for test rig
DECLARE_EVENT_TABLE()
};
And cpp:
Code: Select all
#include "Server.h"
#include "wx/cmdline.h"
#include <iostream>
enum
{
// id for sockets
SERVER_ID = 100,
SOCKET_ID
};
BEGIN_EVENT_TABLE(Server, TestApplication)
EVT_SOCKET(SERVER_ID, Server::OnServerEvent)
END_EVENT_TABLE()
Server::Server(void)
{
// Create the address - defaults to localhost:0 initially
wxIPV4address addr;
addr.Service(3000);
// Create the server socket
socServer_ = new wxSocketServer(addr);
// Setup the event handler and subscribe to connection events
socServer_->SetEventHandler(*this, SERVER_ID);
socServer_->SetNotify(wxSOCKET_CONNECTION_FLAG);
socServer_->Notify(true);
serverEventResult_ =0; //this works
}
Server::~Server(void)
{
}
void Server::OnServerEvent(wxSocketEvent& event)
{
switch(event.GetSocketEvent())
{
case wxSOCKET_CONNECTION : /*serverEventResult_=1;*/ std::cout << "\nServer: wxSOCKET_CONNECTION\n"; break;
default : /*serverEventResult_=0;*/ std::cout << "\nServer: Unexpected event!\n"; break;
}
SetServerEvent(1); //access violation
}
void Server::SetServerEvent(int setting)
{
serverEventResult_ = setting; //access violation
}
Should I be getting the application instance or something because as far as I can see setting the member variable in that way is normal. Normally it would be set in the switch statement in OnServerEvent, but I'm trying to just set it for now, without much luck.
Regards,
Zobbo