英文:
Class template with multi-dimensional std::vectors
问题
我正在尝试初始化一个接受任何类型的2D向量作为参数输入的Matrix类模板:
```cpp
#include <iostream>
#include <vector>
template <class T>
class Matrix
{
std::vector<std::vector<T>> entries;
public:
// Constructor with parameters
Matrix(std::vector<std::vector<T>> Entries) {
entries = Entries;
}
};
int main()
{
std::vector<std::vector<double>> current_entries = {
{1, 2, 3}, {-4, 3, 6}, {-7, 8, 9}
};
Matrix<std::vector<std::vector<double>>> M1(current_entries); // line 19
return 0;
}
但是这在第19行给出了错误:
error: no matching function for call to
'Matrix<std::vector<std::vector<double> > >::Matrix(std::vector<std::vector<double> >&)'
我怀疑这与类型是std::vector
有关,因为当我将所有的2D向量替换为int
时,而不改变其他任何内容,它就可以工作:
#include <iostream>
#include <vector>
template <class T>
class Matrix
{
T entries;
public:
// Constructor with parameters
Matrix(T Entries) {
entries = Entries;
}
};
int main()
{
double current_entries = 3;
Matrix<double> M1(current_entries); // line 19
return 0;
}
英文:
I am trying to initialize a Matrix-class template, that takes a 2D-vector of any type as a parameter input:
#include <iostream>
#include <vector>
template <class T>
class Matrix
{
std::vector<std::vector<T>> entries;
public:
// Constructor with parameters
Matrix(std::vector<std::vector<T>> Entries) {
entries = Entries;
}
};
int main()
{
std::vector<std::vector<double>> current_entries = {
{1, 2, 3}, {-4, 3, 6}, {-7, 8, 9}
};
Matrix<std::vector<std::vector<double>>> M1(current_entries); // line 19
return 0;
}
But this gives the error at line 19:
error: no matching function for call to
'Matrix<std::vector<std::vector<double> > >::Matrix(std::vector<std::vector<double> >&)'
I suspect it has something to do with the type being a std::vector
, because when I replace all the 2D-vectors with int
s, without changing anything else it works:
#include <iostream>
#include <vector>
template <class T>
class Matrix
{
T entries;
public:
// Constructor with parameters
Matrix(T Entries) {
entries = Entries;
}
};
int main()
{
double current_entries = 3;
Matrix<double> M1(current_entries); // line 19
return 0;
}
答案1
得分: 2
I suspect it has something to do with the type being a vector
, [...]
我怀疑这与类型为vector
有关,[...]
The error has nothing to do with the type being std::vector
, rather you are instantiating the class with the wrong type.
错误与类型为std::vector
无关,而是您正在使用错误的类型来实例化类。
Your class template argument at the line
您在以下行的类模板参数
Matrix<std::vector<std::vector<double>>> M1(current_entries);
Matrix<std::vector<std::vector<double>>> M1(current_entries);
is std::vector<std::vector<double>>
, not double
.
是std::vector<std::vector<double>>
,而不是double
。
Therefore, you instantiate a Matrix
with entries
of the following type:
因此,您使用以下类型来实例化Matrix
:
std::vector<std::vector< std::vector<std::vector<double>> >> entries;
std::vector<std::vector< std::vector<std::vector<double>> >> entries;
Obviously, this is not what you want. You need just Matrix<double>
, by which the entries
will be with the type std::vector<std::vector<double>>
.
显然,这不是您想要的。您只需要Matrix<double>
,这样entries
的类型将为std::vector<std::vector<double>>
。
英文:
>I suspect it has something to do with the type being a vector
, [...]
The error has nothing to do with the type being std::vector
, rather you are instantiating the class with the wrong type.
Your class template argument at the line
Matrix<std::vector<std::vector<double>>> M1(current_entries);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ --> is the "T"
is std::vector<std::vector<double>>
, not double
. Therefore, you instantiate a Matrix
with entries
of the following type:
std::vector<std::vector< std::vector<std::vector<double>> >> entries;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ----> "T"
Obviously, this is not what you want. You need just Matrix<double>
, by which the entries
will be with the type std::vector<std::vector<double>>
.
答案2
得分: 0
以下是您要翻译的内容:
错误的原因已经在注释中解释过了。T=vector<vector<float>>
将扩展为 vector<vector<vector<vector<float>>>>
,我相信这不是您想要的结果。
您可以保留以下通用解决方案,然后 T 可以是您想要的矩阵
#include <iostream>
#include <vector>
template <class T>
class Matrix{
T entries;
public:
Matrix(T Entries) { // Constructor with parameters
entries = Entries;
}
};
using namespace std;
int main()
{
std::cout << "Let's Start...." << std::endl;
std::vector<std::vector<double>> current_entries ={{1, 2, 3}, {-4, 3, 6}, {-7, 8, 9}};
Matrix<std::vector<std::vector<double>>> M1(current_entries); // line 19
std::cout << "All is well " << std::endl;
return 0;
}
或者,您还可以为 vector<vector<T>>
添加模板部分特化,并可以提供以下单独的实现。
#include <iostream>
#include <vector>
template <class T>
class Matrix {
T entries;
public:
Matrix(T Entries) : entries(Entries) {
// Constructor with parameters
}
};
// Partial specialization for std::vector<std::vector<T>>
// It should always be implemented after above template
template <class T>
class Matrix<std::vector<std::vector<T>>> {
std::vector<std::vector<T>> entries;
public:
Matrix(std::vector<std::vector<T>> Entries) : entries(Entries) {
// Constructor with parameters
}
};
int main() {
std::cout << "Let's Start...." << std::endl;
std::vector<std::vector<double>> current_entries = {{1, 2, 3}, {-4, 3, 6}, {-7, 8, 9}};
Matrix<std::vector<std::vector<double>>> M1(current_entries);
std::cout << "All is well " << std::endl;
return 0;
}
希望这有所帮助。如果您需要更多关于任何解决方案的说明,请在评论中提出。
英文:
Reason for error is already explained in comments. T=vector<vector<float>>
will expand entries to vector<vector<vector<vector<float>>>>
and I believe that is not the outcome you want.
You can keep generic solution as below and then T can be your desired matrix
#include <iostream>
#include <vector>
template <class T>
class Matrix{
T entries;
public:
Matrix(T Entries) { // Constructor with parameters
entries = Entries;
}
};
using namespace std;
int main()
{
std::cout << "Lets's Start...." << std::endl;
std::vector<std::vector<double>> current_entries ={{1, 2, 3}, {-4, 3, 6}, {-7, 8, 9}};
Matrix<std::vector<std::vector<double>>> M1(current_entries); // line 19
std::cout << "All is well " << std::endl;
return 0;
}
Alternatively you can also have Template Partial Specialisation for vector<vector<T>>
and a separate implementation can be provided as below.
#include <iostream>
#include <vector>
template <class T>
class Matrix {
T entries;
public:
Matrix(T Entries) : entries(Entries) {
// Constructor with parameters
}
};
// Partial specialization for std::vector<std::vector<T>>
// It should always be implemented after above template
template <class T>
class Matrix<std::vector<std::vector<T>>> {
std::vector<std::vector<T>> entries;
public:
Matrix(std::vector<std::vector<T>> Entries) : entries(Entries) {
// Constructor with parameters
}
};
int main() {
std::cout << "Let's Start...." << std::endl;
std::vector<std::vector<double>> current_entries = {{1, 2, 3}, {-4, 3, 6}, {-7, 8, 9}};
Matrix<std::vector<std::vector<double>>> M1(current_entries);
std::cout << "All is well " << std::endl;
return 0;
}
Hope this helps. If you need more description about any of the solution, please ask in a comment.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论