Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old May 23rd, 2006, 05:05 PM
key9
Guest
 
Posts: n/a
Default gcc err : undefined reference to ?

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



  #2  
Old May 23rd, 2006, 05:45 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: gcc err : undefined reference to ?

key9 wrote:[color=blue]
> 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"[/color]

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.
[color=blue]
> class LinuxTestTerminal : public Terminal{[/color]

We have no idea what 'Terminal' is.
[color=blue]
> public:
>
> void printch(char* ch );
> // void printch(char ch);[/color]

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.
[color=blue]
> 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[/color]

Which line?
[color=blue]
> it report has no member named 'printch'
>
>
> why?[/color]

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


  #3  
Old May 23rd, 2006, 06:05 PM
Marcus Kwok
Guest
 
Posts: n/a
Default Re: gcc err : undefined reference to ?

key9 <iamkey9@126.com> wrote:[color=blue]
> Hi all:
>
> app error :
>
> //main.cpp
> ...
> char* q ;
> *q = 'c';[/color]

q is a pointer to garbage, then you try to store a character in this
garbage, so you have undefined behavior here.
[color=blue]
> 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.[/color]

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-lit....html#faq-23.9

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
  #4  
Old May 23rd, 2006, 06:15 PM
Howard
Guest
 
Posts: n/a
Default Re: gcc err : undefined reference to ?


"key9" <iamkey9@126.com> wrote in message
news:e4vbtb$gsq$1@news.yaako.com...[color=blue]
> Hi all:
>
> app error :
>
> //main.cpp
> ...
> char* q ;
> *q = 'c';[/color]

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...
[color=blue]
> 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);[/color]

As Victor asked, why did you change this from char to char*? Is this a
virtual function in Terminal, or your own function?
[color=blue]
> void backSpace();
> void printCR();
> void printTab();
>
>
> };
>
> #endif // __LINUXTERMINAL_H_
>
> gcc report: undefined reference to 'LinuxTestTerminal::printch(char*)
> collect2: ld returned 1 exit status.[/color]

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

-Howard


  #5  
Old May 23rd, 2006, 07:55 PM
key9
Guest
 
Posts: n/a
Default Re: gcc err : undefined reference to ?

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");
}




  #6  
Old May 24th, 2006, 03:35 AM
key9
Guest
 
Posts: n/a
Default Re: gcc err : undefined reference to ?

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);
}


....


  #7  
Old May 24th, 2006, 01:55 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: gcc err : undefined reference to ?

key9 wrote:[color=blue]
> the strange is ,when I move the fuction implement to .h file
> every things seems ok. why?[/color]

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.
[color=blue]
> [...][/color]

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  #8  
Old May 24th, 2006, 04:05 PM
Howard
Guest
 
Posts: n/a
Default Re: gcc err : undefined reference to ?


"key9" <iamkey9@126.com> wrote in message
news:e4vlqt$rle$1@news.yaako.com...
[color=blue]
>
>
> int main(int argc, char *argv[])
> {
> char* q = new char();
>
> *q = 'c';
>[/color]

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.
[color=blue]
> 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?[/color]

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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.