我從向量繼承了我的類,我希望能夠將串列分配給我的類,比如向量。
我的代碼如下:
#include <vector>
using namespace std;
template<typename T>
class Matrix
: public vector<vector<T>>
{
public:
Matrix( vector<vector<T>> && m )
: vector<vector<T>>( m )
{}
// Tried this approach, but it doesn't work
// Matrix(std::initializer_list<std::initializer_list<T>> l){
// }
}
int main()
{
Matrix<int> m({{0, 1}, {2, 3}}); // it works
// Matrix<int> m = {{0, 1}, {2, 3}}; // error: no instance of constructor "Matrix<T>::Matrix [with T=int]" matches the argument list -- argument types are: ({...}, {...})
}
uj5u.com熱心網友回復:
只需將std::vector建構式帶到您的類范圍:
template <typename T> class Matrix : public vector<vector<T>> {
public:
using vector<vector<T>>::vector;
};
https://godbolt.org/z/b3bdx53d8
題外話:繼承對您的情況來說是不好的解決方案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/472145.html
下一篇:在C 中宣告雙精度陣列
