PDA

View Full Version : c++ classes



figa
03-02-2005, 09:57 AM
I took c++ a long time ago in school, now I am trying to relearn everything again. I think this is a very stupid question but I don't care, here it goes:



//test.cpp

#include "point.h"

using namespace std;

int main()
{

Point pt1;

pt1.set_x(1234);
pt1.print_x();

}



----------------------------------------------------------------




//point.h

#ifndef POINT_H
#define POINT_H
#include "point.cpp"

class Point
{

public:
void set_x( int new_number );
void print_x();

private:
int x;

};


#endif



------------------------------------------------------------------




//point.cpp

#include <iostream>

void Point::set_x( int new_number)
{
x = new_number;
}


void Point::print_x()
{
cout << "\nThe value for x is : " << x << endl << endl;
}





Ok so that is my code, as you can see three (3) different files.
When I try to compile this is what happenes:

bash-3.00$ g++ test.cpp
In file included from point.h:3,
from test.cpp:1:
point.cpp:3: error: syntax error before `::' token
point.cpp:9: error: syntax error before `::' token
bash-3.00$

What am I doing wrong here. I have tried to do it many different ways, but nothing seems to work. Please, help!!

Thanks,
Figa

Perspective
03-02-2005, 11:01 AM
#include "Point.h"

add that to Point.cpp and remove #include "Point.cpp" from Point.h

You have it backwards

figa
03-02-2005, 12:00 PM
I tried that, but this is the error whenever I compile that way:



lab35->g++ test.cpp
/tmp/ccWqOfOU.o(.text+0x1d): In function `main':
: undefined reference to `Point::set_x(int)'
/tmp/ccWqOfOU.o(.text+0x2c): In function `main':
: undefined reference to `Point::print_x()'
collect2: ld returned 1 exit status
lab35->


The code itself seems to be right, cause when I put everything in the .h file, it compiles with no problems. It is when I move the functions to their own .cpp file that the prolems arise.

Thanks for the help.
Figa

PS: I am on a RH Linux pc, but I get the same errors on slackware and Ubuntu.

Xipher
03-02-2005, 12:34 PM
I tried that, but this is the error whenever I compile that way:



lab35->g++ test.cpp
/tmp/ccWqOfOU.o(.text+0x1d): In function `main':
: undefined reference to `Point::set_x(int)'
/tmp/ccWqOfOU.o(.text+0x2c): In function `main':
: undefined reference to `Point::print_x()'
collect2: ld returned 1 exit status
lab35->


The code itself seems to be right, cause when I put everything in the .h file, it compiles with no problems. It is when I move the functions to their own .cpp file that the prolems arise.

Thanks for the help.
Figa

PS: I am on a RH Linux pc, but I get the same errors on slackware and Ubuntu.


Once you make the edits Perspective mentioend, try using this command to compile the project

g++ test.cpp Point.cpp

and you should get a.out

when you have multiple files in a project (VERY common), each file that is required much be made into object code, and then linked together to get the resulting binary, another way to go about building (compiling) your project, would be in more then 1 command, but say, 3

g++ -c test.cpp -o test.o
g++ -c Point.cpp -o Point.o
g++ test.o Point.o

and you should again have a.out, and be the exact same. the -c compiles but doesn't LINK the code (linking is where your errors where comming from) the test.o and Point.o where object files. once you put the 2 together (the last command) the project will be linked (and the actuall c++ runtime for gcc is linked as well, as stuff like iostream, string and what not are part of it) and you should get the resulting a.out binary. also the -o tells the compiler what the output should be called, instead of getting a.out every time, you could instead use
g++ test.o Point.o -o test
and you would get a binary called test as output instead.

hopefully this helps, if so, would you mind adding to my rep :D

figa
03-02-2005, 12:49 PM
Oh cool!!. That is what I was doing wrong. and also I did not had the using namespace std in the file. Thank you both a lot.

Figa