igor92128
asked on
What's wrong with my code?
Hello, I have some code here and every time I compile in g++, I get these errors:
/tmp/ccCHleJp.o(.text+0x29 ): In function `main':
: undefined reference to `Point::Point[in-charge](i nt, int)'
/tmp/ccCHleJp.o(.text+0x44 ): In function `main':
: undefined reference to `Point::setPoint(int, int)'
/tmp/ccCHleJp.o(.text+0x4f ): In function `main':
: undefined reference to `Point::getY()'
/tmp/ccCHleJp.o(.text+0x5c ): In function `main':
: undefined reference to `Point::getX()'
/tmp/ccCHleJp.o(.text+0xcf ): In function `main':
: undefined reference to `operator<<(std::basic_ost ream<char, std::char_traits<char> >&, Point const&)'
collect2: ld returned 1 exit status
Point.h:
#include <iostream>
using namespace std;
class Point
{
friend ostream &operator<<(ostream &, const Point &);
public:
Point(int = 0, int = 0);
void setPoint(int, int);
const int getX();
const int getY();
protected:
int x, y;
};
Point.cpp:
#include <iostream>
#include "point.h"
using namespace std;
Point::Point(int a, int b)
{
setPoint(a, b);
}
void Point::setPoint(int a, int b)
{
x = a;
y = b;
}
const int Point::getX()
{
return x;
}
const int Point::getY()
{
return y;
}
//output the point
ostream &operator<<(ostream &output, const Point &p)
{
output << "[" << p.x << ", " << p.y << "]";
return output;
}
main.cpp:
#include <iostream>
#include "point.h"
using namespace std;
int main()
{
Point p(25,50);
p.setPoint(25,25);
cout << "X coordinate is " << p.getX() << "\nY coordinate is " << p.getY() << endl;
cout << "\n\nThe new location of p is " << p << endl;
return 0;
}
Thanks,
Igor
/tmp/ccCHleJp.o(.text+0x29
: undefined reference to `Point::Point[in-charge](i
/tmp/ccCHleJp.o(.text+0x44
: undefined reference to `Point::setPoint(int, int)'
/tmp/ccCHleJp.o(.text+0x4f
: undefined reference to `Point::getY()'
/tmp/ccCHleJp.o(.text+0x5c
: undefined reference to `Point::getX()'
/tmp/ccCHleJp.o(.text+0xcf
: undefined reference to `operator<<(std::basic_ost
collect2: ld returned 1 exit status
Point.h:
#include <iostream>
using namespace std;
class Point
{
friend ostream &operator<<(ostream &, const Point &);
public:
Point(int = 0, int = 0);
void setPoint(int, int);
const int getX();
const int getY();
protected:
int x, y;
};
Point.cpp:
#include <iostream>
#include "point.h"
using namespace std;
Point::Point(int a, int b)
{
setPoint(a, b);
}
void Point::setPoint(int a, int b)
{
x = a;
y = b;
}
const int Point::getX()
{
return x;
}
const int Point::getY()
{
return y;
}
//output the point
ostream &operator<<(ostream &output, const Point &p)
{
output << "[" << p.x << ", " << p.y << "]";
return output;
}
main.cpp:
#include <iostream>
#include "point.h"
using namespace std;
int main()
{
Point p(25,50);
p.setPoint(25,25);
cout << "X coordinate is " << p.getX() << "\nY coordinate is " << p.getY() << endl;
cout << "\n\nThe new location of p is " << p << endl;
return 0;
}
Thanks,
Igor
Maybe you need to #include "Point.h" with a capital P. I compiled your program fine in Windows, but UNIX is pickier about the case of letters.
appear to be that you are not linking correctly with Point's object file (maybe Point.o)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Amit, what exact command did you use to compile in g++?
I just used 'g++ main.cpp -o point' which should work.
efn, I changed the filename and #includes to capital P, but I still get the same errors.
Acutally, 'g++ main.cpp point.cpp -o point' will get the program to compile, but since everything is linked through the includes, the command I tried the first time should have worked too, right?
Igor
I just used 'g++ main.cpp -o point' which should work.
efn, I changed the filename and #includes to capital P, but I still get the same errors.
Acutally, 'g++ main.cpp point.cpp -o point' will get the program to compile, but since everything is linked through the includes, the command I tried the first time should have worked too, right?
Igor
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You need to compile point.cpp sepately, else include in copilation of main
Amit
Amit
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Amit