[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

What's wrong with my code?

Posted on 2005-04-24
8
Medium Priority
?
263 Views
Last Modified: 2010-04-01
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](int, 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_ostream<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
0
Comment
Question by:igor92128
8 Comments
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 13855499
I am running this in g++ and it runs fine

Amit
0
 
LVL 15

Expert Comment

by:efn
ID: 13855500
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.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13855501
appear to be that you are not linking correctly with Point's object file (maybe Point.o)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 200 total points
ID: 13855506
Also, I assume that the "point.h" and "point.cpp" are placed in proper directories
Also, if on an Operating system which case sensitive, then your files need to be defined that way

For example,
You code has
"Point.h"
and "point.h"
at two different instances


Amit
0
 

Author Comment

by:igor92128
ID: 13855540
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
0
 
LVL 15

Assisted Solution

by:efn
efn earned 400 total points
ID: 13855569
No, everything is not linked through the includes.  You need point.cpp on the command line.  Or you could compile it separately and specify point.o on the command line.  But #include directives do not get object modules linked in.
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 13855578
You need to compile point.cpp sepately, else include in copilation of main

Amit
0
 
LVL 22

Accepted Solution

by:
NovaDenizen earned 1400 total points
ID: 13855678
igor, you aren't compiling correctly.
Try this:
g++ -c main.cpp
g++ -c point.cpp
g++ -o point main.o point.o
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question