Thread: No default constructor available

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    23

    No default constructor available

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Point
    {
    	private:
    			int _x,_y;
    
    	public:
    			Point(int x,int y)
    			{
    				_x=x;
    				_y=y;
    			}	
    };
    
    
    class Shape 
    {
    	private:
    			char *colour;
    			Point _pos;
    	public:
    			Shape(char *p,int x,int y)
    			{
    				*colour=*p;
    				_pos = Point(x,y);
    			}
    };
    
    int main()
    {
    	Shape shape1("blue",10,20); 
    }

    Hi guys, I'm a newbie.. In the above code, I have two classes. Point and Shape. I wanted to initialize using this "Shape shape1("blue",10,20); ". But I'm not able to do so. The compiler shows this error. error C2512: 'Point' : no appropriate default constructor available

    Can someone explain to me why? Thank you!

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    You declared a constructor for the point class, so the compiler doesn't generate the default constructor for you.

    Code:
    Point _pos;
    Here you are telling it to use the default constructor.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    And by the way, symbols that start with underscore are reserved for implementation use. Don't use them.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    23
    Thanks for the prompt reply. I ran into another problem. I'm using inheritance now.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Point
    {
    	protected:
    			int _x,_y;
    
    	public:
    			Point(int x,int y)
    			{
    				_x=x;
    				_y=y;
    			}
    			int getx()
    			{
    				return _x;
    			}
    
    };
    
    
    class Shape : public Point 
    {
    	protected:
    			char *colour;
    			
    	public:
    		Shape(char *p,int x,int y) : Point(x,y)
    			{
    				*colour=*p;		
    			} 
    };
    
    int main()
    {
    	Shape shape1("blue",10,20); 
    	cout << shape1.getx() << endl;
    	return 0;
    }

    It compiles fine but when I run it, it says the program has stopped running and windows is finding for a solution to the problem. Is there anything wrong with the code?

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Yes, there is something wrong, but you will learn much more if you figure out what's wrong yourself, using a debugger. Have you tried that?

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    23
    Oh! Just tried that. Thanks cyberfish =) I changed the line "*colour=*p;" to "colour=p;". But I am not very sure why the former doesn't work and the latter works. can you explain pls

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    "colour" is a pointer to char, so is p.

    *p gives you the first character of "blue", so 'b'.

    *colour = *p; is the same as "*colour = 'b';"

    which doesn't work, because you never allocated space for colour! you are trying to store 'b' to a random place in memory. That usually doesn't work.

    "colour = p;" on the other hand sets colour to point to the same string p points to, and is perfectly fine.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    23

    Wink

    Ah Icic! Got another burning question. When I use this constructor, it works.

    Code:
                   Shape(char *p,int x,int y) : Point(x,y)
    	{
    		colour=p;		
    	}
    But when I use the following constructor, it doesn't work. Why is that and What's another equivalent version of this? I tried reading my lecture notes but it doesn't use any alternative versions.. only the constructor above.

    Code:
    	Shape(char *p,int x,int y)
    	{
    		Point(x,y);
    		colour=p;
    	}

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    In the first one you are saying you want the parent's "Point(x,y)" constructor to be executed before Shape's, instead of the default "Point()" constructor.

    Why do you need another version?

    In the second one you are making a function call to a non-existent Point() function which doesn't make much sense.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cyberfish
    In the second one you are making a function call to a non-existent Point() function which doesn't make much sense.
    More accurately, you are invoking a Point constructor to create a temporary Point object which is then destroyed.
    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

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    23
    Think you misunderstood my question. Haha.. I wanted to know whether there is another of saying the following. Another code which does the same thing as the constructor below in other words. Is there any? Tks =)


    Code:
         Shape(char *p,int x,int y) : Point(x,y)
    	{
    	      colour=p;		
    	}

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by 7heavens
    I wanted to know whether there is another of saying the following. Another code which does the same thing as the constructor below in other words. Is there any?
    Yes, since the member variables of Point are protected, you could assign to them in the Shape constructor. However, it would be bad practice to rely on this: assignment is not the same as initialisation, and member variables are usually declared private.

    In the Shape class, why is colour a pointer to char? It looks like it should be a std::string instead. If you really want to use a null terminated C-style string, then you should be aware of the implications of making colour a pointer to char. If you want a Shape to own its colour, then you need to do much more work. If you are satisfied with what you have now, then you are risking the possibility of the colour being destroyed before the Shape, which can lead to undefined behaviour.

    Furthermore, why is Shape derived from Point? Is a Shape really a Point? If you really think so, then it would be good to provide Point with a virtual destructor, in order to cater for the case where a Shape object (or other Point derived class object) is destroyed through a pointer to Point.
    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

  13. #13
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Quote Originally Posted by laserlight View Post
    More accurately, you are invoking a Point constructor to create a temporary Point object which is then destroyed.
    Ah, brainfart on my part. Thanks!

  14. #14
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Also, you cannot invoke the constructor "manually", if that's what you are asking, unlike in Java (but even in Java, "super" has to be the first statement, so it's kind of the same thing).

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you instead wish for Shape to contain a Point, you do that as follows:
    Code:
    class Shape {
        protected:
            std::string colour;
            Point pt;
        public:
            Shape(char *p, int x, int y) : colour(p), pt(x, y)
            {
            } 
    };
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. member as default argument
    By MarkZWEERS in forum C++ Programming
    Replies: 2
    Last Post: 03-23-2009, 08:09 AM
  2. Utilizing another compiled program for a task.
    By kotoroshinoto in forum C Programming
    Replies: 6
    Last Post: 06-03-2008, 01:43 PM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  5. Switching Default Buttons :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 07-02-2002, 04:08 PM