gcc err : undefined reference to ?

Discussion in 'C++' started by key9, May 23, 2006.

  1. key9

    key9 Guest

    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
     
    key9, May 23, 2006
    #1
    1. Advertising

  2. 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::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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 23, 2006
    #2
    1. Advertising

  3. key9

    Marcus Kwok Guest

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

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, May 23, 2006
    #3
  4. key9

    Howard Guest

    "key9" <> wrote in message
    news:e4vbtb$gsq$...
    > 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
     
    Howard, May 23, 2006
    #4
  5. key9

    key9 Guest

    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");
    }
     
    key9, May 23, 2006
    #5
  6. key9

    key9 Guest

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


    ....
     
    key9, May 24, 2006
    #6
  7. 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
     
    Victor Bazarov, May 24, 2006
    #7
  8. key9

    Howard Guest

    "key9" <> wrote in message
    news:e4vlqt$rle$...

    >
    >
    > 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
     
    Howard, May 24, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page