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_;
};
当我尝试使用 virtual void OnConnected()=0;它给了我这个愚蠢的错误 idk whats wrong!!!
1 IntelliSense: object of abstract class type "Connection" is not allowed: d:\c++\ugs\accountserver\connection.h 17
出了什么问题,在我的旧连接类中它运行良好时我该如何修复它!!
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;
};
所以这里缺少什么!!
最佳答案
您还没有为 OnReceived
提供定义,因此它是一个纯虚拟(抽象)方法,该类是一个抽象类。您不能实例化抽象类的对象。要使用 OnReceived
方法,您必须为它提供一个实现(它现在做什么?什么都没有)。抽象类旨在由具体实现子类化,然后为纯虚拟方法提供实现。
编辑 new Connection(io_service)
部分由于上述原因不起作用:您不能创建具有纯虚函数的类的对象(那些以 = 0;
结尾的声明)。您需要继承 Connection
并为这些方法提供实现(如 OnConnected
)。你以前的类(class)没有这个问题。它有纯虚方法,但您还没有尝试实例化它。如果您仍然没有看到错误,我建议您查阅一些有关 C++ 中面向对象的资料,尤其是虚方法和纯虚方法。
关于c++ - 不允许抽象类类型 "Connection"的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6088602/