1

For c++, using visual studio command prompt (2010) as compiler, notepad++ as editor

I'm having such a specific issue I can't seem to find the answer I'm looking for in searches so here goes:

Regarding a program assignment with some constraints (I cant use different variable).
I have a class Card with a static constant member array called "faceName" in my untouchable header file.
I am initializing this member array in my implementation file with assignment operator and list of string literals.
Instead of the boring text "Spades" and "Hearts" etc for suit names, I want to use the char ascii codes 3,4,5, and 6 for the actual symbols.
I can't figure out how to typecast my char variables into something the string can read then load them into the string all in a single line (Since a constant can't be modified later, and I can't actually do the char creation in main, and must be done loosely floating in the implementation file.

Been years since I've programmed, sorry for my poor vocabulary usage if I stated anything incorrectly.

Card.h

class Card
{
    private:
        int face;
        int suit;

        static const int NUM_OF_SUITS = 4;
        static const int FACES_PER_SUIT = 13;

        static const string suitName[NUM_OF_SUITS];
        static const string faceName[FACES_PER_SUIT];

    public:
        string toString() const;
}


Card.cpp

const string Card::faceName[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"}
const string Card::suitName[] = {(char)3, (char)4, (char)5, (char)6}

string Card::toString() const
{
    string nameOfCard;
    if (suit >= 0 && suit <= NUM_OF_SUITS && face >= 0 && face <= FACES_PER_SUIT)
    {
        nameOfCard = faceName[face] + " of " + suitName[suit];
    }
    else nameOfCard = "Error, card type doesn't exist.";
    return nameOfCard;
}


and my error: card.cpp(14) : error C2440: 'initializing' : cannot convert from 'char' to 'const std::string' No constructor could take the source type, or constructor overload resolution was ambiguous

And I can't simply create an sstream to do the conversion because where would I do that? In main - too late, I need to initialize the member arrays in the implementation file.

Thanks for any help!

1 Answer 1

3

I guess you want this:

const string Card::suitName[] = {"\03", "\04", "\05", "\06"};

But I'm not sure how this is a good idea, because these ASCII codes you're trying to use are not printable characters, so how is it going to work when you print them to the screen?

Sign up to request clarification or add additional context in comments.

1 Comment

When using:<br> char a = 3; cout << a << endl; <br>I get the symbol showing up in the command console just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.