By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,796 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

put the code to .h file error gone why?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.