Pairs & Constructors

This is a discussion on Pairs & Constructors within the C++ Programming forums, part of the General Programming Boards category; Code: class MyClass{ public: MyClass(int MyInt,char MyChar){i=MyInt;c=MyChar;} ~MyClass(){}; private: int i; char c; }; MyClass doesn't have an explicit default ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    15

    Pairs & Constructors

    Code:
    class MyClass{
    	public:
    		MyClass(int MyInt,char MyChar){i=MyInt;c=MyChar;}
    		~MyClass(){};
    	private:
    		int i;
    		char c;
    };
    MyClass doesn't have an explicit default constructor. Say I want to create a pair of MyClasses, e.g. pair<MyClass,MyClass> tmp;, I (understandably) get the message,
    Code:
    error: no matching function for call to 'MyClass::MyClass()'
    However, I cannot figure out how to call my constructor if indeed I can in such a situation. Do I have to create a default constructor and initialise the values later? Or alternatively, create instances of MyClass that I bind together in a pair? e.g.
    Code:
    MyClass t1(1,'a'),t2(1,'a');
    pair<MyClass,MyClass>tmp(t1,t2);
    I guess my real question is: Is there no way of calling a constructor with arguments when using pair?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,639
    You might be able to use:
    Code:
    std::make_pair(MyClass(1, 'a'), MyClass(1, 'a'));
    But in your case perhaps this would suffice:
    Code:
    std::pair<MyClass, MyClass> tmp(MyClass(1, 'a'), MyClass(1, 'a'));
    Last edited by laserlight; 04-04-2006 at 09:17 AM.
    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
    Feb 2004
    Posts
    15
    The latter is exactly what I wanted. Thanks, laserlight!

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,815
    Quote Originally Posted by Nereus
    I guess my real question is: Is there no way of calling a constructor with arguments when using pair?
    You could make your constructor (the one with arguments) accept default values for those arguments, the constructor will then work as a default constructor:
    Code:
    class MyClass{
        public:
            MyClass(int MyInt = 1,char MyChar = 'a'){i=MyInt;c=MyChar;}
            ~MyClass(){};
        private:
            int i;
            char c;
    };
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem aligning floating point numbers
    By esbo in forum C Programming
    Replies: 4
    Last Post: 01-05-2009, 07:09 PM
  2. Copy constructors; Best practices (and private)
    By Mario F. in forum C++ Programming
    Replies: 15
    Last Post: 06-23-2006, 04:42 PM
  3. Arrays and Constructors
    By Verdagon in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2005, 07:20 PM
  4. constructors, arrays, and new
    By Thantos in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2004, 06:21 PM
  5. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 09:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21