-
November 3rd, 2013, 08:28 PM
#1
Object Composition
Hi I am learning C++, and trying for 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<iostream>
#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)
I am sure there is some mistake in passing reference, which I cannot figure it out.
-
November 4th, 2013, 02:41 AM
#2
Re: Object Composition
Originally Posted by deathmetal
Hi I am learning C++, and trying for object composition.
So why did you not post in the C++ forum?
Code:
class Point
{
public:
/** Default constructor */
Point(int x, int y);
Point();
...
The first is not the default constructor. The default constructor is a constructor that can be called without arguments. So, it either has no arguments or all argument must have a default value. The second constructor above is a default constructor of Point.
In the constructor of your Creature class, you create an instance of Point (the member loc) without passing any arguments to the constructor. Hence, the default constructor is called, only you did not implement it. You only declared it in your class definition. That's why you get the linker error. To solve it, either implement the default c'tor of Point or use the initializer list in the Creature c'tor to initialize loc with values for x and y (and remove the declaration of the default c'tor of Point).
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
November 5th, 2013, 01:51 AM
#3
Re: Object Composition
Apologies for the inconvenience caused.
Originally Posted by D_Drmmr
So why did you not post in the C++ forum?
Thank you for the help.
I have now added default constructor.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|