ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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();
}
//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!!
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.
#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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.