Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > gcc err : undefined reference to ?

Reply
Thread Tools

gcc err : undefined reference to ?

 
 
key9
Guest
Posts: n/a
 
      05-23-2006
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:rintch(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



 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-23-2006
key9 wrote:
> 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:rintch(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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Marcus Kwok
Guest
Posts: n/a
 
      05-23-2006
key9 <(E-Mail Removed)> wrote:
> 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:rintch(char*)
> collect2: ld returned 1 exit status.


Do you have the source for LinuxTestTerminal.cpp? If so, look to make
sure that LinuxTestTerminal:rintch(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-lit....html#faq-23.9

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      05-23-2006

"key9" <(E-Mail Removed)> wrote in message
news:e4vbtb$gsq$(E-Mail Removed)...
> 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:rintch(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


 
Reply With Quote
 
key9
Guest
Posts: n/a
 
      05-23-2006
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:rintch(char* ch){
fprintf(stdout,"%c",*ch);
}

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

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

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




 
Reply With Quote
 
key9
Guest
Posts: n/a
 
      05-24-2006
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:rintch(char* ch){
fprintf(stdout,"%c",*ch);
}


....


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      05-24-2006
key9 wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      05-24-2006

"key9" <(E-Mail Removed)> wrote in message
news:e4vlqt$rle$(E-Mail Removed)...

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
trying to redirect both std out and err to a file together and still err to a separate file qwertmonkey@syberianoutpost.ru Java 0 08-29-2012 09:07 AM
trying to redirect both std out and err to a file together and still err to a separate file qwertmonkey@syberianoutpost.ru Java 1 08-29-2012 07:28 AM
trying to redirect both std out and err to a file together and still err to a separate file qwertmonkey@syberianoutpost.ru Java 1 08-29-2012 12:10 AM
STDIN, OUT, ERR and $stdin, out, err - Differences? Terry Cooper Ruby 7 06-09-2009 05:48 AM
Xmit-Err and Rcv-Err via SNMP? Marco Shaw Cisco 3 10-03-2007 10:36 AM



Advertisments