Problem constructing a class with a nested struc

This is a discussion on Problem constructing a class with a nested struc within the C++ Programming forums, part of the General Programming Boards category; I am trying to pass a nested struct type as an argument for the class constructor, and use an initializer ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    21

    Problem constructing a class with a nested struc

    I am trying to pass a nested struct type as an argument for the class constructor, and use an initializer list to invoke the implicit copy constructor for the struct to do all the work. But it's somehow not working.

    This is my header file

    Code:
    #ifndef _OBJECT_HPP_
    #define _OBJECT_HPP_
    
    class Object
    {
    public:
    
    	// Nested structure to represent a point and its corresponding operations
    	struct Point {
    		float x, y;
    
    		// Struct constructor
    		Point ( float X = 0, float Y = 0 );
    
    		// Way to set the point
    		void Set ( float X, float Y );
    
    		// The origin
    		static const Point ORIGIN;
    	};
    	
    	// Constructor
    	Object ( const Point & center );
    
    	// Function for viewing and setting the center of the object
    	Point & Center ();
    
    	// Function for returning the area, a pure virtual function
    	virtual float Area() const = 0;
    
    	// Drawing function
    	virtual void Draw ();
    	
    	// Destructor
    	virtual ~Object();
    
    
    private:
    	Point center;
    };
    #endif
    and this is my implementation of the class
    Code:
    #include "Object.hpp"
    
    const Object::Point Object::Point::ORIGIN = Object::Point();
    
    // Point constructor
    Object::Point::Point ( float X, float Y )
    {
    	x = 0;
    	y = 0;
    }
    
    // Set function
    void Object::Point::Set ( float X, float Y)
    {
    	x = X;
    	y = Y;
    }
    
    // Object constructor (default and non-default)
    // Use implicit copy constructor
    Object::Object ( const Point & center ) : Point(center)
    {
    }
    
    // Function for returning the area, a pure virtual function
    float Object::Area() const
    {
    }
    
    // Drawing function
    void Object::Draw ()
    {
    }
    	
    // Destructor
    Object::~Object()
    {
    }
    The red part is where I am having problem, when I tried to compile I got this message
    Code:
    error C2436: 'Point' : member function or nested class in constructor initializer list
    Does a struct come with a default copy constructor or do I need to define it separately?
    Please advice, thanks.
    Last edited by pliang; 04-14-2005 at 08:34 PM.

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    1st there is no Point object in the global scope to pass to object constructor, you would have to do Object::Object ( const Object::Point & center ) : Point(center), However Point is not a member variable, it is a type so you need to do this:
    Code:
    Object::Object ( const Object::Point & Center ) : center(Center) // I capitalize the C so you could differentiate 
    {
    ...
    }
    One last note: you really need to name you variables better, a suggest for the member variable would be to prefix an m like m_center, m_x, m_y, especially when you want to reuse them in functions and contructors of your class.
    Last edited by Darryl; 04-14-2005 at 08:39 PM.

  3. #3
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    The problem is that you're using the name of the struct in the initializer list instead of the name of an object. Instead of Point(center) which implies you want to instantiate a temporary Point object passing center as an arg, you should say center(c) // using c instead of "center" as the constructor's argument.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    21
    I figured out the problem, but thanks you all for the advice

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 03:46 PM
  2. Inheritance using Stack Class Problem
    By dld333 in forum C++ Programming
    Replies: 17
    Last Post: 12-06-2005, 11:14 PM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 05:24 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM

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