Help with undefined references

Discussion in 'C++' started by sieg1974@yahoo.com, Aug 15, 2006.

  1. Guest

    Hi,

    I know it must be a simple mistake, but I'm just learning C++ and can't
    figure out what's wrong. Any help will be vey much appreciated :)
    I got the following error messages when the following program is
    compiled and linked. What could be wrong?

    Thanks,

    Andre

    /tmp/ccX0Z0Il.o(.text+0x17d): In function `Singleton::Instance()':
    : undefined reference to `Singleton::theSingleton'
    /tmp/ccX0Z0Il.o(.text+0x1ca): In function `Singleton::Instance()':
    : undefined reference to `Singleton::theSingleton'
    /tmp/ccX0Z0Il.o(.text+0x1cf): In function `Singleton::Instance()':
    : undefined reference to `Singleton::theSingleton'
    collect2: ld returned 1 exit status

    #include <iostream>

    class Singleton
    {
    public:
    static Singleton * Instance();
    ~Singleton();

    private:
    Singleton();
    static Singleton * theSingleton;
    };

    Singleton::Singleton()
    {
    std::cout << "Singleton constructot\n";
    }

    Singleton::~Singleton()
    {
    std::cout << "Singleton destructor\n";
    }

    Singleton * Singleton::Instance()
    {
    if( !theSingleton )
    {
    theSingleton = new Singleton();
    }

    return( theSingleton );
    }

    Singleton * theSingleton = 0;

    int main()
    {
    return( 0 );
    }
     
    , Aug 15, 2006
    #1
    1. Advertising

  2. Greg Guest

    wrote:
    > Hi,
    >
    > I know it must be a simple mistake, but I'm just learning C++ and can't
    > figure out what's wrong. Any help will be vey much appreciated :)
    > I got the following error messages when the following program is
    > compiled and linked. What could be wrong?
    >
    > Thanks,
    >
    > Andre
    >
    > /tmp/ccX0Z0Il.o(.text+0x17d): In function `Singleton::Instance()':
    > : undefined reference to `Singleton::theSingleton'
    > /tmp/ccX0Z0Il.o(.text+0x1ca): In function `Singleton::Instance()':
    > : undefined reference to `Singleton::theSingleton'
    > /tmp/ccX0Z0Il.o(.text+0x1cf): In function `Singleton::Instance()':
    > : undefined reference to `Singleton::theSingleton'
    > collect2: ld returned 1 exit status
    >
    > #include <iostream>
    >
    > class Singleton
    > {
    > public:
    > static Singleton * Instance();
    > ~Singleton();
    >
    > private:
    > Singleton();
    > static Singleton * theSingleton;
    > };


    >
    > Singleton * theSingleton = 0;


    Should be:

    Singleton * Singleton::theSingleton = 0;

    The member name needs to be qualified with its class name:

    Greg
     
    Greg, Aug 15, 2006
    #2
    1. Advertising

  3. Guest

    You can see this book <<Modern C++ Design>>
    Chapter 6. Implementing Singletons
    :)
    wrote:
    > Hi,
    >
    > I know it must be a simple mistake, but I'm just learning C++ and can't
    > figure out what's wrong. Any help will be vey much appreciated :)
    > I got the following error messages when the following program is
    > compiled and linked. What could be wrong?
    >
    > Thanks,
    >
    > Andre
    >
    > /tmp/ccX0Z0Il.o(.text+0x17d): In function `Singleton::Instance()':
    > : undefined reference to `Singleton::theSingleton'
    > /tmp/ccX0Z0Il.o(.text+0x1ca): In function `Singleton::Instance()':
    > : undefined reference to `Singleton::theSingleton'
    > /tmp/ccX0Z0Il.o(.text+0x1cf): In function `Singleton::Instance()':
    > : undefined reference to `Singleton::theSingleton'
    > collect2: ld returned 1 exit status
    >
    > #include <iostream>
    >
    > class Singleton
    > {
    > public:
    > static Singleton * Instance();
    > ~Singleton();
    >
    > private:
    > Singleton();
    > static Singleton * theSingleton;
    > };
    >
    > Singleton::Singleton()
    > {
    > std::cout << "Singleton constructot\n";
    > }
    >
    > Singleton::~Singleton()
    > {
    > std::cout << "Singleton destructor\n";
    > }
    >
    > Singleton * Singleton::Instance()
    > {
    > if( !theSingleton )
    > {
    > theSingleton = new Singleton();
    > }
    >
    > return( theSingleton );
    > }
    >
    > Singleton * theSingleton = 0;
    >
    > int main()
    > {
    > return( 0 );
    > }
     
    , Aug 15, 2006
    #3
  4. Guest

    Thanks for the help, I could make it work now. But I have another
    question :).
    I have the following 2 versions of my program. The first one works
    fine, but not the second one. I get the same linking erros as before.
    Can anyone point out what could be wrong?

    Thanks,

    Andre

    ***********
    Version 1
    ***********

    #include <iostream>

    class Singleton
    {
    public:
    static Singleton * Instance()
    {
    if( !theSingleton )
    {
    theSingleton = new Singleton();
    }

    return( theSingleton );
    }

    private:
    static Singleton * theSingleton;

    };

    Singleton * Singleton::theSingleton = 0;

    int main()
    {
    return( 0 );
    }

    ***********
    Version 2
    ***********

    #include <iostream>

    class Singleton
    {
    public:
    static Singleton * Instance();

    private:
    static Singleton * theSingleton;

    };

    Singleton * Singleton::Instance()
    {
    if( !theSingleton )
    {
    theSingleton = new Singleton();
    }

    return( theSingleton );
    }

    Singleton * Singleton::theSingleton = 0;

    int main()
    {
    return( 0 );
    }
     
    , Aug 15, 2006
    #4
  5. wrote:
    > Thanks for the help, I could make it work now. But I have another
    > question :).
    > I have the following 2 versions of my program. The first one works
    > fine, but not the second one. I get the same linking erros as before.


    Same, which?

    > Can anyone point out what could be wrong?
    >
    > Thanks,
    >
    > Andre
    >
    > ***********
    > Version 1
    > ***********
    >
    > #include <iostream>

    ^^^^^^^^^^^^^^^^^^^
    You don't need this since you use nothing from that header.

    >
    > class Singleton
    > {
    > public:
    > static Singleton * Instance()
    > {
    > if( !theSingleton )
    > {
    > theSingleton = new Singleton();
    > }
    >
    > return( theSingleton );


    I really wish you'd drop the parentheses around the expression in the
    'return' statement. Makes your code look amateurish. 'return' is not
    a function. There is no need for parentheses!

    > }
    >
    > private:
    > static Singleton * theSingleton;
    >
    > };
    >
    > Singleton * Singleton::theSingleton = 0;
    >
    > int main()
    > {
    > return( 0 );


    Ugh!

    > }
    >
    > ***********
    > Version 2
    > ***********
    >
    > #include <iostream>
    >
    > class Singleton
    > {
    > public:
    > static Singleton * Instance();
    >
    > private:
    > static Singleton * theSingleton;
    >
    > };
    >
    > Singleton * Singleton::Instance()
    > {
    > if( !theSingleton )
    > {
    > theSingleton = new Singleton();
    > }
    >
    > return( theSingleton );


    Yuck!

    > }
    >
    > Singleton * Singleton::theSingleton = 0;
    >
    > int main()
    > {
    > return( 0 );


    Yechh!

    > }


    I don't get any link errors if I try building your programs from
    the source code you posted.

    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, Aug 15, 2006
    #5
  6. Salt_Peter Guest

    wrote:
    > Thanks for the help, I could make it work now. But I have another
    > question :).
    > I have the following 2 versions of my program. The first one works
    > fine, but not the second one. I get the same linking erros as before.
    > Can anyone point out what could be wrong?
    >
    > Thanks,
    >
    > Andre
    >
    > ***********
    > Version 1
    > ***********
    >
    > #include <iostream>
    >
    > class Singleton
    > {
    > public:
    > static Singleton * Instance()
    > {
    > if( !theSingleton )
    > {
    > theSingleton = new Singleton();


    try:
    theSingleton = new Singleton;

    > }
    >
    > return( theSingleton );


    return theSingleton;

    > }
    >
    > private:
    > static Singleton * theSingleton;
    >
    > };
    >
    > Singleton * Singleton::theSingleton = 0;
    >
    > int main()
    > {
    > return( 0 );
    > }
    >
    > ***********
    > Version 2
    > ***********
    >
    > #include <iostream>
    >
    > class Singleton
    > {
    > public:
    > static Singleton * Instance();
    >
    > private:
    > static Singleton * theSingleton;
    >
    > };
    >
    > Singleton * Singleton::Instance()
    > {
    > if( !theSingleton )
    > {
    > theSingleton = new Singleton();


    same changes...
    theSingleton = new Singleton;

    > }
    >
    > return( theSingleton );
    > }
    >
    > Singleton * Singleton::theSingleton = 0;
    >
    > int main()
    > {
    > return( 0 );


    return 0;

    > }
     
    Salt_Peter, Aug 16, 2006
    #6
    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.
Similar Threads
  1. Roger Leigh
    Replies:
    8
    Views:
    467
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    474
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    631
    DanielEKFA
    May 16, 2005
  4. Replies:
    8
    Views:
    745
    Bruno Desthuilliers
    Dec 12, 2006
  5. Replies:
    5
    Views:
    515
    Default User
    Jun 30, 2006
Loading...

Share This Page