Thread: object compostion

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    87

    object compostion

    Dear Forum Members,
    I am new to C++, and trying to learn object composition.

    I have 2 classes: Point and Creature.
    Point->int x and int y
    Creature:-> Point location and string name

    Below is my Point class header:
    Code:
    #ifndef POINT_H
    #define POINT_H
    
    class Point
    {
        public:
            /** Default constructor */
            Point(int x, int y);
    
            Point();
    
            void set_x(int);
            void set_y(int);
            int get_x();
            int get_y();
    
        protected:
    
        private:
        int x;
        int y;
    };
    
    #endif // POINT_H
    Below is my Point class cpp
    Code:
    #include "Point.h"
    
    Point::Point(int t, int h)
    {
        x=t;
        y=h;
        //ctor
    }
    void Point::set_x(int h){
        x=h;
    }
    
    void Point::set_y(int h){
        y=h;
    }
    
    int Point::get_x(){
        return x;
    }
    
    int Point::get_y(){
        return y;
    }
    My creature header file looks like:
    Code:
    #ifndef CREATURE_H
    #define CREATURE_H
    #include "Point.h"
    #include <string>
    
    class Creature
    {
        public:
            /** Default constructor */
            Creature(Point &location, std::string name);
            /** Access loc
             * \return The current value of loc
             */
            Point Getloc() { return loc; }
            /** Set loc
             * \param val New value to set
             */
            void Setloc(Point &val) { loc = val; }
            /** Access name
             * \return The current value of name
             */
            std::string Getname() { return name; }
            /** Set name
             * \param val New value to set
             */
            void Setname(std::string val) { name = val; }
        protected:
        private:
            Point loc; //!< Member variable "loc"
            std::string name; //!< Member variable "name"
    };
    
    #endif // CREATURE_H
    My creature cpp looks like this:

    Code:
    #include "Creature.h"
    #include "Point.h"
    
    Creature::Creature(Point &location, std::string n_name)
    {
        //ctor
    
        name=n_name;
        loc=location;
    
    }
    I need to have a creature object with its location. The location will be passed as an object of Point.
    I am getting error


    undefined reference to 'Point::Point()'
    at line:
    Code:
    Creature::Creature(Point &location, std::string n_name)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You forgot to define the default constructor for Point.

    By the way, in the Creature constructor, the Point reference parameter should probably be a const reference instead. Likewise, the std::string paramater should be a const reference. Also, you should be using the initialiser list in the constructor to initialise the member variables.
    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
    Oct 2013
    Posts
    87
    Default constructor was the issue? Why is it?
    Edited the code and yes, it works. Thank you...

    By constant and initialiser list, you mean: ?
    Code:
    Creature::Creature(const Point& location, std::string n_name)
    :loc(location),name(n_name)
    Quote Originally Posted by laserlight View Post
    You forgot to define the default constructor for Point.

    By the way, in the Creature constructor, the Point reference parameter should probably be a const reference instead. Likewise, the std::string paramater should be a const reference. Also, you should be using the initialiser list in the constructor to initialise the member variables.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes.
    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

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    87
    Great!
    Quote Originally Posted by laserlight View Post
    Yes.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by deathmetal View Post
    Default constructor was the issue? Why is it?
    Because it was declared in the header file, not defined (implemented) anywhere. The Creature constructor requires it (since a Creature's Point member is constructed before Creature's constructor is invoked).

    You correctly guessed laserlight's meaning with initialiser lists.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by laserlight View Post
    By the way, in the Creature constructor, the Point reference parameter should probably be a const reference instead. Likewise, the std::string paramater should be a const reference. Also, you should be using the initialiser list in the constructor to initialise the member variables.
    With Point it's six of one, half a dozen of the other, but with std::string, passing by value is good here in C++11. It's a sync parameter, and passing by value allows move semantics.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question: Determining object scope from root object
    By Imanuel in forum C++ Programming
    Replies: 6
    Last Post: 12-25-2012, 11:20 AM
  2. Trouble storing ifstream object in pair object
    By Programmer_P in forum C++ Programming
    Replies: 7
    Last Post: 01-17-2012, 01:20 AM
  3. What is the diffrence betwen aggregtion and compostion
    By jabka in forum C++ Programming
    Replies: 6
    Last Post: 04-24-2007, 12:45 PM
  4. Replies: 4
    Last Post: 11-14-2006, 11:52 AM

Tags for this Thread