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

Abanoub
  • 3,623
  • 16
  • 66
  • 104
  • might be worth getting a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) if you do not have one already. – Sam Miller May 22 '11 at 14:51

3 Answers3

4

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.

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
  • @Christian Rau thnx but in my old class which i have posted now was working right , i dont understand what am missing here!! – Abanoub May 22 '11 at 23:47
  • @MixedCoder It shouldn't have worked with this class either as this is also an abstract class. – Christian Rau May 23 '11 at 00:12
  • @Christian Rau it workes fine with the old class, besides how do i use pure virtual method with the connection class coz really i cant get it , i already know the virtual and pure virtual methods, but i cant get this class to work , can u give an exampl or something , thnaks for your time and your help – Abanoub May 23 '11 at 01:31
  • @MixedCoder Just read about pure virtual methods. They essentially are virtual methods without an implementation. They are just there to tell, that subclasses have such a function. But the actual subclasses are responsible for providing their implementations. For the fifth time: You cannot create an object of a class that has pure virtual methods (which you do with your `new`). You have to create an object of a concrete subclass derived from `Connection`, which implements the pure virtual methods (so they aren't pure anymore). If you still don't get it, look at the last sentence of my answer. – Christian Rau May 23 '11 at 01:36
  • @Christian Rau ah ah i got it yeah right sorry for wasting your time :) and thanx i appreciate it – Abanoub May 23 '11 at 01:41
  • @Christian: Virtual member functions are **not** functions "without an implementation". Virtual member functions may have an implementation; they merely don't _have_ to, because: the _ONLY_ thing "magical" about virtual member functions is that they prevent their encapsulating class from being instantiable. – Lightness Races in Orbit May 23 '11 at 07:12
  • @Tomalak First, please speak of pure virtual (abstract) member functions, **like I did**. I know, that even pure virtual member functions may have an implementation, but that's a rare and more convenience case, as they cannot be called by anybody directly, except for a subclass. I did not want to confuse him even more, as usually they just don't have an implementation. – Christian Rau May 23 '11 at 11:34
  • @Christian: [They can _absolutely_ be called directly](http://codepad.org/8mQ1wrlu). If you didn't want to confuse the OP, you should not speak untruths to him/her. [EDIT: I did in fact mean to say "pure virtual member functions" in my previous comment, and had accidentally forgotten to say "pure". Sorry about that.] – Lightness Races in Orbit May 23 '11 at 11:42
  • @Tomalak Ok, I didn't consider `b.A::foo()` as directly. You are right in terms of terminology. Effectively they don't have an implementation, perhaps I should have chosen correcter words (followed by >3 lines explaining what I mean with those words and therefore confusing the asker). – Christian Rau May 23 '11 at 12:10
  • @Christian: Well given my earlier typo (w.r.t. omitting "pure"), I think we're both as guilty as each other. :) And in fairness the `= 0` syntax for declaring pure virtual member functions has _always_ been confusing; it _looks_ like there can be no implementation (as if there were some function pointer set to `0`, almost), whereas of course that's not the case. The syntax merely sets a property on the class. – Lightness Races in Orbit May 23 '11 at 16:24
2

You have decided to make Connection abstract, but then attempted to instantiate it. Which did you mean to do?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

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.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203