error LNK2001: unresolved external symbol

E

Ed Dana

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

Michael

class Suite {
private:
static char *clsSuiteName[];
};
======================================================================

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

Jim Langston

Ed Dana said:
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.
 
E

Ed Dana

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top