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

  • Home
  • SEARCH
  • 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 6542177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:11:39+00:00 2026-05-25T11:11:39+00:00

Is there a way to initialize an array like so: static const vec3d<long> XI[Q]

  • 0

Is there a way to initialize an array like so:

static const vec3d<long> XI[Q] = {
     vec3d<long>( 0, 0, 0 ),

 vec3d<long>(-1, 0, 0 ),  vec3d<long>( 0,-1, 0 ),  vec3d<long>( 0, 0,-1 ),
 vec3d<long>(-1,-1, 0 ),  vec3d<long>(-1, 1, 0 ),  vec3d<long>(-1, 0,-1 ), [etc]
};

where

00039 template<typename TYPE>
00040 class vec3d : public vec<TYPE>{
00041 public:
00042 
00049         vec3d() : vec<TYPE>( 0, 3 ){};
00057         vec3d( TYPE right ) : vec<TYPE>( right, 3 ){};
00065         vec3d( TYPE X_val, TYPE Y_val, TYPE Z_val ) : vec<TYPE>( 0, 3 ){
00066                 this->val[0] = X_val;
00067                 this->val[1] = Y_val;
00068                 this->val[2] = Z_val;
00069         };
00077         vec3d( vec3d<TYPE>& right ) : vec<TYPE>( 0, 3 ){
00078                 this->val[0] = right[0];
00079                 this->val[1] = right[1];
00080                 this->val[2] = right[2];
00081         }; [etc] };

and

    00040 template<typename TYPE>
    00041 class vec{
    00042 public:
    00047         TYPE *val;
    00052         int dimension;
    00053 public:
    00060         vec();
    00066         vec( TYPE right );
    00073         vec( TYPE right, int _dimension );
    00081         vec( vec<TYPE> &right ); 
    00082 
    00087         ~vec();
    00088 
    00089 
    00090         TYPE& operator[]( int right);
    00091         vec<TYPE>& operator=( TYPE right );
    00092         vec<TYPE>& operator=( vec<TYPE> &right );
[etc] };

Source is:

00049 template<typename TYPE>
00050 vec<TYPE>::vec( TYPE right, int _dimension ){
00051         dimension = _dimension;
00052         val = new TYPE[_dimension];
00053         assert( val );
00054         for( int i = 0; i < dimension; i++ ) val[i] = right;
00055 
00056 };

00075 template<typename TYPE>
00076 TYPE& vec<TYPE>::operator[]( int right ){
00077         assert( ( right < dimension ) );
00078         assert( right >= 0 );
00079         assert( val );
00080         return val[right];
00081 };

are constructors. Q is declared “static const int”, so it should fulfil C++ standard of being non-variable, right?

Compiler says:
error: no matching function for call to ‘albm::vec3d::vec3d(albm::vec3d)’
vec3d.h:77:2: note: candidates are: albm::vec3d::vec3d(albm::vec3d&) [with TYPE = long int]

Obviously there is the problem, that I can’t pass vec3d& here. Is there some workaround? Defining every single vector first seems to be a solution. Would be a hazzle though…

And sorry for my stupid question…maybe this thread exists somewhere, but I didn’t find it. Probably this issue has some special name I don’t know – therefore I can’t google it!
“extended initializer list”, “class array initialisation” and such didn’t do the trick…

SOLUTION: some postprocessing here…maybe so. else encounters the same prob:
The copy constructor lacked a “const”:

00077         vec3d( const vec3d<TYPE>& right ) : vec<TYPE>( 0, 3 ){
00078                 this->val[0] = right.val[0];
00079                 this->val[1] = right.val[1];
00080                 this->val[2] = right.val[2];

Further I can’t access right[] directly – my guess for a reason would be the template-style – but right.val[] does the trick!

  • 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-25T11:11:40+00:00Added an answer on May 25, 2026 at 11:11 am

    Your copy-constructor is not correctly declared –
    the toCopyFrom parameter needs to be const:

    vec3d<TYPE>( const vec3d<TYPE>& toCopyFrom)
    

    Edit:

    Now, on your second problem regarding the subscript operator:

    The subscript operator operator[]() in your base class does not have const (i.e. read-only) semantics. In your copy constructor, you were trying to access values in right through that subscript operator. Now, that right parameter is declared const, which basically means that you are not allowed to change the internal state of right. Since your operator[]() function is not const, the language’s assumption is that the function could mutate/alter the state of right – which conflicts with right‘s const-ness. Indeed, the function actually returns a reference to the internal member variables of the class, which would allow a client of that code to change the internal state of right!

    The solution is to provide an additional, const, subscript operator in the base class:

    const TYPE& operator[]( int right) const
    

    Note – personally, I’d rename right to index – for clarity.

    The trailing const on the above function signature provides it with const access semantics, which allows you to call – for instance – x = right[n] when right is const. The leading const on the return type just means that the returned TYPE reference will be const, which prevents you doing right[n] = x when right is const. Note – when you add the function, its implementation (body) of the function should probably look the same as for the existing subscript operator. You did not provide that, so I cannot write it.

    In conclusion, I would suggest you read up on const and ‘const-correctness’ topics.

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

Sidebar

Related Questions

Is there an elegant way to create and initialize a const std::vector<const T> like
Is there more elegant way(LINQ or other) to initialize array like this? int[] result
Is there a way to initialize all elements of an array to a constant
Is there are an easy way to initialize byte array from portion of existing
Is there a way to declare first and then initialize an array in C?
Is it possible to initialize a static const empty array, please see below code,
Is there a way to do something like the array initialization braces method for
Is there a particular way to initialize an IList<T> ? This does not seem
is there way how to get name ov event from Lambda expression like with
I want to initialize a static collection within my C# class - something like

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.