LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-27-2005, 11:06 PM   #1
figadiablo
Member
 
Registered: Feb 2002
Location: Virginia USA
Distribution: Slackware, CentOS, Fedora
Posts: 342

Rep: Reputation: 30
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!!
 
Old 02-27-2005, 11:18 PM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
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

Last edited by jonaskoelker; 03-02-2005 at 09:53 AM.
 
Old 03-02-2005, 09:07 AM   #3
figadiablo
Member
 
Registered: Feb 2002
Location: Virginia USA
Distribution: Slackware, CentOS, Fedora
Posts: 342

Original Poster
Rep: Reputation: 30
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.

Last edited by figadiablo; 03-02-2005 at 09:10 AM.
 
Old 03-02-2005, 11:59 AM   #4
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Use g++ instead of gcc or at an -lstdc++ ot your compile command.
 
Old 03-02-2005, 12:17 PM   #5
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Classes in VC++ Diederick Programming 2 11-30-2005 10:57 AM
Possible uses for classes. RHLinuxGUY Programming 4 11-21-2005 10:10 PM
classes in C++ niteshadw Programming 2 07-05-2005 07:05 PM
PHP and classes Boby Programming 5 03-23-2005 05:15 AM
OOP (PHP) classes and extended classes ldp Programming 3 03-05-2005 11:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration