简体   繁体   English

不允许使用抽象类类型“ Connection”的对象

[英]object of abstract class type “Connection” is not allowed

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; 当我尝试使用虚拟void OnConnected()= 0时; it gives me this stupid error idk whats wrong!!! 它给了我这个愚蠢的错误idk怎么了!!!

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. 您尚未提供OnReceived的定义,因此它是纯虚拟(抽象)方法,而该类是抽象类。 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). 要使用OnReceived方法,您必须为其提供一个实现(此刻它做什么?什么都没有)。 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; ). 编辑:由于上述原因, new Connection(io_service)不起作用:您无法创建具有纯虚函数(那些以= 0;结尾的声明)的类的对象。 You need to subclass Connection and provide implementations for those methods (like OnConnected ). 您需要继承Connection子类,并提供这些方法的实现(例如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. 如果仍然看不到错误,建议您参考一些有关C ++中面向对象的材料,尤其是虚拟方法和纯虚拟方法。

You have decided to make Connection abstract, but then attempted to instantiate it. 您已经决定将Connection抽象化,但是尝试实例化它。 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. 您不能创建抽象类的对象,因为并非所有功能都已实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM