C++ C++;构造函数中的错误
我的代码有问题。 我有一个叫做Player的类,看起来像这样C++ C++;构造函数中的错误,c++,arrays,constructor,C++,Arrays,Constructor,我的代码有问题。 我有一个叫做Player的类,看起来像这样 class Player { public: ... Player(); Player(string firstName, string lastName, int birthYear); ~Player(); ... }; string firstName = ...; string lastName = ...; int birth = ... Player team[x](firstName, lastName,
class Player
{
public:
...
Player();
Player(string firstName, string lastName, int birthYear);
~Player();
...
};
string firstName = ...;
string lastName = ...;
int birth = ...
Player team[x](firstName, lastName, birth); // <--- This is were I get my errors
我的source.cpp看起来像这样
class Player
{
public:
...
Player();
Player(string firstName, string lastName, int birthYear);
~Player();
...
};
string firstName = ...;
string lastName = ...;
int birth = ...
Player team[x](firstName, lastName, birth); // <--- This is were I get my errors
我想使用的构造函数是Player(stringfirstname,stringlastname,int birthYear)
。我想我可能正在source.cpp中使用默认构造函数
我想创建5个玩家团队[x](名字、姓氏、出生)
但这就是我犯错误的地方。有什么建议吗?这一行根本无效:
Player team[x](firstName, lastName, birth); // <--- This is were I get my errors
当然,在最初创建数组时,您已经创建了一组(默认已初始化)。由于这是C++,所以使用<代码> STD::vector < /代码> .<
此外,还有一些错误,但没有产生错误:
int matches;
int* dates = new int[matches];
此处,
匹配项
未初始化,其值不确定。读取该变量会调用未定义的行为,当然,您不希望数组有任何随机大小(为什么不再使用向量?),您需要在使用它之前初始化匹配项。这一行根本无效:
Player team[x](firstName, lastName, birth); // <--- This is were I get my errors
当然,在最初创建数组时,您已经创建了一组(默认已初始化)。由于这是C++,所以使用<代码> STD::vector < /代码> .<
此外,还有一些错误,但没有产生错误:
int matches;
int* dates = new int[matches];
此处,匹配项
未初始化,其值不确定。读取该变量会调用未定义的行为,当然,您不希望数组有任何随机大小(为什么不再使用向量?),您需要在使用它之前初始化匹配项。这一行根本无效:
Player team[x](firstName, lastName, birth); // <--- This is were I get my errors
当然,在最初创建数组时,您已经创建了一组(默认已初始化)。由于这是C++,所以使用<代码> STD::vector < /代码> .<
此外,还有一些错误,但没有产生错误:
int matches;
int* dates = new int[matches];
此处,匹配项
未初始化,其值不确定。读取该变量会调用未定义的行为,当然,您不希望数组有任何随机大小(为什么不再使用向量?),您需要在使用它之前初始化匹配项。这一行根本无效:
Player team[x](firstName, lastName, birth); // <--- This is were I get my errors
当然,在最初创建数组时,您已经创建了一组(默认已初始化)。由于这是C++,所以使用<代码> STD::vector < /代码> .<
此外,还有一些错误,但没有产生错误:
int matches;
int* dates = new int[matches];
此处,匹配项
未初始化,其值不确定。读取该变量会调用未定义的行为,当然,您不希望数组有任何随机大小(为什么不再次使用向量?),您需要在使用它之前初始化匹配项。代码的一个问题是变量匹配项尚未初始化,并且具有不确定的值
int matches;
int* dates = new int[matches];
在调用newint[matches]
之前,应该初始化匹配项
当您分配了一个玩家数组时,将构建一个团队
,由nrOfPlayers
玩家组成:
Player* team = new Player[nrOfPlayers];
您现在可以通过创建一个临时player
对象并将其分配给team
中的一个元素来填写玩家信息。这将调用播放器的隐式定义的复制分配运算符:
将第75行替换为:
team[x] = Player(firstName, lastName, birth); // copy constructor is called
代码的一个问题是变量匹配
尚未初始化,并且具有不确定的值
int matches;
int* dates = new int[matches];
在调用newint[matches]
之前,应该初始化匹配项
当您分配了一个玩家数组时,将构建一个团队
,由nrOfPlayers
玩家组成:
Player* team = new Player[nrOfPlayers];
您现在可以通过创建一个临时player
对象并将其分配给team
中的一个元素来填写玩家信息。这将调用播放器的隐式定义的复制分配运算符:
将第75行替换为:
team[x] = Player(firstName, lastName, birth); // copy constructor is called
代码的一个问题是变量匹配
尚未初始化,并且具有不确定的值
int matches;
int* dates = new int[matches];
在调用newint[matches]
之前,应该初始化匹配项
当您分配了一个玩家数组时,将构建一个团队
,由nrOfPlayers
玩家组成:
Player* team = new Player[nrOfPlayers];
您现在可以通过创建一个临时player
对象并将其分配给team
中的一个元素来填写玩家信息。这将调用播放器的隐式定义的复制分配运算符:
将第75行替换为:
team[x] = Player(firstName, lastName, birth); // copy constructor is called
代码的一个问题是变量匹配
尚未初始化,并且具有不确定的值
int matches;
int* dates = new int[matches];
在调用newint[matches]
之前,应该初始化匹配项
当您分配了一个玩家数组时,将构建一个团队
,由nrOfPlayers
玩家组成:
Player* team = new Player[nrOfPlayers];
您现在可以通过创建一个临时player
对象并将其分配给team
中的一个元素来填写玩家信息。这将调用播放器的隐式定义的复制分配运算符:
将第75行替换为:
team[x] = Player(firstName, lastName, birth); // copy constructor is called
请把你的密码贴在这里。这些错误中哪一个让你感到困惑?我试过了。但是当我在这个页面上使用code函数,然后复制我的代码时,它只在一行左右放一个代码片段。我不喜欢抄袭我所有的台词。是否有更好的方法将代码复制到此页面?@user3194111选择代码,复制,来到这里,粘贴,调整缩进。有关格式的信息,请查看帮助选项。选择“代码”->“Ctrl+K请在此处发布代码”。这些错误中哪一个让你感到困惑?我试过了。但是当我在这个页面上使用code函数,然后复制我的代码时,它只在一行左右放一个代码片段。我不喜欢抄袭我所有的台词。是否有更好的方法将代码复制到此页面?@user3194111 selec