[英]How do I capture this struct vector? c++
So I've made a class, one of it's function returns a struct vector, like so: 所以我做了一个类,其中一个函数返回一个结构向量,如下所示:
vector<highscore::players> highscore::returnL(){
load();
return list;
}
So list is basically, 所以清单基本上是
struct players {
string name;
int score;
};
vectors<players> list;
In my source cpp, I tried to capture this vector, so I made another struct and struct vector. 在我的源cpp中,我尝试捕获此向量,因此我制作了另一个struct和struct向量。 Source.cpp: Source.cpp:
struct players1 {
string name;
int score;
};
vector<players1> highscorelist;
Then I tried to 然后我试图
highscore high; //class' name is highscore
highscorelist = high.returnL();
But I get the error message: 但是我收到错误消息:
No operator "=" matches these operands
" operand types are std::vector<players1, std::allocator<players1>> = std::vector<highscore::players, std::allocator<highscore::players>> "
Is it not possible to do it this way? 这样行不行吗? I honestly don't know what to search for so this might have been answered before, apologize if that's the case. 老实说,我不知道要搜索什么,所以以前可能已经回答了,如果是这样的话,对不起。
You could use reinterpret_cast
, but that's not a good solution. 您可以使用reinterpret_cast
,但这不是一个好的解决方案。 Why don't you use highscore::player
? 为什么不使用highscore::player
?
std::vector<highscore::player> highscoreList;
highscoreList = high.returnL(); // ok
highscore::player
and player1
are different types, even though they have the same variables and probably even the same memory layout. highscore::player
和player1
是不同的类型,即使它们具有相同的变量甚至可能是相同的内存布局。 You cannot just interchange types like that. 您不能仅仅交换那样的类型。 Also, if you change one of those types, you have to change the other, which is just a maintenance nightmare if it were possible. 另外,如果更改其中一种类型,则必须更改另一种,如果可能的话,这只是维护方面的噩梦。
If you can, you could also use auto
: 如果可以,也可以使用auto
:
auto highscoreList = high.returnL();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.