c++ classes

This is a discussion on c++ classes within the Linux Programming forums, part of the Platform Specific Boards category; I took c++ a long time ago in school, now I am trying to relearn everything again. I think this ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    8

    c++ classes

    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:

    Code:
    //test.cpp
    
    #include "point.h"
    
    using namespace std;
    
    int main()
    {
    
      Point pt1;
    
      pt1.set_x(1234);
      pt1.print_x();
    
    }
    ----------------------------------------------------------------

    Code:
    //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
    ------------------------------------------------------------------

    Code:
    //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

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    #include "Point.h"

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

    You have it backwards

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    8
    I tried that, but this is the error whenever I compile that way:

    Code:
    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.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by figa
    I tried that, but this is the error whenever I compile that way:

    Code:
    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
    Last edited by Xipher; 03-02-2005 at 11:40 AM.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    8
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 04:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21