gpt4 book ai didi

c++ - 在类中的函数外部归档 vector

转载 作者:搜寻专家 更新时间:2023-10-30 23:55:44 25 4
gpt4 key购买 nike

这里的问题很简单,在类 .cpp 文件中填充函数外部 vector 的最佳方法是什么?目前我正在尝试以下不起作用:

std::vector<Player> midfielder(8);

midfielder.at(0) = Midfielder("Default ",0,"Midfielder");
midfielder.at(1) = Midfielder("David Armitage ",1,"Midfielder");
midfielder.at(2) = Midfielder("Tom Rockliff ",2,"Midfielder");
midfielder.at(3) = Midfielder("Gary Ablett ",3,"Midfielder");
midfielder.at(4) = Midfielder("Dyson Heppel ",4,"Midfielder");
midfielder.at(5) = Midfielder("Scott Pendlebury",5,"Midfielder");
midfielder.at(6) = Midfielder("Michael Barlow ",6,"Midfielder");
midfielder.at(7) = Midfielder("Jack Steven ",7,"Midfielder");

为了提供上下文,“Midfielder”是一个继承自“Player”类的类。

团队管理.h

#ifndef TEAMMANAGEMENT_H
#define TEAMMANAGEMENT_H
#include <vector>
#include "Player.h"
#include "Midfielder.h"
#include <string>

class TeamManagement
{
public:
TeamManagement();


void Display_Players();

};

#endif // TEAMMANAGEMENT_H

团队管理.cpp

#include <iostream>
#include <string>
#include <vector>
#include "Player.h"
#include "Midfielder.h"
#include "TeamManagement.h"
using namespace std;

TeamManagement::TeamManagement()
{

}

std::vector<Player> midfielder(8);
//errors start occurring on line below: 'midfielder' does not name a type
midfielder.at(0) = Midfielder("Default ",0,"Midfielder");
midfielder.at(1) = Midfielder("David Armitage ",1,"Midfielder");
midfielder.at(2) = Midfielder("Tom Rockliff ",2,"Midfielder");
midfielder.at(3) = Midfielder("Gary Ablett ",3,"Midfielder");
midfielder.at(4) = Midfielder("Dyson Heppel ",4,"Midfielder");
midfielder.at(5) = Midfielder("Scott Pendlebury",5,"Midfielder");
midfielder.at(6) = Midfielder("Michael Barlow ",6,"Midfielder");
midfielder.at(7) = Midfielder("Jack Steven ",7,"Midfielder");
//errors stop occurring here

void TeamManagement::Display_Players(){

cout<<"Position Name ID"<<endl;

for (int i=1;i<8;i++)
{
cout<<midfielder[i].Player_Details()<<" "<<midfielder[i].Get_player_id()<<endl;
}
}

最佳答案

第一个问题是您不能像在函数外部那样执行赋值。您必须使用构造或初始化。

使用 C++98,您不能在函数外部填充/初始化 vector 。

使用 C++11/14,您可以使用初始化语法填充一个:

#include <iostream>
#include <vector>

struct Thing {
int m_i, m_j;
Thing(int i, int j) : m_i(i), m_j(j) {}
};

std::vector<Thing> things {
{ 1, 2 }, { 2, 3 }
};

int main() {
std::cout << "things[0].m_j = " << things[0].m_j << '\n';
}

但是 std::vector 不会喜欢您尝试将“Midfielder”放入 Player 的 vector 中。让我们使用 SSCCE 来重建您正在造成的损害:

#include <iostream>

struct Base {
int i;
};

struct Derived : public Base {
int j;
};

int main() {
std::cout << "Base size = " << sizeof(Base) << '\n';
std::cout << "Derived size = " << sizeof(Derived) << '\n';
}

这告诉我们 Base 和 Derived 的大小不同。但是您试图将这两个对象放入同一个容器中,因为它们是相关的。圆钉和方钉是相关的...它们无法放入同一个孔中,这就是我们现在遇到的问题。

vector 根据您提供的类型在内存中为您的元素创建空间,然后它要求您传递给它的确切类型以填充这些空间,或者是具有存储类型转换机制的类型。

如果你想要一个不同类型的容器,你需要使用指针,但是你会遇到一个问题,你得到的将是一个指向基类型的指针,你将需要为自己提供一种区分不同玩家类型的方法。

参见 Store derived class objects in base class variables对于 C++98 方法。在现代 C++(11 和 14)中,您应该使用智能指针,例如

std::vector<std::unique_ptr<Base>>

std::vector<std::shared_ptr<Base>>

关于c++ - 在类中的函数外部归档 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30480367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com