Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何为对象的向量使用setter_C++_Oop_Pointers_Vector_Std - Fatal编程技术网

C++ 如何为对象的向量使用setter

C++ 如何为对象的向量使用setter,c++,oop,pointers,vector,std,C++,Oop,Pointers,Vector,Std,我做错了什么?我想在不改变std::vector游戏的情况下解决这个问题 i.setGames(gameSet); 需要setGames将指针指向std::vector。这意味着下面的函数签名 i.setGames(&gameSet); void讲师::设置游戏(标准::向量*值); 您拥有的是简单std::vector void讲师::设置游戏(const std::向量和值); 很明显,它们是不同的类型,你有错误 您只需const std::vector&value vo

我做错了什么?我想在不改变
std::vector游戏的情况下解决这个问题

 i.setGames(gameSet);
需要
setGames
将指针指向
std::vector
。这意味着下面的函数签名

i.setGames(&gameSet);
void讲师::设置游戏(标准::向量*值);
您拥有的是简单
std::vector

void讲师::设置游戏(const std::向量和值);
很明显,它们是不同的类型,你有错误


您只需
const std::vector&value

void Instructor::setGames(const std::vector<Game*> &value);
void讲师::设置游戏(const std::向量和值)
{
games=value;//同时将游戏类型更改为std::vector
}
大体上

void Instructor::setGames(const std::vector<Game>& value)
{
    games = value; // also change the type of games  to be std::vector<Game>
}
游戏g1、g2、g3;
std::向量游戏集{g1,g2,g3};
i、 setGames(游戏集);

假设你有一个
游戏
指针向量,这是它将接受的对象类型,因此
g1
g2
g3
必须是指针:

  Game g1, g2, g3;
  std::vector <Game> gameSet{ g1, g2, g3};
  i.setGames(gameSet);
游戏*g1、*g2、*g3;
向量博弈集;
g1=新游戏()//初始化指针
g2=新游戏();
g3=新游戏();
//...
i、 setGames(游戏集);

注意,现在使用原始指针并不是一个很好的实践,最好使用智能指针,仔细看看


std::vector
std::vector*
是两种不同的类型,但是当我更改
std::vector游戏集时
,即使我使用了一个参考@Petert,我在推回时也会出错。问题是什么是
游戏
,是
std::vector
还是
std::vector
std::vector游戏@Anastaciu不要使用原始指针,使用共享的\u ptrIs如果不更改``std::vector games;```就无法修复此问题@FSJ好的,下面是答案。以及“i.setGames(gameSet)”<代码>。但是你不需要
std::vector`。这给了我一个分段错误核心(dump)@FSJ,我给你做了一个现场演示,添加到答案中,希望有帮助。
i.setGames(&gameSet);
void Instructor::setGames(std::vector<Game> *value);
void Instructor::setGames(const std::vector<Game*> &value);
void Instructor::setGames(const std::vector<Game>& value)
{
    games = value; // also change the type of games  to be std::vector<Game>
}
  Game g1, g2, g3;
  std::vector <Game> gameSet{ g1, g2, g3};
  i.setGames(gameSet);
Game *g1, *g2, *g3;
std::vector <Game*> gameSet;

g1 = new Game(); //intialize pointers
g2 = new Game();
g3 = new Game();
//...
i.setGames(gameSet);