473,503 Members | 11,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

put the code to .h file error gone why?

Hi all
I wrote some source code to study,

but I have problem: If I wrote the implement code to .h file directly

code can pass the complie, if I put the code to .cpp file ,it fails , why?

Here's the source code:

[key9@localhost unit]$ gcc -o test test.cpp -lstdc++
/home/key9/tmp/ccgdxJIB.o(.text+0x1c2): In function `main':
: undefined reference to `LinuxTestTerminal::printch(char*)'
/home/key9/tmp/ccgdxJIB.o(.gnu.linkonce.t._ZN17LinuxTestTerminalC 1Ev+0x19):
In function `LinuxTestTerminal::LinuxTestTerminal()':
: undefined reference to `vtable for LinuxTestTerminal'
collect2: ld returned 1 exit status

Source code:
********/Terminal.h/*********
#ifndef __TERMINAL_H_
#define __TERMINAL_H_

class Terminal{ // this is virtual class of terminal
public:
virtual void printch(char*) =0;
virtual void backSpace() = 0;
virtual void printCR() = 0;
virtual void printTab() =0;
private:
};
#endif // __TERMINAL_H_
********/LinuxTerminal.h/*********
#ifndef __LINUXTESTTERMINAL_H_
#define __LINUXTESTTERMINAL_H_

#include "Terminal.h"

class LinuxTestTerminal : public Terminal{
public:
void printch(char*);
void backSpace();
void printCR();
void printTab();
};

#endif //__LINUXTESTTERMINAL_H_

********/LinuxTerminal.cpp/*********
#include <stdio.h>
#include "Terminal.h"

void
LinuxTestTerminal::printch(char* ch){
printf("%c",*ch);
}
void
LinuxTestTerminal::backSpace(){
printf("/b");
}

void
LinuxTestTerminal::printCR(){
printf("/n");
}
void
LinuxTestTerminal::printTab(){
printf(" ");
}

********/test.cpp/*********
//use -lstdc++

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <iostream>

#include "Terminal.h"
#include "LinuxTerminal.h"

class Terminal;

using namespace std;

int mygetch( ) {
struct termios oldt,newt;
int ch;
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}

int main(int argc, char *argv[])
{

printf("new line of doing test");

LinuxTestTerminal lt;
lt.printch("dsadadasdasd");

while(mygetch());

}

*********************************
another /LinuxTerminal.h/, delete the LinuxTerminal.cpp file
#ifndef __LINUXTESTTERMINAL_H_
#define __LINUXTESTTERMINAL_H_

#include "Terminal.h"

class LinuxTestTerminal : public Terminal{
public:
void printch(char* ch){
printf("%c",*ch);

}
void backSpace(){
printf("/b");

}
void printCR(){
printf("/n");
}
void printTab(){
printf(" ");
}

};

#endif //__LINUXTESTTERMINAL_H_


Jun 13 '06 #1
2 1632
key9 wrote:
Hi all
I wrote some source code to study,

but I have problem: If I wrote the implement code to .h file directly

code can pass the complie, if I put the code to .cpp file ,it fails , why?

Here's the source code:

[key9@localhost unit]$ gcc -o test test.cpp -lstdc++
/home/key9/tmp/ccgdxJIB.o(.text+0x1c2): In function `main':
: undefined reference to `LinuxTestTerminal::printch(char*)'
/home/key9/tmp/ccgdxJIB.o(.gnu.linkonce.t._ZN17LinuxTestTerminalC 1Ev+0x19):
In function `LinuxTestTerminal::LinuxTestTerminal()':
: undefined reference to `vtable for LinuxTestTerminal'
collect2: ld returned 1 exit status

Look like you haven't linked the LinuxTerminal.cpp object file. You
should either compile both cpp files together, or read up on how to link
more than one object file.

--
Ian Collins.
Jun 13 '06 #2
key9 wrote:
Hi all
I wrote some source code to study,

but I have problem: If I wrote the implement code to .h file directly

code can pass the complie, if I put the code to .cpp file ,it fails , why?

Here's the source code:

[key9@localhost unit]$ gcc -o test test.cpp -lstdc++
/home/key9/tmp/ccgdxJIB.o(.text+0x1c2): In function `main':
: undefined reference to `LinuxTestTerminal::printch(char*)'
/home/key9/tmp/ccgdxJIB.o(.gnu.linkonce.t._ZN17LinuxTestTerminalC 1Ev+0x19):
In function `LinuxTestTerminal::LinuxTestTerminal()':
: undefined reference to `vtable for LinuxTestTerminal'
collect2: ld returned 1 exit status

Source code:
********/Terminal.h/*********
#ifndef __TERMINAL_H_
#define __TERMINAL_H_
Irrelevant to your linking problems, but your include guard is invalid
per 17.4.3.1.

[redacted]


********/LinuxTerminal.h/*********
#ifndef __LINUXTESTTERMINAL_H_
#define __LINUXTESTTERMINAL_H_
Same with this include guard.
[redacted]
#ifndef __LINUXTESTTERMINAL_H_
#define __LINUXTESTTERMINAL_H_


And this one.

[redacted]
Jun 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2173
by: ajikoe | last post by:
Hello, If I use os.remove(fileName), does it always assure that the code will move to the next code only if the fileName is deleted completely? Pujo
1
2470
by: H.L Bai | last post by:
hi, everybody i meet a parse error when i used the xml4c. any proposal is helpful. The error is following .../XMLRegionHandler.h:59 parse error before '*' .../XMLRegionHandler.h:60 parse...
2
1788
by: Susan Baker | last post by:
Hi, I have declared a class MyClass in a header file MyClass.h I have then gone onto define the class in MyClass.cpp. This is (roughly) what the definition (.cpp) file looks like: #include...
1
1453
by: Russ | last post by:
I've been trying to get my head around this for 3 days now and it seems like everything I try does not work for one reason or another. I built a test page using the TabStrip and MultiPage controls....
21
2098
by: Sandy | last post by:
Hello - I am using Visual Studio .Net. I need an example of how to construct a class that can be used throughout a project where I can include its subs and functions in various pages in the...
1
5087
by: Dan | last post by:
Environment: Win2k Server, ASP.NET(v1.1.4322) Everything seemed to work fine till this morning...and than this happened What could this possibly mean? Is it the file? (The file in the error...
6
2332
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
27
2687
by: David Golightly | last post by:
This is just a quick poll for all you web devs out there: What browsers do you test on/are concerned about compatibility with? Obviously, you're going to test on current-generation browsers such...
3
3067
by: Yash | last post by:
Hi all, I found that while we are updating our application, the application is not known to tomcat, untill it is completed. So tomcat sends 503 page as resource not found. I want to know is...
2
9251
by: AtTheEnd | last post by:
Hi folks, I've searched high and low for solution. Here's the skinny: MySQL Server 5.0 on Windows Server 2003 I have a 156MB sql file I'm trying to import using mysql -uusername -p database <...
0
7212
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7296
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5604
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4696
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1524
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
405
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.