Very simple assigning array

class Hangman{
public:
Hangman(void);
static const char ALPHABET[];
static const string DICTIONARY[];
static const int ALPHABET_SIZE = 26;
private:
vector<char> guesses;
char remainingLetters[];
};

Hangman::Hangman()
{
const string DICTIONARY[] = {"word1","word2"};
char ALPHABET[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','u','r','s','t','v','w','x','y','z' };
char remainingLetters[] = ALPHABET;
}



I get the following error :


1 IntelliSense: initialization with '{...}' expected for aggregate object f:\visual studio 2010\projects\learncplusplus\hangman\hangman.cpp


Why can I not assign this array to another?

Thanks

Here ALPHABET is an array to constant char an remainingLetters is an array to char. You can't assign it because it break constness: if you could assign it, you could modify the chars of ALPHABET.

And another thing, ALPHABET, DICTIONARY and ALPHABET_SIZE are static members, normally you have to initialize them outside the class like this:
 
const string Hangman::DICTIONARY[] = {"word1","word2"};
1. whats wrong with initalizing DICTIONARY and ALPHABET in the constructor?
2. whats the best way assign ALPHABET to remainingLetters? const cast? copy array? loop through each element and assign to each element of remainingLetters?

Thanks
static object are linked with the class and not the instance of the class .
as while instantiating the object of the class the constuctor is called . which will initialize the array again and again for each object of the class . This is not the goal of the static variable.

Thanks bluecoder, however I am still unsure of how to assign a const array to a non const arrary. I managed to do it for a string, but I cant get the array copy constructor to work

const string str = "chris";
string cpy (str); //use copy constructor
cpy = "tom";
cout << "const string is: " << str<< endl;
cout << "cpy is: " << cpy << endl;

const char ALPHABET[] = {'a','b'};
char cpy[2] (ALPHABET); //doesnt compile

Any tips?

Thanks
Also I am having another problem. I moved all static declarations out of my constructor to a new main class. e.g

#include "Hangman.h"

int main()
{

const string Hangman::DICTIONARY[] = {"word1","word2"};
const char Hangman::ALPHABET[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','u','r','s','t','v','w','x','y','z' };
const int DICTIONARY_SIZE = sizeof(Hangman::DICTIONARY) /sizeof(*Hangman::DICTIONARY);
Hangman h;
return 0;
}

However I get the error "cannot be defined in the current scope" on each line. What am I missing?

Thanks
bump
I am not able to understand you ..
you cannot assign character array this way ..
char cpy[2] (ALPHABET); //doesnt compile
secondly you cannot declare it in the main .. it should be outside of main ()
1
2
3
4
const string Hangman::DICTIONARY[] = {"word1","word2"};
const char Hangman::ALPHABET[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','u','r','s','t','v','w','x','y','z' };
const int DICTIONARY_SIZE = sizeof(Hangman::DICTIONARY) /sizeof(*Hangman::DICTIONARY);
Hangm
an h;
Thanks the declaration of variables now compiles.

I know it doesnt compile, but I dont know how else to assign a const array to a non const array.

The only way I seem to be able to this is via:


for(int i = 0; i< ALPHABET_SIZE ;i++)
{
remainingLetters[i] = char(ALPHABET[i]);
}

However there must be a more efficient way than this surely??
Of couse you could use a const cast but the goal of const is to give robustness in your program and if you break it, it can become horrible.
Why are you using const? If you use const because the ALPHABET can't change, which is a good reason I think, respect this choice in your entire program.
What is the role of remainingLetters? I would say it is the letters the player can still use.
So Alphabet and remaining letters are DIFFERENTS thing. So you don't have to assign ALPHABET to remaining letters.

To make things simpler, I would suggest you not using arrays.
Here remainingLetters would be best implemented as a std::unordered_set (or std::set if you don't have c++11)

And you can fill it like this i think:
 
remainingLetters = std::set(ALPHABET, ALPHABET + sizeof(ALPHABET)/sizeof(*ALPHABET));
Yes your right remainingLetters is the letters the player can still use. This array will be the same as ALPHABET during start up since the player has not had any guesses.

Thanks for the showing me how to do it using sets. Just out of interest how would you do it using arrays?

Thanks
Using array you can do it by first copying ALPHABET like you did or using std::copy.
You have to keep the size of the remaining letters in anorther field, say nbRemain.
Then each time the player uses a letter you have to find in the array if the letter is present(std::find) and if yes swap the letter with the one at the end of the array and decrement the size.
But it's far more simple to use sets (and maybe more efficient for the search but not sure, there is probably too few elements to take advantage of std::set but it may be better with std::unordered_set).
Thanks aquaz.

Yes if I was changing the size, a set or vector would have been better, however all I was planning to do is replace guessed letter with '-' so the size always stays the same.

Anyone who is instressted in the codez:

const char ALPHABET[] = {'a','b'};
char arrCpy[2];
std::copy(ALPHABET, ALPHABET +2,arrCpy);
cout << "const arr el 0: " << ALPHABET[0]<< '\n';
cout << "cpy is: " << arrCpy[0] << '\n';
Topic archived. No new replies allowed.