Array of Class Objects Errors.

I'm getting some errors in my code for an array of class object, but I'm not sure why. Everything works with 5 elements, but when I try to add more I get up to 20 errors. It seems to be hinting to something being wrong with my for loop counter.

Example.

This works!

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

	const int NUM_GAMES = 5; 

	Game Inventory[NUM_GAMES] =
	{
	    Game("Metal Gear Solid V: The Phantom Pain", 59.99, "Kojima Productions", "Konami"),
	    Game("Bloodborne", 59.99, "From Software", "SCE"),
	    Game("The Last of Us", 49.99, "Naughty Dog", "SCE"),
	    Game("Infamous: Second Son", 39.99, "SCE", "SCE"), 
	    Game("Driveclub", 29.99, "Evolution Studios", "SCE")
		
	};
	
for (int i = 0; i < NUM_GAMES; i++) // LOOP COUNTER.
 {
   cout << setw(40) << left << Inventory[i].getDescription() << "$" << setw(10) << left 
    << Inventory[i].getCost() << setw(10) << left << Inventory[i].getDeveloper() << setw(5) 
      << left << Inventory[i].getPublisher() << "\n";
  }
	return 0;
}


But this does not!

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

const int NUM_GAMES = 9; 

Game Inventory[NUM_GAMES] =
	{
	  Game("Metal Gear Solid V: The Phantom Pain", 59.99, "Kojima Productions", "Konami"),
	  Game("Bloodborne", 59.99, "From Software", "SCE"),
          Game("The Last of Us", 49.99, "Naughty Dog", "SCE"),
	  Game("Infamous: Second Son", 39.99, "SCE", "SCE"), 
	  Game("Driveclub", 29.99, "Evolution Studios", "SCE")
	  Game("Grand Theft Auto", 59.99, "Rockstar Games", "Rockstar North")
		
	};
	
for (int i = 0; i < NUM_GAMES; i++) // LOOP COUNTER.
	{
	cout << setw(40) << left << Inventory[i].getDescription() << "$" << setw(10) << left
              << Inventory[i].getCost() << setw(10) << left << Inventory[i].getDeveloper() 
               << setw(5) << left << Inventory[i].getPublisher() << "\n";
	}

	return 0;
}


What I did was change const int NUM_GAMES = //to 9. And then tried to add more to my list below
(See "Grand Theft Auto" line).

Here's my class / constructors if there is anything wrong with them. I'm not sure!

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

class Game
{
private: 

	string Description; // DESCRIPTION OF GAME.
	string Publisher;	// PUBLISHER OF GAME.
	string Developer;	// DEVELOPER OF GAME.
	double Cost;		// COST OF THE VIDEO GAME FOR RENTAL.
	int YearReleased;	// YEAR THE VIDEO GAME WAS RELEASED.
	
public:
	// CONSTRUCTOR #1.
	Game() 
	{ 
		Description = " "; 
		Publisher = " ";
		Developer = " ";
		Cost = 0; 
	}

	// CONSTRUCTOR #2.

	Game(string Desc, string Pub, string Dev)
	{
		Description = Desc; 
		Developer = Dev;
		Publisher = Pub; 
		Cost = 0; 	
	}

	// CONSTRUCTOR #3.
	Game(string Info, double Price, string Pub, string Dev)
	{
		Description = Info; 
		Developer = Dev;
		Publisher = Pub;
		Cost = Price; 
	}


SCREENSHOT of errors: http://i.imgur.com/DQKAd4N.png
Any ideas as to what is wrong?
Thank you! This website is awesome!

Last edited on
You're missing a comma in between your Game() functions.
Nevermind, you were right! That's just embarrassing lmao. Don't you just hate when it's that one semicolon or comma? Usually I wait before posting issues, but for some reason I was assuming it was a different error due to my new found experience with classes etc.
Last edited on
Can you post or link to your full code or post your error messages?
If you are using C++11 (which you should), have a habit of placing comma after each array element:
1
2
3
4
5
6
7
8
int foo[] {
    1,
    2,
    3,
    4,
    5, //Comma here is ignored by compiler, but helps if you want to add other element
       //or reorder them
};
Last edited on
I use Microsoft Visual C++ 2010. At least that's what we use for my community college.
Topic archived. No new replies allowed.