LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ classes questions? (https://www.linuxquestions.org/questions/programming-9/c-classes-questions-295702/)

figadiablo 02-27-2005 11:06 PM

C++ classes questions?
 
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) 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!!

jonaskoelker 02-27-2005 11:18 PM

point.hh shouldn't include point.cc, but point.cc should include point.hh; to compile:
$ g++ test.cc point.cc

(use your favorite extensions instead of hh and cc if you care -- but emacs gets c++-headers right for .hh but not .h; just a hint)

Hope this helps,

Jonas

figadiablo 03-02-2005 09:07 AM

Hey,

that is not working. If I #include point.h in point.cpp i get different compile errors.

To make sure, I got some code, straight our of the book and typed it in. After compiling I got the same kind of errors. The code involves a time.cpp, time.h, and a time_test.cpp. when I compile this is what happens:

Code:

lab35->gcc time_test.cpp
/tmp/cco9tHMh.o(.text+0x18): In function `main':
: undefined reference to `Time::Time()'
/tmp/cco9tHMh.o(.text+0x28): In function `main':
: undefined reference to `std::cout'
/tmp/cco9tHMh.o(.text+0x2d): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/cco9tHMh.o(.text+0x3c): In function `main':
: undefined reference to `Time::printMilitary()'
/tmp/cco9tHMh.o(.text+0x4c): In function `main':
: undefined reference to `std::cout'
/tmp/cco9tHMh.o(.text+0x51): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/cco9tHMh.o(.text+0x60): In function `main':
: undefined reference to `Time::printStandard()'
/tmp/cco9tHMh.o(.text+0x72): In function `main':
: undefined reference to `Time::setTime(int, int, int)'
/tmp/cco9tHMh.o(.text+0x82): In function `main':
: undefined reference to `std::cout'
/tmp/cco9tHMh.o(.text+0x87): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/cco9tHMh.o(.text+0x96): In function `main':
: undefined reference to `Time::printMilitary()'
/tmp/cco9tHMh.o(.text+0xa6): In function `main':
: undefined reference to `std::cout'

I am not posting the code here, because the code is not mine, I assume it is copyrighted. But it is the same kind of errors that I get with the code that I posted first. Except, obviously that I change the #includes as suggested.

jtshaw 03-02-2005 11:59 AM

Use g++ instead of gcc or at an -lstdc++ ot your compile command.

jtshaw 03-02-2005 12:17 PM

Okay... I made a few changes...

point.hh
Code:

#ifndef POINT_HH
#define POINT_HH

class Point
{

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

                      private:
                      int x;

};


#endif


point.cc
Code:

#include <iostream>
#include "point.hh"

using namespace std;

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


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

int main (void) {
        return 0;
}

This compiled cleanly and with no warnings using "g++ -Wall -o point point.cc". Naturally it doesn't do anything, but you could put test calles to set_x and print_x in main if you'd like.


All times are GMT -5. The time now is 10:51 PM.