473,659 Members | 2,681 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(.tex t+0x1c2): In function `main':
: undefined reference to `LinuxTestTermi nal::printch(ch ar*)'
/home/key9/tmp/ccgdxJIB.o(.gnu .linkonce.t._ZN 17LinuxTestTerm inalC1Ev+0x19):
In function `LinuxTestTermi nal::LinuxTestT erminal()':
: undefined reference to `vtable for LinuxTestTermin al'
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 __LINUXTESTTERM INAL_H_
#define __LINUXTESTTERM INAL_H_

#include "Terminal.h "

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

#endif //__LINUXTESTTERM INAL_H_

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

void
LinuxTestTermin al::printch(cha r* ch){
printf("%c",*ch );
}
void
LinuxTestTermin al::backSpace() {
printf("/b");
}

void
LinuxTestTermin al::printCR(){
printf("/n");
}
void
LinuxTestTermin al::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");

LinuxTestTermin al lt;
lt.printch("dsa dadasdasd");

while(mygetch() );

}

*************** *************** ***
another /LinuxTerminal.h/, delete the LinuxTerminal.c pp file
#ifndef __LINUXTESTTERM INAL_H_
#define __LINUXTESTTERM INAL_H_

#include "Terminal.h "

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

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

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

};

#endif //__LINUXTESTTERM INAL_H_


Jun 13 '06 #1
2 1639
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(.tex t+0x1c2): In function `main':
: undefined reference to `LinuxTestTermi nal::printch(ch ar*)'
/home/key9/tmp/ccgdxJIB.o(.gnu .linkonce.t._ZN 17LinuxTestTerm inalC1Ev+0x19):
In function `LinuxTestTermi nal::LinuxTestT erminal()':
: undefined reference to `vtable for LinuxTestTermin al'
collect2: ld returned 1 exit status

Look like you haven't linked the LinuxTerminal.c pp 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(.tex t+0x1c2): In function `main':
: undefined reference to `LinuxTestTermi nal::printch(ch ar*)'
/home/key9/tmp/ccgdxJIB.o(.gnu .linkonce.t._ZN 17LinuxTestTerm inalC1Ev+0x19):
In function `LinuxTestTermi nal::LinuxTestT erminal()':
: undefined reference to `vtable for LinuxTestTermin al'
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 __LINUXTESTTERM INAL_H_
#define __LINUXTESTTERM INAL_H_
Same with this include guard.
[redacted]
#ifndef __LINUXTESTTERM INAL_H_
#define __LINUXTESTTERM INAL_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
2199
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
2497
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 error before '*'
2
1793
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 "BaseHeader.h" /****************************************************/ /* Constructors/Destructor */
1
1458
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. The example shows how to put up pictures in each tab, and it works fine. But of course I don't want pictures, I want a webform where the data is different in each tab/view. The form could be different in each tab too, but for now I'll settle...
21
2123
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 project. I googled this and either I didn't use the correct words, or there doesn't seem to be much on it. Any help will be appreciated!
1
5094
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 is not missing nor is it misspelled or on a different path) Is it permissions? (ASP.NET account is given full access to E:\DNN dir)
6
2340
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 solution. My code can be downloaded from here: http://www.tprimke.net/konto/PyObject-problem.tar.bz2. There are some scripts for GNU/Linux system (bash to be precise). All you need to know is that there are four classes. (Of course, you may...
27
2724
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 as IE6, IE7, Firefox 1.5/2, Opera 8/9, Safari 2, etc. How old must a browser be before you stop worrying about it? Anybody here still test on IE4? Thanks,
3
3093
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 virtual hosting is the right way to deal with this issue. Is it possible to customise the 503 page, so that, for any resource not found issue, tomcat displays the customised page. I am trying to set the page in tomcat directory itself. I have gone...
2
9264
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 < dump.sql I get an error response of: Error 2006 <HY000> at line 145: Mysql Server has gone away. I already increase the max_allowed_packets to 250MB. And I've already checked the error logs and there are no messages. I can't split up the...
0
8339
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8751
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8535
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8629
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2757
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 we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.