Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3724592
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T06:41:48+00:00 2026-05-19T06:41:48+00:00

I’m trying to create my own container for an array of any dimension for

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T06:41:49+00:00Added an answer on May 19, 2026 at 6:41 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.