class Connection
{
public:
typedef boost::shared_ptr<Connection> pointer;
static pointer create(boost::asio::io_service& io_service){return pointer(new Connection(io_service));}
explicit Connection(boost::asio::io_service& io_service);
virtual ~Connection();
boost::asio::ip::tcp::socket& socket();
-->>>virtual void OnConnected()=0;
void Send(uint8_t* buffer, int length);
bool Receive();
private:
void handler(const boost::system::error_code& error, std::size_t bytes_transferred );
boost::asio::ip::tcp::socket socket_;
};
when am trying to use virtual void OnConnected()=0; it gives me this stupid error idk whats wrong!!!
1 IntelliSense: object of abstract class type "Connection" is not allowed: d:\c++\ugs\accountserver\connection.h 17
开发者_运维知识库whats wrong and how can i fix it while in my old connection class it was working good!!
class Connection
{
public:
explicit Connection(int socket);
virtual ~Connection();
virtual void OnConnected() =0;
virtual int Send(uint8_t* buffer, int length);
bool Receive();
int getSocket() const;
void Disconnect();
protected:
virtual void OnReceived(uint8_t* buffer, int len) = 0;
private:
int m_socket;
bool disconnecting;
};
so what am missing here!!
You have not provided a definition for OnReceived
, it is therefore a pure virtual (abstract) method and the class an abstract class. You cannot instantiate an object of an abstract class. To use the method OnReceived
you have to, well, provide an implementation for it (what does it do at the moment? nothing). Abstract classes are intended to be subclassed by concrete implementations which then provide implementations for the pure virtual methods.
EDIT: The part new Connection(io_service)
does not work for the above mentioned reason: you cannot create an object of a class, that has pure virtual functions (those declarations ending with = 0;
). You need to subclass Connection
and provide implementations for those methods (like OnConnected
). Your old class didn't have that problem. It had pure virtual methods, but you have not tried to instantiate it. If you still don't see the error, I suggest you to consult some material on object-orientation in C++, especially virtual and pure virtual methods.
You have decided to make Connection
abstract, but then attempted to instantiate it. Which did you mean to do?
You an use it, but not until it is implemented in some derived class.
You cannot create objects of abstract classes, because not all functions are implemented.
精彩评论