-
String problems
Ok, so I'm making this BlackJack game, and it uses strings, and when I use a "cout << stringGoesHere" I get the following error:
Code:
Linking...
BJMain.obj : error LNK2001: unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl PlayingCard::formatCard(int)" (?formatCard@PlayingCard@@SA?AV?$basic_string@DU?$ch
ar_traits@D@std@@V?$allocator@D@2@@std@@H@Z)
Debug/BlackJack.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
BlackJack.exe - 2 error(s), 0 warning(s)
What's up with that? I #included <string> and I'm running VC++.
-
Do you have:
using namespace std;
in your code?
-
Argh!
Code:
// Deck.h: interface for the Deck class.
//
//////////////////////////////////////////////////////////////////////
#include "PlayingCard.h"
#if !defined(AFX_DECK_H__F82EEDCF_483B_48F6_8843_87FC69005F06__INCLUDED_)
#define AFX_DECK_H__F82EEDCF_483B_48F6_8843_87FC69005F06__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class Deck
{
public:
Deck();
~Deck();
void shuffle();
void resetCards();
int *deal(int cards);
protected:
int count;
PlayingCard card[52];
};
#endif // !defined(AFX_DECK_H__F82EEDCF_483B_48F6_8843_87FC69005F06__INCLUDED_)
Code:
// PlayingCard.h: interface for the PlayingCard class.
//
//////////////////////////////////////////////////////////////////////
#include <string>
#if !defined(AFX_PLAYINGCARD_H__E9A6857C_CA56_4680_AAD6_D63942541399__INCLUDED_)
#define AFX_PLAYINGCARD_H__E9A6857C_CA56_4680_AAD6_D63942541399__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class PlayingCard
{
public:
PlayingCard(int cardNum);
PlayingCard();
int getValue();
int getCard();
int getRealValue();
static int *cardToValue(int cardNum);
void setUsed(bool ifUsed);
std::string formatCard();
static std::string formatCard(int cardNum);
bool isUsed();
void setValue(int cardNum);
protected:
int value;
int suit;
bool used;
};
#endif // !defined(AFX_PLAYINGCARD_H__E9A6857C_CA56_4680_AAD6_D63942541399__INCLUDED_)
Code:
#include "Deck.h"
#include "PlayingCard.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
Deck theDeck;
int *newCards=theDeck.deal(2);
for(int x=0;x<*newCards;x++)
{
string card=PlayingCard::formatCard(*(newCards+1+x));
cout << "You have been dealt a " << card << "\n";
}
return 1;
}
Code:
--------------------Configuration: BlackJack - Win32 Debug--------------------
Linking...
BJMain.obj : error LNK2001: unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl PlayingCard::formatCard(int)" (?formatCard@PlayingCard@@SA?AV?$basic_string@DU?$ch
ar_traits@D@std@@V?$allocator@D@2@@std@@H@Z)
Debug/BlackJack.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
BlackJack.exe - 2 error(s), 0 warning(s)
If I do using namespace std; after the string in PlayingCard, I get like 200 errors of "Ambiguous symbol."
-
Looks like a linker error, in which case the namespace wouldn't help anyway. Plus you have std::string anyway. I'll see if I can compile it.
-
What's your formatCard function look like?
> string card=PlayingCard::formatCard(*(newCards+1+x));
Maybe you need:
static string card=PlayingCard::formatCard(*(newCards+1+x));
I notice you have static in your formatCard declaration. I'm not sure why though.
-
Ahh, I fixed it... I defined the string.h twice within my code, and when I deleted one declaration, everything worked.
Thanks for the help, though!