put the code to .h file error gone why?

K

key9

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_
 
I

Ian Collins

key9 said:
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.
 
R

red floyd

key9 said:
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_

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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top