Click to See Complete Forum and Search --> : First c++ program with .h file won't compile


NecroLin
03-20-2006, 07:31 AM
OK, so I'm learning C++ and am trying to compile my first main.cpp and *.h file and am getting errors. The code was downloaded from the official site for the book that I'm using so I'm assuming there are no errors, but... it won't compile!!! What am I doing wrong?? Or does the program have errors in it?

Files:
main.cpp:
#include <iostream>

using namespace std;

#include "ccc_time.h"

int main()
{
Time wake_up(5, 6, 7);
wake_up.add_seconds(1000);
cout << wake_up.get_hours() << ":" << wake_up.get_minutes() << ":" << wake_up.get_seconds() << endl;

system("pause");
return (0);
}

ccc_time.h:
#ifndef CCC_TIME_H
#define CCC_TIME_H


class Time
{
public:

Time(int hour, int min, int sec);

Time();

int get_hours() const;

int get_minutes() const;

int get_seconds() const;

int seconds_from(Time t) const;

void add_seconds(int s);

private:
int time_in_secs;
};

#endif

Error I get:
[Linker error] undefined reference to 'Time::Time(int, int, int)'
[Linker error] undefined reference to 'Time::add_seconds(int)'
[Linker error] undefined reference to 'Time::get_seconds() const'
[Linker error] undefined reference to 'Time::get_minutes() const'
[Linker error] undefined reference to 'Time:: get_hours() const'
ls returned 1 exit status

I've tried compiling using g++ from command line, using Bloodshet Dev C++, and using Anjuta. I just can't seem to get it to work. What's wrong?

Thanks.

wheelnut
03-20-2006, 08:29 AM
My shockingly limited knowledge of C++ suggested the following changes to the ccc_time.h file:
/* START Here ---------------------------*/
#ifndef CCC_TIME_H
#define CCC_TIME_H

class Time
{
public:

Time(int hour, int min, int sec)
{
}

Time();

int get_hours() const
{
};

int get_minutes() const
{
};

int get_seconds() const
{
};

int seconds_from(Time t) const
{
};

void add_seconds(int s)
{
};

private:
int time_in_secs;

};

#endif
/* END Here ---------------------------------- */

I think that the compiler that the book is using is allowing the prototypes of the functions to exist without the actual methods... Anyway, try this, it should compile ok with g++

NecroLin
03-20-2006, 09:25 AM
The book forgot to mention a 3rd file that was required in order to compile the program. Erghhhh, not very reader friendly if you ask me. It works now. =)

bwkaz
03-20-2006, 07:33 PM
But system("pause"); won't work; Linux has no pause command, so the system() function can't run it.

You want to remove this line: since you'll be running your program from the command line anyway, you won't need to pause the console to see the output. (Or at least, you should be running the program from the CLI; that way you can see errors that it prints, if any.) When you're running a program from a console, it's really crappy from a usability perspective if it makes you hit a key before it will exit. It's just one more pointless key I have to hit before I can do the next thing I need. :)

wheelnut
03-21-2006, 05:32 AM
You could try linking in the curses module (it is only a C based bit of code, not C++, so it might take some messing about (like extern "C"...)) and replace the system ("pause"); with getch (); - just a thought.

bwkaz
03-21-2006, 07:57 PM
But that's still another pointless key I have to hit before I can do whatever I was planning on doing next. How would you like it if ls added a link to ncurses and a getch() call right before it exited? ;)

(If you're worried about the program running in a new terminal, and that terminal closing before you can see the results (like what happens on windows), then you can fire up certain terminals with a "--pause" option, which will make the terminal wait until you press a key. Then your program doesn't do it on its own, so if the user already has a terminal open, they won't be bothered. I know Eterm supports the --pause option, but I can't seem to find a similar option for xterm, so I don't think it's universal. Too bad; it's a nice option.)