Can't initialize string array in constructor

Hi everyone,

I am currently practicing designing classes. In one exercise, I am trying to store 15 words in an array, and randomly print one (using the rand() functions and seeding it with crime. I have a header file, a respective .cpp file, and a main .cpp file. When I try to compile the code using g++ GuessWord.cpp UseGuessWord.cpp -o UseGuessWord, I get the following error in my constructor: expected primary-expression before ‘{’ token

Here is my code:

header file (GuessWord.h):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef GUESSWORD
#define GUESSWORD
#include <string>
using namespace std;

class GuessWord{
	public:
		GuessWord();
		void Launch() const; //begins the game
		void displayWord(int number) const; //displays the word
	
	private:
		string words[15];

};


#endif 


Here is the respective .cpp file (GuessWord.cpp)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "GuessWord.h"
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;

GuessWord::GuessWord(){ // initialize the word array
	words = {"automobile","lantern", "surfing", "woman", "university", "computer", "vacation", "television",
		"apartment", "airplane", "venom", "elephant", "recording",
		"Canada", "rhinoceros"};
}

void GuessWord::Launch() const{
	srand(time(0));
	int number = rand() % 15;
	string displayWord(number);
}

void displayWord(int number) const{
	cout << words[i] << endl;
}


Here is my main class (USeGuessWord.cpp)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include "GuessWord.h"
using namespace std;

int main(){
	GuessWord game1();
	game1.lauch();
	
	
	return 0;
}


I am new to C++, and any help would be greatly appreciated.

Thank you!
In your constructor, you are not allowed to do such an assignment because your string array has already been created. It is like:
1
2
string arr[2];
arr = {"Hi", "there!"};

which is illegal.

Instead, you will have to use an initialization list, which looks like:
1
2
3
4
5
Foo::Foo()
   : arr(  //Note the colon which indicates the start of the list
      {"Blah", "Blah", /*...*/}
   ) //Also note that we use parentheses
{}

Using an initialization list declares the variable with the value(s), like:
 
string arr[2] = {"Hi", "There!"};
closed account (28poGNh0)
I changed your code hope its fine

header file (GuessWord.h):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# ifndef GUESSWORD
# define GUESSWORD

# include <string>
using namespace std;

class GuessWord
{
	public:
		GuessWord();
		void Launch() const; //begins the game
		void displayWord(int number) const; //displays the word

	private:
		string words[15];
};

#endif 



Here is the respective .cpp file (GuessWord.cpp)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# include "GuessWord.h"

# include <iostream>
# include <string>
# include <ctime>
# include <cstdlib>
using namespace std;

GuessWord::GuessWord()
{
    // You can initialize the word array at this way since you know its elements number
	const char *szBuffer[] = {"automobile","lantern", "surfing", "woman", "university", 
                              "computer","vacation", "television","apartment", "airplane",
                              "venom", "elephant","recording","Canada", "rhinoceros"};

    for(int i=0;i<15;i++)
    {
        words[i] = szBuffer[i] ;
    }
}

void GuessWord::Launch() const
{
	srand(time(0));
	int number = rand() % 15;
	//string displayWord(number);
	displayWord(number);
}

// displayWord(int number) is a member of GuessWord class so...
void GuessWord::displayWord(int number) const
{
	// cout << words[i] << endl; i is underclared
	cout << words[number] << endl;
}


Here is my main class (USeGuessWord.cpp)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# include "GuessWord.h"

# include <iostream>
# include <string>
# include <ctime>
# include <cstdlib>
using namespace std;

int main()
{
	GuessWord game1;
	// game1.lauch(); Syntax wrong

	game1.Launch();

	return 0;
}


any question just ask

Hope that helps
Topic archived. No new replies allowed.