Thread: An error saying 'no matching function call'; even when I declared the function

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Why is the call to the other constructor needed?

    Here is the code....at a very nascent stage.
    Code:
    class net_device
    {
        
        protected:
        bool send(net_device&,packet);
        std::deque<pack_dev_map> send_buffer;
        std::deque<pack_dev_map> recv_buffer;
        public:
        static bool connect(net_device,net_device);
    
    };
    //********/
    class packet
    {
        private:
        std::string main_data;
        //packet headers go here..
        public:
        std::string get_main_data();
        packet(std::string);
        //packet(packet&)
    };
    packet::packet(std::string data_arg)
    {
        main_data = data_arg;
    }
    
    
    std::string packet::get_main_data()
    {
        return main_data;
    }
    //*********/
    class pack_dev_map //packet-device sets...did not use std::map
    {
        public:
        packet p;
        net_device* n;
        pack_dev_map(packet&,net_device&);
    };
    
    pack_dev_map::pack_dev_map(packet& p_arg,net_device& n_arg)
    {
        p = p_arg;
        n = &n_arg;
    }
    The bold lines show an error that packet:: packet() is not defined. It is not, but why does it need it ?

    [also..to the moderators who may see this, I can't find a way to change the - now invalid- title of the post, can you do that?]
    Last edited by manasij7479; 07-08-2011 at 09:38 AM.

  2. #2
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Because you aren't using initialization lists. From a quick google (the first question): C++ Q&A: Initializing C++ Class Members and Incorporating the Places Bar in your MFC Apps

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    The article says that the member objects must gave a way to be initialized; I'm sending references of objects of those types for that purpose.
    Shouldn't the default copy constructor and/0r the default assignment operator do the same job here ?

    In other words:: Why must I use

    pack_dev_map::pack_dev_map(packet& p_arg,net_device& n_arg):p(p_arg),n(&n_arg){}
    ?
    Last edited by manasij7479; 07-08-2011 at 10:20 AM.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Creating a non default constructor will remove the default constructor.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Why do I need the default constructor here?
    Isn't the default copy constructor/assignment operator enough for initializing ?

    I don't want to keep a default constructor because there wouldn't be any default objects in the program.
    Last edited by manasij7479; 07-08-2011 at 10:27 AM.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    The string constructor is enough if you use and initializer list, yes. But If you don't then the packet needs to be default-constructed before the body of the constructor is called.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    the packet needs to be default-constructed before the body of the constructor is called.
    Thank you..
    That was what I was having problems to come in terms with..!
    Anyway, is there a reason for that ?

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    In order to use a variable in the constructor, such as by assigning to it, it must be a valid object. That means it must already be constructed somehow. Lacking an initializer list, a no argument constructor is the only way.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This might also be of some use to you: SourceForge.net: Do not remove parameter names - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No Matching Function Call for Constructor
    By manasij7479 in forum C++ Programming
    Replies: 5
    Last Post: 02-07-2011, 03:29 PM
  2. Replies: 2
    Last Post: 02-27-2009, 12:46 PM
  3. No Matching Function Error When Opening File
    By zephyrcat in forum C++ Programming
    Replies: 19
    Last Post: 07-15-2008, 01:36 PM
  4. Replies: 3
    Last Post: 10-12-2006, 06:58 AM
  5. no matching function for call to
    By f6ff in forum C++ Programming
    Replies: 4
    Last Post: 06-10-2006, 03:34 PM