still problem of organize file

K

key9

Hi all

on last post I confused on how to organize file of class, ok ,the problem
solved : should include class define head on cpp file.
but this time ,still link error:

strange is I put the implement to .h file directly like this:
*******head file*****
class LinuxTestTerminal : public Terminal{
public:
void printch(char ch){
fprintf(stdout,"%c",ch); <--- directly implement here
}
};

there's no error occur;

but when I wrote the .cpp file,(same name as .h file)
*******head file*****
class LinuxTestTerminal : public Terminal{
public:
void printch(char ch);
};

*******cpp file******
#include "LinuxTestTerminal.h"
void
LinuxTestTerminal::printch(char ch){
fprintf(stdout,"%c",ch);
}

error occurs: undefined reference of printch();


What's on earth the problem is?

thank you very much!

key9


completed code here : cygwin enviroment

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

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

#include "LinuxTerminal.h"

using namespace std;


//this printch function is just for test ,direct use this can pass the test
print
/* void printch(char ch){
fprintf(stdout,"%c",ch);
}
*/


int main(int argc, char *argv[])
{
char q = 'c';

printf("print a new line of doing test");


LinuxTestTerminal* lt = new LinuxTestTerminal();
lt->printch(q);

//printch(q); // direct use this can pass, but lt->printch(q) can not
,why?



while(true); // test perpose loop

}

***********************************
// Terminal.h

#ifndef __TERMINAL_H_
#define __TERMINAL_H_


class Terminal{ // this is virtual class of terminal
public:

/* <--- comment for test perpose since can not pass the complie
virtual void printch(char) = 0;
virtual void backSpace() = 0;
virtual void printCR() = 0;
virtual void printTab() = 0;
*/

};


#endif // __TERMINAL_H_

***********************************
// LinuxTerminal.h

#ifndef __LINUXTERMINAL_H_
#define __LINUXTERMINAL_H_

#include "Terminal.h"

class LinuxTestTerminal : public Terminal{
public:

void printch(char ch);
void backSpace();
void printCR();
void printTab();


};


#endif // __LINUXTERMINAL_H_



****************************************
// LinuxTerminal.cpp

#include "LinuxTestTerminal.h" // this time, add this line

void
LinuxTestTerminal::printch(char ch){
fprintf(stdout,"%c",ch);
}

void
LinuxTestTerminal::backSpace(){
fprintf(stdout,"/b")
}

void
LinuxTestTerminal::printCR(){
fprintf(stdout,"/n");
}

void
LinuxTestTerminal::printTab(){
fprintf(stdout,"/t");
}
 
J

Jonathan Mcdougall

key9 said:
on last post I confused on how to organize file of class, ok ,the problem
solved : should include class define head on cpp file.
but this time ,still link error:

strange is I put the implement to .h file directly like this:
there's no error occur;
error occurs: undefined reference of printch();

Are you sure you link all these files together? Check your compiler's
documentation to learn how to deal with multiple source files.


Jonathan
 
H

Howard

key9 said:
Hi all

on last post I confused on how to organize file of class, ok ,the problem
solved : should include class define head on cpp file.

The link problem was NOT because you failed to include the header file in
your source file. If that was the problem, then the .cpp file would fail to
compile, and you'd get a compiler (not link) error.
but this time ,still link error:

strange is I put the implement to .h file directly like this:
*******head file*****
class LinuxTestTerminal : public Terminal{
public:
void printch(char ch){
fprintf(stdout,"%c",ch); <--- directly implement here
}
};

there's no error occur;

but when I wrote the .cpp file,(same name as .h file)
*******head file*****
class LinuxTestTerminal : public Terminal{
public:
void printch(char ch);
};

*******cpp file******
#include "LinuxTestTerminal.h"
void
LinuxTestTerminal::printch(char ch){
fprintf(stdout,"%c",ch);
}

error occurs: undefined reference of printch();


What's on earth the problem is?

thank you very much!

key9

It's the same problem as before. It isn't your code that's the problem,
it's the way you're compiling and linking. When you are compiling and
linking, you're simply failing to include the .cpp file properly. (And when
I say "include", I am referring to making sure the .cpp file compiles and
links properly, NOT whether you're using the preprocessor symbol "#include"
correctly!) You probably just need to specify the .cpp filename somewhere
in the command line (or in your project settings, if you have such a thing).
Read your compiler's documentation on how to compile and link multiple
source files.

-Howard
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top