"Error" [Linker error] undefined reference to `point::point(double, double)'

Mar 17, 2012 at 6:00am
hay I am using dev c++ and I am having these errors.
[Linker error] undefined reference to `point::point(double, double)'
[Linker error] undefined reference to `point::tostring() const'
[Linker error] undefined reference to `point::point(double, double)'
[Linker error] undefined reference to `point::tostring() const'
[Linker error] undefined reference to `point::point(point const&)'
[Linker error] undefined reference to `point::tostring() const'
[Linker error] undefined reference to `point::operator=(point const&)'
[Linker error] undefined reference to `point::tostring() const'
[Linker error] undefined reference to `point::x() const'
[Linker error] undefined reference to `point::y() const'
[Linker error] undefined reference to `point::~point()'
[Linker error] undefined reference to `point::~point()'
[Linker error] undefined reference to `point::~point()'
[Linker error] undefined reference to `point::~point()'
[Linker error] undefined reference to `point::~point()'
[Linker error] undefined reference to `point::~point()'
32:2 C:\Dev-Cpp\include\c++\3.4.2\backward\backward_warning.h more undefined references to `point::~point()' follow
32:2 C:\Dev-Cpp\include\c++\3.4.2\backward\backward_warning.h ld returned 1 exit status





and my program is:

// program


#include<string.h>
using namespace std;
class point
{
public:
point(double=0.0,double=0.0);
point(const point&);
~point();
point& operator=(const point&);
double x() const;
double y() const;
string tostring() const;
protected:
double _x, _y;
};
int main()
{
point p0;
cout << "p0 = " << p0.tostring() << endl;
point p1(5,-2);
cout << "p1 = " << p1.tostring() << endl;
point p2=p1;
cout << "p2 = " << p2.tostring() << endl;
p0=p1;
cout << "p0 = " << p0.tostring() << endl;
cout << "p0.x() = " << p0.x() << endl;
cout << "p0.y() = " << p0.y() << endl;

system("pause");
}



// Please help me out of these errors.
// please donot refer me to any other post at all. I have read all of the post might be there would be answer in those posts but I am unable to understand that since I am a biggner. Please help me.
Mar 17, 2012 at 6:29am
Did you implement any of those methods of point?
You only have declarations here, so the compiler doesn't know what to do.
If you have implemented them in another file, you probably forgot to add it to your project.
Mar 17, 2012 at 6:38am
I did not do anything this is just a bigging of my data structure. This is first program and I am unable do finish with it. I donot know how to use it.
Mar 17, 2012 at 8:55am
Like I said, a declaration like double x() const; doesn't tell compiler what you want x() to do. You need to add an implementation double x() const { return _x; }. Do this for all of your methods.

tostring is a bit complicated, so I'll show that too:
1
2
3
4
5
string tostring() const {
  stringstream text;
  text << _x << ", " << _y;
  return text.str();
}
You need to include <sstream> for this.
Mar 17, 2012 at 9:21am
I really donot know what are you talking about.
Will you please do me a favour to write a perfect code so that I could understand it.
Mar 17, 2012 at 3:07pm
Don't expect me to do it for you. Perfect code:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

struct example{
  int method()//this is the method signature. It tells you how to call this method
  {//from here is the implementation. It says what the method actually does.
     return 5;
  }
};

int main() {
  example e;
  std::cout << e.method();
}
Mar 19, 2012 at 6:29am
dude its there you can see

int main()
{
point p0;
cout << "p0 = " << p0.tostring() << endl;


//what do you want me to do with this? Its already there.
Mar 19, 2012 at 6:36am
Your code in main is correct; you just need to add more code for your point class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class point
{
protected:
    double _x, _y;
public:
    point(double a = 0.0, double b = 0.0)
    {
        _x = a;
        _y = b;
    }

    double x() const
    {
        return _x;
    }

    double y() const
    {
        return _y;
    }

    string tostring() const
    {
        stringstream text;
        text << _x << ", " << _y;
        return text.str();
    }
};


Note that I left out the copy constructor, assignment operator, and destructor. The compiler will fill those in for you if you don't put them in the class declaration, but if you do put them there you have to write them yourself.
Mar 19, 2012 at 6:58am
I donot get it.
Mar 19, 2012 at 7:01am
make the code for your class!!! What is there to not get?
Mar 19, 2012 at 10:12am
not working
Mar 19, 2012 at 10:45am
See this line?
point(double=0.0,double=0.0);

This line in your code means "there is going to be a constructor function, which I will write, that will accept two parameters of type double". Where is that function?

point(const point&);
This line in your code means "there is going to be a constructor function, which I will write, that will accept a single parameters of type point". Where is that function?

~point();
This line in your code means "there is going to be a destructor function, which I will write". Where is that function?

point& operator=(const point&);
And this. Where is the function that you need to write?

double x() const;
And this. Where is the function that you need to write?

double y() const;
And this. Where is the function that you need to write?

string tostring() const;
And this. Where is the function that you need to write?


You created a class, and you said it had all these functions in it, but NONE OF THEM EXIST. C++ is not magic; you have to write functions yourself. If none of that makes any sense to you, classes are too advanced for you and you need to go back to the start.
Last edited on Mar 19, 2012 at 10:46am
Mar 19, 2012 at 11:32am
Yup! Classes are too advanced for him. Perhaps even function.
Mar 31, 2012 at 12:45pm
I am sorry it was just a start of me of classes. But now that's ok I got it. Thanks dudes.
Topic archived. No new replies allowed.