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

Dani AI

Generated

The compiler error means something in your code is trying to create a Connection object, but Connection is abstract (it has at least one pure virtual method). is correct: you cannot instantiate an abstract class. The usual causes and fixes are:

  • Either implement the pure virtual method(s) so Connection is no longer abstract, or
  • Instantiate a concrete subclass that implements the pure virtual(s) instead of trying to new a Connection.

A common, clean fix is to move the factory into a concrete subclass. Example pattern (replace names to match your project):

struct TcpConnection : Connection {
    TcpConnection(boost::asio::io_service& svc) : Connection(svc) {}
    void OnConnected() override { /* start reads/handshake */ }
    static pointer create(boost::asio::io_service& svc) {
        return boost::make_shared<TcpConnection>(svc);
    }
};

Then call TcpConnection::create(...) instead of trying to construct Connection directly.

If you really want a base-level factory that can produce different concrete types, make it templated instead of static/virtual (static functions cannot be virtual):

template<typename Concrete>
static pointer create(boost::asio::io_service& svc) {
    return boost::make_shared<Concrete>(svc);
}

Usage: auto c = Connection::create<MyConnType>(io_service);

Quick troubleshooting checklist:

  • Search for every new Connection( or any stack declaration Connection x(...) — those are the instantiation sites that trigger this error.
  • Make sure no other pure virtuals are left unimplemented in derived classes.
  • If IntelliSense shows the error but the compiler doesn’t, do a full rebuild (IntelliSense can lag).
  • Consider making the base constructor protected if you intend it to be abstract so accidental instantiation is harder.

This explains why the factory in your first snippet fails: it attempts to construct an abstract type. Implement the concrete OnConnected() (or instantiate a concrete class) and the error will go away.

The error message says it all. You have made the function OnConnected() a pure virtual function which means that it has no implementation in the class Connection, which, in turn, forces you to provide a class, derived from Connection, that implements this function. In short, this pure virtual function makes the Connection class an "Abstract Class" and an object of that class cannot be created. You need to create a derived class, with an implementation for OnConnected(), and create an object of that class instead. In other words, you need to revise your implementation of your factory function create() which creates an object of Abstract class Connection. The second version succeeds only because you don't have that erroneous factory function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.