No default constructor available

This is a discussion on No default constructor available within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <string> using namespace std; class Point { private: int _x,_y; public: Point(int x,int y) { _x=x; ...

  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,167
    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,167
    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,167
    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,167
    "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,167
    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
    21,431
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,431
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,167
    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,167
    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,295
    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, 04: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

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