What am I missing here?

First off, I am very new to C++, just started a week ago. I'm running into trouble with one of my classes. Here are my classes (I believe the problem is in my HockeyTeam.h constructor):

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
// HockeyPlayer.h

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

class HockeyPlayer
{
	public:
		HockeyPlayer(string name, string position, int number);
		~HockeyPlayer();
		inline string getName() const;
		inline string getPosition() const;
		inline int getNumber() const;
	private:
		string name;
		string position;
		int number;
};

HockeyPlayer::HockeyPlayer(string name, string position, int number)
{
	this->name = name;
	this->position = position;
	this->number = number;
}

HockeyPlayer::~HockeyPlayer()
{

}

inline string HockeyPlayer::getName() const
{
	return name;
}

inline string HockeyPlayer::getPosition() const
{
	return position;
}

inline int HockeyPlayer::getNumber() const
{
	return number;
}


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
// HockeyTeam.h

#include <iostream>
#include <string>
#include "HockeyPlayer.h"

class HockeyTeam
{
	public:
		HockeyTeam(HockeyPlayer center, HockeyPlayer leftWing, HockeyPlayer rightWing, HockeyPlayer leftDefenseman, HockeyPlayer rightDefenseman, HockeyPlayer goalie);
		~HockeyTeam();
		void getTeam();
	private:
		string name;
		HockeyPlayer team[6];
};

HockeyTeam::HockeyTeam(HockeyPlayer center, HockeyPlayer leftWing, HockeyPlayer rightWing, HockeyPlayer leftDefenseman, HockeyPlayer rightDefenseman, HockeyPlayer goalie)
{
	team[0] = center;
	team[1] = leftWing;
	team[2] = rightWing;
	team[3] = leftDefenseman;
	team[4] = rightDefenseman;
	team[5] = goalie;
}

HockeyTeam::~HockeyTeam()
{
}

void HockeyTeam::getTeam()
{
	for (int i = 0; i < 6; i++)
	{
		cout << team[i].getName() << endl;
		cout << team[i].getPosition() << endl;
		cout << team[i].getNumber() << endl << endl;
	}
}


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

#include <iostream>
#include <string>
#include "HockeyTeam.h"

using namespace std;

int main()
{
	HockeyPlayer center("Lemieux", "Center", 66);
	HockeyPlayer leftWing("Hossa", "Left Wing", 30);
	HockeyPlayer rightWing("Jagr", "Right Wing", 68);
	HockeyPlayer leftDefenseman("Kasparitis", "Left Defenseman", 00);
	HockeyPlayer rightDefenseman("Borque", "Right Defenseman", 00);
	HockeyPlayer goalie("Roy", "Goalie", 00);

	HockeyTeam penguins(center, leftWing, rightWing, leftDefenseman, rightDefenseman, goalie);

	penguins.getTeam();

	return 0;
}
in a weeks time you learned so much..classes, inline, const...impressive.. what you eat in your breakfast..!!! :P

anyway..

I'm running into trouble with one of my classes


whats the trouble...??? i didnt see any!!
Yeah, for the life of me I cant find what the problem is. The error is:

...error C2512: 'HockeyPlayer' : no appropriate default constructor available
HockeyTeam.h, line 15:
HockeyPlayer team[6];
The array initialization requires a constructor with no arguments
Last edited on
So I changed it to:

HockeyPlayer team[]

Now I get:

warning C4200: nonstandard extension used : zero-sized array in struct/union
and
error C2512: 'HockeyPlayer' : no appropriate default constructor available
Keep the [6], just add a constructor with no arguments or make the arguments in the constructor you already have optional
eg:
 
HockeyPlayer ( string name = "", string position = "", int number = 0 );

Thanks Bazzy, that worked. Why do I need to apply default values to the constructor in case I don't pass arguments?
You can also overload another constructor with no parameters but you need a constructor which can be called with no arguments, if you overload a constructor the default one (which takes no arguments) is not provided
Last edited on
Topic archived. No new replies allowed.