C++ 如何从向量类派生
请帮助我理解如何从vector创建派生类。据我所知,不鼓励使用标准容器 以下是我的例子:C++ 如何从向量类派生,c++,vector,C++,Vector,请帮助我理解如何从vector创建派生类。据我所知,不鼓励使用标准容器 以下是我的例子: // Example program #include <vector> using namespace std; struct Card { int suit; int value; Card(int pSuit, int pValue) : suit(pSuit), value(pValue) { } }; struct CardVector
// Example program
#include <vector>
using namespace std;
struct Card
{
int suit;
int value;
Card(int pSuit, int pValue) : suit(pSuit), value(pValue)
{
}
};
struct CardVector : public vector<Card>
{
void removeCards(const CardVector &cardArr)
{
// todo
}
};
int main()
{
CardVector cardArr1;
cardArr1.push_back(Card(1,1)); // works
vector<Card> cardArr2{Card(1,1)}; // works
CardVector cardArr3{Card(1,1)}; // doesn't compile
return 0;
}
这就产生了编译错误
In function 'int main()':
30:32: error: no matching function for call to 'CardVector::CardVector(<brace-enclosed initializer list>)'
30:32: note: candidates are:
16:8: note: CardVector::CardVector()
16:8: note: candidate expects 0 arguments, 1 provided
16:8: note: CardVector::CardVector(const CardVector&)
16:8: note: no known conversion for argument 1 from 'Card' to 'const CardVector&'
16:8: note: CardVector::CardVector(CardVector&&)
16:8: note: no known conversion for argument 1 from 'Card' to 'CardVector&&'
您也可以通过使用基类构造函数和其他方法来实现:
#include <iostream>
#include <vector>
#include <string>
struct Card {
std::string name;
};
class CardVector : public std::vector<Card> {
using std::vector<Card>::vector; // <- like so
void removeCards(const CardVector& cardVector) {}
void appendCards(const CardVector& cardVector) {}
};
int main() {
Card a{"A"}, b{"B"};
CardVector cv = {a, b};
for(auto& c : cv) {
std::cout << c.name << "\n";
}
}
。。。但是,由于std::vector的析构函数不是虚拟的,请确保永远不会通过基类指针删除对象
为了避免将来的麻烦,请使用组合并为需要支持的对象创建代理函数
轨道上的亮度竞赛揭示了编译错误:
原因是缺少一个使用std::initializer_列表的构造函数,要修复这个问题,您可以添加一个使用std::initializer_列表作为参数的构造函数,或者像我前面所做的那样重新使用基类ctor。LRiO也是共享的,这当然值得考虑,如果您使用VS.您也可以使用基类构造函数和其他方法:
#include <iostream>
#include <vector>
#include <string>
struct Card {
std::string name;
};
class CardVector : public std::vector<Card> {
using std::vector<Card>::vector; // <- like so
void removeCards(const CardVector& cardVector) {}
void appendCards(const CardVector& cardVector) {}
};
int main() {
Card a{"A"}, b{"B"};
CardVector cv = {a, b};
for(auto& c : cv) {
std::cout << c.name << "\n";
}
}
。。。但是,由于std::vector的析构函数不是虚拟的,请确保永远不会通过基类指针删除对象
为了避免将来的麻烦,请使用组合并为需要支持的对象创建代理函数
轨道上的亮度竞赛揭示了编译错误:
原因是缺少一个使用std::initializer_列表的构造函数,要修复这个问题,您可以添加一个使用std::initializer_列表作为参数的构造函数,或者像我前面所做的那样重新使用基类ctor。LRiO也是共享的,如果您使用VS.的话,这当然值得考虑。一般来说,您不应该从标准容器类派生,而应该将它们封装为成员变量。编写允许在卡片上迭代的函数应该很容易实现。虽然这可能是一个很好的建议,但这不是我应该从容器继承的问题。编译错误问题=>PleaseEvery,非常相关:第一条评论并没有回答我的问题——说一种方法不好并不是一个充分的答案。通常,您不应该从标准容器类派生,而应该将它们封装为成员变量。编写允许在卡片上迭代的函数应该很容易实现。虽然这可能是一个很好的建议,但这不是我应该从容器继承的问题。编译错误问题=>PleaseEvery,非常相关:第一条评论没有回答我的问题-说方法不好是不够的回答,如果你解释原因是缺少使用初始值设定项列表的ctor,并且你可以添加一个ctor,或者按照你展示的方法重复使用基ctor,那就更好了。也要知道,谢谢!我或多或少地复制了你写的内容。如果你解释原因是缺少一个使用初始值设定项的ctor,并且你可以添加一个,或者按照你所展示的方法重复使用基的ctor,那就更好了。也要知道,谢谢!我或多或少地复制了你写的东西。