模板类构造函数问题 - 为多维数组设计容器
我正在尝试为任意维度的数组创建自己的容器以进行数值计算。我想使用模板来执行此操作,以便我可以重载下标运算符 [],以便它像普通数组和向量一样工作,例如访问像 a[10][10][10] 等条目。
我在获取构造函数时遇到问题在尝试创建容器来保存多维数组时起作用。请帮忙!
#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
using namespace std;
template <class T>
class container{
public:
inline T& operator[](int i){return data[i];}
container(int si, T initval){
size=si;
data=new T[size];
transform(data,data+size,data, [initval] (T d) {return initval;});
// transform fills array with the initial value.
}
~container(){delete [] data;}
private:
T* data;
int size;
};
int main(){
//For example:
vector<vector<int>> v1(10,vector<int>(10,0)); //2D 10x10
vector<vector<vector<int>>> v2(10,vector<vector<int>>(10,vector<int>(10,0)));
//3D 10x10x10
container<int> c1(10,0); //1D 10x1 works!
container<container<int>> c2(10,container<int>(10,0)); //2D 10x10 fails!
system("pause");
return 0;
}
VS10 错误输出:
error C2512: 'container<T>' : no appropriate default constructor available
with
[
T=int
]
c:\users\jack\documents\visual studio 2010\projects\ref\ref\ref.cpp(11) : while compiling class template member function 'container<T>::container(int,T)'
with
[
T=container<int>
]
c:\users\jack\documents\visual studio 2010\projects\ref\ref\ref.cpp(28) : see reference to class template instantiation 'container<T>' being compiled
with
[
T=container<int>
]
Build FAILED.
我知道我可以只使用 valarray 或 boost 库,但我想了解如何创建自己的库。效率很重要。谢谢!
I'm trying to create my own container for an array of any dimension for numerical computing. I would like to do this using templates so that I could overload the subscript operator [] so that it works like normal arrays and vectors e.g. access entries like a[10][10][10] etc.
I am having trouble getting the constructor to work when trying to create containers to hold multidimensional arrays. Please help!
#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
using namespace std;
template <class T>
class container{
public:
inline T& operator[](int i){return data[i];}
container(int si, T initval){
size=si;
data=new T[size];
transform(data,data+size,data, [initval] (T d) {return initval;});
// transform fills array with the initial value.
}
~container(){delete [] data;}
private:
T* data;
int size;
};
int main(){
//For example:
vector<vector<int>> v1(10,vector<int>(10,0)); //2D 10x10
vector<vector<vector<int>>> v2(10,vector<vector<int>>(10,vector<int>(10,0)));
//3D 10x10x10
container<int> c1(10,0); //1D 10x1 works!
container<container<int>> c2(10,container<int>(10,0)); //2D 10x10 fails!
system("pause");
return 0;
}
VS10 error output:
error C2512: 'container<T>' : no appropriate default constructor available
with
[
T=int
]
c:\users\jack\documents\visual studio 2010\projects\ref\ref\ref.cpp(11) : while compiling class template member function 'container<T>::container(int,T)'
with
[
T=container<int>
]
c:\users\jack\documents\visual studio 2010\projects\ref\ref\ref.cpp(28) : see reference to class template instantiation 'container<T>' being compiled
with
[
T=container<int>
]
Build FAILED.
I know I could just use valarray or a boost library, but I would like to understand how to create my own. Efficiency is important. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的构造函数使用表达式 new T[size],这要求
T
是默认可构造的(如果T
是类类型)。您需要执行以下操作:分配原始内存(例如使用
operator new
)并使用放置new
表达式“就地”构造T
实例。或者,您可以只为容器提供一个默认构造函数。Your constructor uses the expression
new T[size]
and this requiresT
to be default constructible (ifT
is a class type).You need to do something like: allocate raw memory (e.g. using
operator new
) and constructT
instances "in place" using a placementnew
expression. Alternatively, you could just givecontainer
a default constructor.你缺少很多构造函数。您至少还需要一个默认构造函数和一个复制构造函数,
operator=
。You are missing a lot of constructors. You need a default constructor and a copy constructor at the very least,
operator=
too.请参阅此
或查看boost::multi_array
您想要做什么:需要提前模板编程技能,因此回顾 boost 将是一个好的开始恕我直言。
see this
or review boost::multi_array<>
What you are trying to do: requires advance template programming skills, therefore reviewing boost will be a good start IMHO.