抽象类类型&QUOT对象;连接"不允许 [英] object of abstract class type "Connection" is not allowed

查看:203
本文介绍了抽象类类型&QUOT对象;连接"不允许的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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_;
};

当我尝试使用虚拟无效OnConnected()= 0;它给了我这个愚蠢的错误IDK的什么是错!

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!!

推荐答案

您没有为 OnReceived 提供的定义,因此,一个纯虚(抽象)方法类的抽象类。你不能实例化一个抽象类的一个对象。使用的方法 OnReceived 你,好了,为它的实现(这是什么的时刻吗?没有)。抽象类主要由具体实现,然后为纯虚方法的实现被继承。

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.

编辑:部分新建连接(io_service对象)不为上述工作的原因:你不能创建一个类的对象,有纯虚函数(那些 = 0结束的声明; )。你需要继承连接并提供这些方法的实现(如 OnConnected )。您老班没有这个问题。它有纯虚的方法,但你还没有尝试进行实例化。如果你还没有看到错误,我建议你向在C ++面向对象,尤其是虚拟和纯虚方法的一些材料。

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.

这篇关于抽象类类型&QUOT对象;连接&QUOT;不允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆