error LNK2001: unresolved external symbol

Discussion in 'C++' started by Ed Dana, Jan 5, 2007.

  1. Ed Dana

    Ed Dana Guest

    Greetings, all. :)

    I'm familiar with other object oriented programming tools, such as Java
    & REBOL. In an effort to become more familiar with C++, I decided to
    write an object oriented BlackJack program. To begin with, I needed some
    simple static routines for telling the value or suite of a face card.
    So, I wrote the following C++ routine for Suites:
    ======================================================================
    class Suite {
    public:
    static int getSuiteID(int prmCardID);
    static char *getSuiteName(int prmCardID);
    static char getShortName(int prmCardID);
    static int main( );

    Suite();

    private:
    static char *clsSuiteName[];
    static int getSID(int prmCardID);

    };

    Suite::Suite( ) {
    clsSuiteName[0] = "Hearts";
    clsSuiteName[1] = "Clubs";
    clsSuiteName[2] = "Diamonds";
    clsSuiteName[3] = "Spades";
    }

    int Suite::getSID(int prmCardID) {
    return prmCardID / 13;
    }

    int Suite::getSuiteID(int prmCardID) {
    return getSID(prmCardID) + 1;
    }

    char *Suite::getSuiteName(int prmCardID) {
    return clsSuiteName[ getSID( prmCardID ) ];
    }

    char Suite::getShortName(int prmCardID) {
    return (char)getSuiteName(prmCardID);
    }
    ======================================================================

    Compiling this in M$ VC gives me the linker error: 'Suite.obj : error
    LNK2001: unresolved external symbol "private: static char * *
    Suite::clsSuiteName" (?clsSuiteName@Suite@@0PAPADA)'

    Compiling it in wxDevC++ gives me a similar error: "[Linker error]
    undefined reference to `Suite::clsSuiteName'"

    I've tried everything I can think of to correct the problem, but at this
    point, I ain't got a clue.

    Any and all advice is welcome. :)

    Ed.
    Ed Dana, Jan 5, 2007
    #1
    1. Advertising

  2. Ed Dana

    Michael Guest

    > class Suite {
    <snip>
    > private:
    > static char *clsSuiteName[];
    > };
    >

    <snip>

    > ======================================================================
    >
    > Compiling this in M$ VC gives me the linker error: 'Suite.obj : error
    > LNK2001: unresolved external symbol "private: static char * *
    > Suite::clsSuiteName" (?clsSuiteName@Suite@@0PAPADA)'
    >
    > Compiling it in wxDevC++ gives me a similar error: "[Linker error]
    > undefined reference to `Suite::clsSuiteName'"


    You need to define clsSuiteName. You've declated it in your class (in
    a .h file or some such), now you need to define it in a .cpp file or
    some such. The definition will probably look something like this:
    char* Suite::clsSuiteName[] = { "abc", "def" };

    Michael

    P.S. While you're at it, you might look into the joy of std::string
    instead of char*, but that's orthogonal to the problem you're having.
    Michael, Jan 5, 2007
    #2
    1. Advertising

  3. Ed Dana

    Jim Langston Guest

    "Ed Dana" <> wrote in message
    news:Xgxnh.10686$...
    > Greetings, all. :)
    >
    > I'm familiar with other object oriented programming tools, such as Java &
    > REBOL. In an effort to become more familiar with C++, I decided to write
    > an object oriented BlackJack program. To begin with, I needed some simple
    > static routines for telling the value or suite of a face card. So, I wrote
    > the following C++ routine for Suites:
    > ======================================================================
    > class Suite {
    > public:
    > static int getSuiteID(int prmCardID);
    > static char *getSuiteName(int prmCardID);
    > static char getShortName(int prmCardID);
    > static int main( );
    >
    > Suite();
    >
    > private:
    > static char *clsSuiteName[];


    You declared clsSuitName, but you have to define it.

    > static int getSID(int prmCardID);
    >
    > };


    char* Suit::clsSuitName[] = { "Hearts", "Clubs", "Diamands", "Spades" };

    > Suite::Suite( ) {
    > clsSuiteName[0] = "Hearts";
    > clsSuiteName[1] = "Clubs";
    > clsSuiteName[2] = "Diamonds";
    > clsSuiteName[3] = "Spades";
    > }


    I think this is what you tried to do for clsSuitName, but you misnamed it,
    and made it a function.

    > int Suite::getSID(int prmCardID) {
    > return prmCardID / 13;
    > }
    >
    > int Suite::getSuiteID(int prmCardID) {
    > return getSID(prmCardID) + 1;
    > }
    >
    > char *Suite::getSuiteName(int prmCardID) {
    > return clsSuiteName[ getSID( prmCardID ) ];
    > }
    >
    > char Suite::getShortName(int prmCardID) {
    > return (char)getSuiteName(prmCardID);
    > }
    > ======================================================================
    >
    > Compiling this in M$ VC gives me the linker error: 'Suite.obj : error
    > LNK2001: unresolved external symbol "private: static char * *
    > Suite::clsSuiteName" (?clsSuiteName@Suite@@0PAPADA)'
    >
    > Compiling it in wxDevC++ gives me a similar error: "[Linker error]
    > undefined reference to `Suite::clsSuiteName'"
    >
    > I've tried everything I can think of to correct the problem, but at this
    > point, I ain't got a clue.
    >
    > Any and all advice is welcome. :)
    >
    > Ed.
    Jim Langston, Jan 5, 2007
    #3
  4. Ed Dana

    Ed Dana Guest

    @ All

    Excellent, thank you! I knew I was missing something obvious.


    @ Michael

    > P.S. While you're at it, you might look into the joy of std::string
    > instead of char*, but that's orthogonal to the problem you're having.


    I did, initially, but that caused me more errors, so I decided I should
    learn to crawl before I walk. :)

    Ed.
    Ed Dana, Jan 5, 2007
    #4
    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. Izak Pretorius
    Replies:
    2
    Views:
    1,568
    Christopher
    Sep 14, 2003
  2. We need more power captain
    Replies:
    3
    Views:
    6,688
    Muthu
    Feb 18, 2004
  3. ozgan.net
    Replies:
    2
    Views:
    6,062
  4. Faith
    Replies:
    2
    Views:
    4,573
    Ben Pfaff
    Mar 2, 2004
  5. sadegh
    Replies:
    6
    Views:
    851
    Masterchief
    Apr 17, 2007
Loading...

Share This Page