C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

put the code to .h file error gone why?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
key9
Guest





PostPosted: Tue Jun 13, 2006 9:10 am    Post subject: put the code to .h file error gone why? Reply with quote



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._ZN17LinuxTestTerminalC1Ev+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_
Back to top
Ian Collins
Guest





PostPosted: Tue Jun 13, 2006 9:10 am    Post subject: Re: put the code to .h file error gone why? Reply with quote



key9 wrote:
Quote:
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._ZN17LinuxTestTerminalC1Ev+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.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.