LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 03-20-2006, 05:37 AM   #1
necrolin
LQ Newbie
 
Registered: Jun 2003
Posts: 10

Rep: Reputation: 0
Question First c++ program with .h file won't compile


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.
 
Old 03-20-2006, 06:11 AM   #2
fakhrul
Member
 
Registered: Mar 2006
Location: Dhaka,Bangladesh
Distribution: Ubuntu 12.10
Posts: 51

Rep: Reputation: 15
Quote:
Originally Posted by necrolin
main.cpp:
#include <iostream>

using namespace std;

#include "ccc_time.h"

ccc_time.h:
#ifndef CCC_TIME_H
#define CCC_TIME_H


class Time
{
...
...
};
Simple, you have a header file having function declarations in class but where is the function definitions?

you need another .cpp or c++ file that will contain the definitions of those functions.
you see all errors are for missing functions:
Quote:
Originally Posted by necrolin
[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'

Last edited by fakhrul; 03-20-2006 at 06:15 AM.
 
Old 03-20-2006, 07:24 AM   #3
necrolin
LQ Newbie
 
Registered: Jun 2003
Posts: 10

Original Poster
Rep: Reputation: 0
That worked. This book is a little frustrating in that the chapter had the example file, but didn't explain anything how to compile the program/get it to work. Not very newbie friendly if you ask me. The other file was in the slew of files that I downloaded from the website. I added it & everything works great. Super thanks.
 
  


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
Why I can't compile my c program? zhuqlfeixia Programming 6 12-12-2005 09:13 PM
How to compile a C program in Glade which is included the <pcap.h> header file. swaviswa Programming 0 03-21-2004 07:47 AM
How does one compile a program? What do you need? TheMusicGuy Linux - Newbie 4 03-12-2004 01:10 AM
Compile Program mattp Linux - Newbie 4 03-09-2004 11:13 AM
compile program Eddie9 Linux - General 1 05-21-2002 10:11 PM

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

All times are GMT -5. The time now is 12:05 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