#Missing subscript error with templates

54 messages · Page 1 of 1 (latest)

steep bolt
#

this constructor get's an error missing subscript:

BasicMatrix(T InMatrix[Size_Y][Size_X]) {
            _1_MatrixData = new T[Size_X * Size_Y]; 

            for (unsigned long long i = 0; i < Size_Y; i++) {
                for (unsigned long long j = 0; j < Size_X; j++) {
                    _1_MatrixData[i * Size_X + j] = InMatrix[i][j];
                }
            }


        }

Size_X and Size_Y are template arguments

template<size_t Size_X, size_t Size_Y, typename T, bool EnableIntrinsics = false>
    class BasicMatrix {
hardy hingeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

raw cloak
#

_1_MatrixData <- this is bad

#

some names beginning with underscores are reserved by the spec, so as a rule of thumb, don't do this, use a trailing _ if you wish instead, that's fine, like MatrixData_1_

#

but I don't see much of a reason to use leading nor trailing _ there, it's a member var, and your param name is already different, so there's no conflict

steep bolt
raw cloak
#

also, as written, this is a memory leak

raw cloak
#

yes, it's true, that not all names beginning with underscore are forbidden, by why tempt fate?

steep bolt
raw cloak
#

not enough information, send a minimal reproducible example

steep bolt
raw cloak
#

good, but neither minimal nor reproducible

steep bolt
raw cloak
#

no it's not

#

not in the sense that the SO guide suggests

#

it uses OpenGL etc, I have no idea how to build or if your build even works, I'm not going to bother

#

also it's not minimal, it does not include exactly and only the relevant details to demonstrate the problem, in a reproducible way, and nothing more

#

maybe someone else wants to try your build and wade through your full code, but I don't

steep bolt
raw cloak
#

it seems you're missing the point

steep bolt
#

most files are independent of eachother

raw cloak
#

but, you know, good luck, maybe someone else will help

steep bolt
#

and for minimal, I gave minimal example in the original question. IDK where the problem is so IDK how to reproduce it minimally

raw cloak
versed matrixBOT
#
Compiler Output
<source>: In function 'int main()':
<source>:3:1: error: a template declaration cannot appear at block scope
    3 | template<size_t Size_X, size_t Size_Y, typename T, bool EnableIntrinsics = false>
      | ^~~~~~~~
<source>:7:2: error: expected '}' at end of input
    7 | }
      |  ^
<source>:2:16: note: to match this '{'
    2 | int main(void) {
      |                ^
Build failed
raw cloak
#

if it was in line with the SO guide, I could do that ☝️

steep bolt
#
        template<size_t Size_X, size_t Size_Y, typename T, bool EnableIntrinsics = false>
    class BasicMatrix {
    public:
        BasicMatrix(T InMatrix[Size_Y][Size_X]) {
            _1_MatrixData = new T[Size_X * Size_Y]; 

            for (unsigned long long i = 0; i < Size_Y; i++) {
                for (unsigned long long j = 0; j < Size_X; j++) {
                    _1_MatrixData[i * Size_X + j] = InMatrix[i][j];
                }
            }


        }
                ~BasicMatrix() {
            delete _1_MatrixData;
        }
         }
raw cloak
#

but obviously that doesn't work for, you know, obvious reasons

versed matrixBOT
#
Compiler Output
<source>: In function 'int main()':
<source>:3:1: error: a template declaration cannot appear at block scope
    3 | template<size_t Size_X, size_t Size_Y, typename T, bool EnableIntrinsics = false>
      | ^~~~~~~~
Build failed
raw cloak
#

still nope

#

like yes, this probably requires that you expend effort to create a minimal reproducible example

#

if you are willing to spend that effort, I'm willing to help

steep bolt
raw cloak
#

well yes, that's what happens when you get more than a few lines of code

#

and that's why you create an MRE

steep bolt
#

ok, now there is really something weird, I deleted the whole main function to try and strip down the class and it worked

raw cloak
#

because, for example, by trying to create an MRE, you have discovered where the problem probably is not

steep bolt
#

and I just did ctrl+z until everything was back in place as it was before, and the error dissapeared

raw cloak
#

and half the time, creating an MRE, you will find your own problem and therefor you don't need our help

#

but then if you still need help, you have something that is reasonable to ask us to look at and help with

steep bolt
#

So nothing is changed, error is not there

raw cloak
#

indeed, if you cannot reproduce an error, it is difficult to diagnose

#

maybe you forgot to save a file? idk, I still do that sometimes

steep bolt
raw cloak
steep bolt
#

this is the prime example of a meme I saw a while a back. How we programmers have 2 moods. "it doesn't work, why?" and "it works... why?"

#

anyways, thx

#

!solved