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 7585179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:06:48+00:00 2026-05-30T19:06:48+00:00

I haven’t worked with c++ in a while, but I just started a project

  • 0

I haven’t worked with c++ in a while, but I just started a project with it. This may not be possible, but Im trying to create a template class with an array that sets its size to the value of a constant which i’m trying to set with the constructor.

This is the code of the constructor:

Tarray(int s): start_size(s){
    }

This is the code that sets the array size:

const int start_size;
T this_array[start_size];

This is the entire file:

#ifndef TARRAY_H_
#define TARRAY_H_


template<typename T>
class Tarray {
private:
    const int start_size;
    T this_array[start_size];
    int array_size;
public:
    Tarray(int s): start_size(s){
    }
    ~Tarray(){
        delete[] this_array;
    }
    T & operator[](int i){
        return this_array[i];
    }
};



#endif /* TARRAY_H_ */

These are the errors I get:

..\/template_array/Tarray.h:16:24: error: 'Tarray<T>::start_size' cannot appear in a constant-expression
..\/template_array/Tarray.h:16:34: error: 'new' cannot appear in a constant-expression
..\/template_array/Tarray.h:16:34: error: ISO C++ forbids initialization of member 'this_array' [-fpermissive]
..\/template_array/Tarray.h:16:34: error: making 'this_array' static [-fpermissive]
..\/template_array/Tarray.h: In instantiation of 'Tarray<Person>':
..\Human.cpp:17:24:   instantiated from here
..\/template_array/Tarray.h:16:34: error: invalid in-class initialization of static data member of non-integral type 'Person*'
Build error occurred, build is stopped
Time consumed: 343  ms. 

The error messages have been changing as I try to tweak the code, but these are the errors from this particular build.

Thanks for any help

  • 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-30T19:06:50+00:00Added an answer on May 30, 2026 at 7:06 pm

    The reason you’re getting compiler errors is this line:

    T this_array[start_size];
    

    This line would make your Tarray actually contain start_size instances of T. It wouldn’t hold a pointer or reference to these instances – they would be part of same block of memory that contains the Tarray’s other instance variables.
    This would make the class’ size depend on start_size, and start_size is not known at compile time. The size of any C++ class must be known at compile time, this isn’t possible.

    There are two ways to solve this:

    1. Allocate the array of T instances on the heap, using array new. This is what std::vector does. Writing such a class and getting it to behave right when it’s copied/moved/expanded/etc is difficult and tedious, so I’d recommend just using std::vector instead.
    2. Make the number of T instances fixed, and pass it as a template parameter

    i.e.:

    template<typename T, std::size_t N>
    class TArray
    {
        ...
        T this_array[N];
        ...
    }
    

    This is what std::array (C++11 only) and boost::array do. Again, I’d recommend using one of these instead of writing your own. Unless this is homework, of course…

    Lastly, it’s worth noting that this is an error:

    ~Tarray(){
        delete[] this_array;
    }
    

    this_array wasn’t allocated with new, so you shouldn’t delete it. If the array is part of the class as it is here (rather than being separately heap-allocated and owned by the class), then it will be destroyed along with the rest of the class by default. Calling delete is not only unnecessary, it will almost certainly cause a crash.

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

Sidebar

Related Questions

I haven't found any documentation on this or seen this done before, but is
I haven't worked with SQL Reporting much, however I have been trying to get
Haven't touch javascript for 3 years. Just got a javascript project and wanted to
Haven't seen anything, but I have seen that you can create albums in the
I haven't yet worked out a specific case. But I am about to embark
I haven't found any information on MSDN regarding this problem. If we create an
I haven't been able to find much about this but am I the only
Haven't been coding in a bit, but took on a project for a friend.
Haven't fired up reflector to look at the difference but would one expect to
Haven't seen many Geneva related questions yet, I have posted this question in the

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.