Thread: Please Explain This 'Constructor'

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    139

    Please Explain This 'Constructor'

    Hello and thank you for reading. To be honest I have never seen a constructor like this (with a colon)

    Code:
    #ifndef CSERVER_H
    #define CSERVER_H
    
    
    #include <iostream>
    #include <boost/bind.hpp>
    #include <boost/asio.hpp>
    
    
    using boost::asio::ip::tcp;
    
    
    class CServer
    {
        public:
            CServer(boost::asio::io_context& io_context, short port)  // this is the line with the error
        : io_context_(io_context),
          acceptor_(io_context, tcp::endpoint(tcp::v4(), port));
            virtual ~CServer();
    
    
        protected:
    
    
        private:
            boost::asio::io_context& io_context_;
            //tcp::acceptor acceptor_;
    };
    
    
    #endif // CSERVER_H


    Code:
    #include "CServer.h"
    
    
    CServer::CServer(boost::asio::io_context& io_context, short port)
        : io_context_(io_context),
          acceptor_(io_context, tcp::endpoint(tcp::v4(), port))
    {
    
    
      {
        start_accept();
      }
    }
    
    
    CServer::~CServer()
    {
        //dtor
    }
    error
    Code:
    include/CServer.h|13|error: expected ‘)’ before ‘&’ token|

    My question is, what does a : do in a constructor and how can I rewrite this code so that it removes the :

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by EverydayDiesel
    My question is, what does a : do in a constructor and how can I rewrite this code so that it removes the :
    It marks the start of the initialiser list in the constructor. You shouldn't remove it, although for this constructor it looks incorrectly written as the constructor body was omitted, i.e., the semi-colon at the end should have been replaced by a pair of braces. However, it seems that the constructor was intended to be defined in the source file instead, which means that the initialiser list in the declaration of the constructor within the class definition should be removed, i.e., the declaration would just declare the parameters as per normal.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    139
    thank you for taking the time to reply.

    I think I got it converted? But why am I getting the three errors?

    // CServer.h
    Code:
    #ifndef CSERVER_H
    #define CSERVER_H
    
    
    #include <iostream>
    #include <boost/bind.hpp>
    #include <boost/asio.hpp>
    
    
    using boost::asio::ip::tcp;
    
    
    class CServer
    {
        public:
            CServer(boost::asio::io_context& ioc, short port);
            virtual ~CServer();
    
    
        protected:
    
    
        private:
            boost::asio::io_context& m_io_context;
            tcp::acceptor m_acceptor;
    };
    
    
    #endif // CSERVER_H
    // CServer.cpp
    Code:
    #include "CServer.h"
    
    
    CServer::CServer(boost::asio::io_context ioc, short port)
    {
        io_context_(io_context),
        acceptor_(io_context, tcp::endpoint(tcp::v4(), port))
        start_accept();
    }
    
    
    CServer::~CServer()
    {
        //dtor
    }

    Code:
    include/CServer.h|13|error: expected ‘)’ before ‘&’ token|
    include/CServer.h|19|error: ‘io_context’ in namespace ‘boost::asio’ does not name a type|
    CServer.cpp|3|error: expected constructor, destructor, or type conversion before ‘(’ token|
    ||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

  4. #4
    Registered User
    Join Date
    Jan 2014
    Posts
    139
    thanks for helping me, it was a boost version mis-match

    have a good day!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is wrong:
    Code:
    CServer::CServer(boost::asio::io_context ioc, short port)
    {
        io_context_(io_context),
        acceptor_(io_context, tcp::endpoint(tcp::v4(), port))
        start_accept();
    }
    It should be:
    Code:
    CServer::CServer(boost::asio::io_context ioc, short port)
        : io_context_(ioc), acceptor_(ioc, tcp::endpoint(tcp::v4(), port))
    {
        start_accept();
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2014
    Posts
    139
    do they not do the same thing? sorry for the dumb question, I am just trying to learn.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by EverydayDiesel View Post
    do they not do the same thing? sorry for the dumb question, I am just trying to learn.
    The initializers go before the constructor body, not inside.

  8. #8
    Guest
    Guest
    Your example doesn't compile, does it? So why are you asking whether they do the same thing? Clearly one of them is wrong.

    Please have a look at member initializer lists, and try to write a basic example (for yourself) to confirm you have understood them. There's little use in guessing one's way to a solution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-11-2017, 09:26 AM
  2. Replies: 2
    Last Post: 06-23-2012, 10:47 PM
  3. Replies: 5
    Last Post: 02-21-2011, 02:19 AM
  4. Specialized Constructor call Default Constructor
    By threahdead in forum C++ Programming
    Replies: 15
    Last Post: 08-23-2010, 03:39 PM
  5. Replies: 10
    Last Post: 06-02-2008, 08:09 AM

Tags for this Thread