Expected primary expression before '{' token

Hello... Just started out and am trying to explore the various features of c++ by writing a small program.

In xcode I got the errors:

Expected primary expression before '{' token
Expected ';' before '{' token

... on line 31 in the following code:

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
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using std::cout;
using std::endl;

#include <iomanip>
using std::setw;

#include <string>
using std::string;


#include "Tut.h"

Warrior::Warrior(string name)
{
	setWarriorName(name);
}

void Warrior::setWarriorName(string name)
{
	warriorNameSet = name;
}

string Warrior::getWarriorName()
{
	return warriorNameSet;
}

void Warrior::warriorStats()
{
	startStats[statNum] = {1, 1, 1, 1};

	cout << "| Str | Def | Agl | Lck |" << endl;
	
	for (int i = 0; i < statNum; i++)
	{
		if (i == 0)
		{
			cout << setw(4) << startStats[i];	
		}
		else 
		{
			cout << setw(6) << startStats[i];
		}

	}
	cout << endl;
}


here is the header file and main.cpp

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

class Warrior
{
public:
	
	const static int statNum = 4;
	
	Warrior(string);
	void setWarriorName(string);
	string getWarriorName();
	void warriorStats();
	int startStats[statNum];
	
private:
	string warriorNameSet;

};


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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
using std::cout;
using std::cin;
#include <string> 
using std::string;	 

using namespace std;

#include "Tut.h"

void startPrint();
string nameInput();

int main()
{	

	startPrint();
	cout << "Name of Warrior:";
	string warriorNamed = nameInput();

	
    Warrior warrior1(warriorNamed);
	
	warrior1.warriorStats();

	
	cout << warrior1.getWarriorName() 
	<< " Vs "
	<< warrior1.getWarriorName() << endl;

	return 0;
	
}

void startPrint()
{
	cout << "\n*****************************\n"
	<< "** Welcome to Battle Arena **\n"
	<< "*****************************\n" << endl ;
}

string nameInput()
{
	string testName;
	string passedName;
	int z = 0;
	
	cin >> testName;
	
	while (z == 0)
	{
		if (testName.length() >= 12)
		{
			cout << "Name must be less than 25 characters..."
			<< endl ;
			cout << "Retype Name:"; 
			cin >> testName;
		}
		if (testName.length() < 12)
		{
			passedName = testName;
			z = 1;
		}
	}
	
	return passedName;
	
}


Any help would be great.... Thanks.

You can only use the {} syntax when initializing arrays. For example,
 
int a[]={0,3,1};

In your case, you have to initialize each element of Warrior::startStats individually.
Alternatively, since all the elements have the same value, you can use std::fill().
http://www.cplusplus.com/reference/algorithm/fill/
1
2
3
4
//example use of std::fill() on an array
int a[1000];
std::fill(a,a+1000,12);
std::cout <<a[128];
Last edited on
thanks... i think i get it....

I just added int and it seemed to work:

int startStats[statNum] = {1, 1, 1, 1};

Whats the point in putting...

int startStats[statNum];

... in the header file? I thought that initialised the array....

thanks.
int startStats[statNum] = {1, 1, 1, 1};
Note that that creates an array separate from the class member.


Whats the point in putting...

int startStats[statNum];

... in the header file? I thought that initialised the array....
That only declares the array. You still have to initialize it.
Declare: add a symbol to the compiler's symbol table so that it can be used (compile time action).
Initialize: set data to a useful value for the first time (run time action).
OK... thanks a lot helios... you've been a great help.
Topic archived. No new replies allowed.