String problems

This is a discussion on String problems within the C++ Programming forums, part of the General Programming Boards category; Ok, so I'm making this BlackJack game, and it uses strings, and when I use a "cout << stringGoesHere" I ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    27

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

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Do you have:
    using namespace std;
    in your code?

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    27
    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."
    Last edited by sirSolarius; 12-18-2003 at 07:46 PM.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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.

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    27
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  4. io stream and string class problems
    By quizteamer in forum C++ Programming
    Replies: 2
    Last Post: 04-25-2005, 01:07 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21