模板类构造函数问题 - 为多维数组设计容器

发布于 2024-10-11 12:25:25 字数 1766 浏览 2 评论 0原文

我正在尝试为任意维度的数组创建自己的容器以进行数值计算。我想使用模板来执行此操作,以便我可以重载下标运算符 [],以便它像普通数组和向量一样工作,例如访问像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

霓裳挽歌倾城醉 2024-10-18 12:25:25

您的构造函数使用表达式 new T[size],这要求 T 是默认可构造的(如果 T 是类类型)。

您需要执行以下操作:分配原始内存(例如使用operator new)并使用放置new表达式“就地”构造T实例。或者,您可以只为容器提供一个默认构造函数。

Your constructor uses the expression new T[size] and this requires T to be default constructible (if T is a class type).

You need to do something like: allocate raw memory (e.g. using operator new) and construct T instances "in place" using a placement new expression. Alternatively, you could just give container a default constructor.

痴梦一场 2024-10-18 12:25:25

你缺少很多构造函数。您至少还需要一个默认构造函数和一个复制构造函数,operator=

You are missing a lot of constructors. You need a default constructor and a copy constructor at the very least, operator= too.

能否归途做我良人 2024-10-18 12:25:25
  • 为容器提供一个不带参数的默认构造函数。看来您想让容器不可调整大小。
  • 您可以将容器的尺寸设为模板参数并忽略 T initVal
  • 如果您这样做,您可能甚至不需要使用 new[] 和 delete[] 但如果您这样做,您需要完整的“3 规则”复制并作出安排,妥善落实。
  • 您可能会发现 operator[] 的 const 重载也很有用。
  • Give container a default constructor, that takes no parameters. It looks like you want to make your container non-resizable though.
  • You could make the dimension of container a template parameter and ignore the T initVal
  • If you do that you probably don't even need to use new[] and delete[] but if you do you need the full "rule of 3" of copying and assigning to be properly implemented.
  • You might find a const overload of operator[] useful too.
原谅我要高飞 2024-10-18 12:25:25

请参阅

或查看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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文