0

I've got a problem with my code. I've got a class called Player which looks like this

class Player
{
public:
   ...
Player();
Player(string firstName, string lastName, int birthYear);
~Player();
   ...
};

My source.cpp looks like this

string firstName = ...;
string lastName = ...;
int birth = ...

Player team[x](firstName, lastName, birth); // <--- This is were I get my errors

My errors are saying

error C3074: an array can only be initialized with an initializer-list

error C2466: cannot allocate an array of constant size 0

error C2057: expected constant expression

The constructor I want to use is Player(string firstName, string lastName, int birthYear). I think that I might be using the default constructor in source.cpp

I want to create 5x Player team[x](firstName, lastName, birth)

But this is where I get my errors. Any suggestions?

4
  • Please post your code here. Which of those errors is confusing you? Commented Feb 14, 2014 at 21:01
  • I tried to. But when I use the code function on this page, and then copy my code, it just puts a code snippet around 1 line. I don't fancy copying all of my lines. Is there a better way to copy code to this page? Commented Feb 14, 2014 at 21:02
  • @user3194111 select code, copy, come here, paste, adjust indent. Look at the help options for information about formatting. Commented Feb 14, 2014 at 21:03
  • 1
    Select code -> Ctrl+K Commented Feb 14, 2014 at 21:04

2 Answers 2

3

This line simply isn't valid:

Player team[x](firstName, lastName, birth); // <--- This is were I get my errors

It doesn't make sense. You're trying to declare an array and call a constructor at the same time. You already created your team array. If you want to create a Player and assign it then you would use:

team[x] = Player(firstName, lastName, birth);

Of course, you already created a bunch of them (default initialized) when you created the array in the first place. Since this is C++, use a std::vector<Player>.


Also, something that is wrong but is not generating an error:

int matches;
int* dates = new int[matches];

Here, matches is uninitialized and its value is indeterminate. Reading that variable invokes undefined behavior, and certainly you don't want any random size for your array (why aren't you using a vector again?) You need to initialize matches before using it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I've never used vectors, that's why. I'll google around and look for it. Thanks
0

One problem with your code is that the variable matches hasn't been initialized and has an indeterminate value.

int matches;
int* dates = new int[matches];

You should initialize matches before calling new int[matches].

A team of nrOfPlayers players is constructed when you allocated an array of Players:

Player* team = new Player[nrOfPlayers];

You can now fill in a player's information by creating a temporary Player object and assigning it to an element in team. This will call Player's implicitly defined copy assignment operator:

Replace line 75 with:

team[x] = Player(firstName, lastName, birth); // copy constructor is called

4 Comments

"This is happening because you're trying to allocate an array of integers with the variable matches which hasn't been defined." - That's not true. matches is uninitialized and has an indeterminate value, and reading it is UB, but that is not a compiler error. The size error comes from this line- Player team[x](firstName, lastName, birth);
@EdS. Thanks for pointing that out. I revised my entry to reflect your correction.
Okay. The player constructor works now. The problem now is - as you mentioned - the int dates[matches]. My idea was to put int* dates = new int[matches] the line below sas >> matches; However, that did not work. I got some linking errors
@user3194111: What is the type of sas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.