gcc err : undefined reference to ?

K

key9

Hi all:

app error :

//main.cpp
....
char* q ;
*q = 'c';
LinuxTestTerminal* lt = new LinuxTestTerminal();
lt->printch(q);
...


**********************************
//LinuxTestTerminal.h
#ifndef __LINUXTERMINAL_H_
#define __LINUXTERMINAL_H_

#include "Terminal.h"

class LinuxTestTerminal : public Terminal{
public:

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


};

#endif // __LINUXTERMINAL_H_

gcc report: undefined reference to 'LinuxTestTerminal::printch(char*)
collect2: ld returned 1 exit status.

if I deleted that line

it report has no member named 'printch'


why?

thank you very much

key9
 
V

Victor Bazarov

key9 said:
app error :

//main.cpp
...
char* q ;
*q = 'c';
LinuxTestTerminal* lt = new LinuxTestTerminal();
lt->printch(q);
..


**********************************
//LinuxTestTerminal.h
#ifndef __LINUXTERMINAL_H_
#define __LINUXTERMINAL_H_

#include "Terminal.h"

Are we supposed to know what that is? If it's OS-specific header,
you should consider posting to the newsgroup that deals with your OS.
class LinuxTestTerminal : public Terminal{

We have no idea what 'Terminal' is.
public:

void printch(char* ch );
// void printch(char ch);

Why is that commented out? Are you trying to override a virtual
function? You are supposed to keep the signature the same, you know.

Besides, 'printch' name *suggests* that it's supposed to only print
a single character. Passing a pointer makes much less sense. Passing
a pointer to *non-const* char makes even less sense than that.
void backSpace();
void printCR();
void printTab();


};

#endif // __LINUXTERMINAL_H_

gcc report: undefined reference to 'LinuxTestTerminal::printch(char*)
collect2: ld returned 1 exit status.

if I deleted that line

Which line?
it report has no member named 'printch'


why?

Could it be because you deleted it?

V
 
M

Marcus Kwok

key9 said:
Hi all:

app error :

//main.cpp
...
char* q ;
*q = 'c';

q is a pointer to garbage, then you try to store a character in this
garbage, so you have undefined behavior here.
LinuxTestTerminal* lt = new LinuxTestTerminal();
lt->printch(q);
..


**********************************
//LinuxTestTerminal.h
#ifndef __LINUXTERMINAL_H_
#define __LINUXTERMINAL_H_

#include "Terminal.h"

class LinuxTestTerminal : public Terminal{
public:

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


};

#endif // __LINUXTERMINAL_H_

gcc report: undefined reference to 'LinuxTestTerminal::printch(char*)
collect2: ld returned 1 exit status.

Do you have the source for LinuxTestTerminal.cpp? If so, look to make
sure that LinuxTestTerminal::printch(char*) has been implemented. If it
hasn't, then since you are deriving from another class, maybe something
along the lines of this issue is present:

http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.9
 
H

Howard

key9 said:
Hi all:

app error :

//main.cpp
...
char* q ;
*q = 'c';

Bad idea. The variable q is a pointer, but it has not been initialized yet.
This results in Undefined Behavior, and anything can happen when it is run
(such as your program crashing).

In most cases when you're expected to pass a pointer to an existing item,
you pass the address of that item, not a pointer variable. Something like
this:

char q = 'c';
....and then...
lt->printch(&q);

But I suspect that you don't really want this anyway. See below...
LinuxTestTerminal* lt = new LinuxTestTerminal();
lt->printch(q);
..


**********************************
//LinuxTestTerminal.h
#ifndef __LINUXTERMINAL_H_
#define __LINUXTERMINAL_H_

#include "Terminal.h"

class LinuxTestTerminal : public Terminal{
public:

void printch(char* ch );
// void printch(char ch);

As Victor asked, why did you change this from char to char*? Is this a
virtual function in Terminal, or your own function?
void backSpace();
void printCR();
void printTab();


};

#endif // __LINUXTERMINAL_H_

gcc report: undefined reference to 'LinuxTestTerminal::printch(char*)
collect2: ld returned 1 exit status.

This is a link error, not a compile error. You apparently have not defined
[the body of] your function printch anywhere.

-Howard
 
K

key9

still confuse :
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 = new 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
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 printch(char ch);
void backSpace();
void printCR();
void printTab();


};


#endif // __LINUXTERMINAL_H_



****************************************
// LinuxTerminal.cpp
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");
}
 
K

key9

the strange is ,when I move the fuction implement to .h file
every things seems ok. why?

// LinuxTerminal.h

#ifndef __LINUXTERMINAL_H_
#define __LINUXTERMINAL_H_

#include "Terminal.h"

class LinuxTestTerminal : public Terminal{
public:

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


};


#endif // __LINUXTERMINAL_H_

****************************************
// LinuxTerminal.cpp
void
LinuxTestTerminal::printch(char* ch){
fprintf(stdout,"%c",*ch);
}


....
 
V

Victor Bazarov

key9 said:
the strange is ,when I move the fuction implement to .h file
every things seems ok. why?

Maybe because you forget to compile and/or link the translation unit
with the function into your program. Learn about project management
and how to maintain several source files.

V
 
H

Howard

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

*q = 'c';

Why are you doing that??? Now you've got a memory leak, because you're
calling new but not delete.

Just do like I showed you:

char q = 'c';
....
lt->printch(&q);

There's no reason to dynamically allocate the char variable. Just pass the
address using &. It accomplishes the same thing with less headaches.
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?

Because you're not linking LinuxTerminal properly. Look up how to include
your .cpp files when compiling/linking. (I don't use cygwin, Linux, or
command-line compiles, so I can't tell you how.)

-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,067
Latest member
HunterTere

Latest Threads

Top