c++matrixvisual-c++-6

Table of constants in C++


I'm working on a project with a software that uses C++ compiler (VisualStudio 2010 C++). The software is for making numerical model for real electric circuits. I write some codes in different sections in the software, ir's a C++ code but some variables must have some prefixes (SeqInteger instead of Integer, ptVar if Var is the name of a connection in the model, etc ...). The software generates then cpp files with the code I wrote and other sections in it.

The issue : I am trying to initiate a table contaning constant values, to do so, I wrote :

SeqReal A[4];

A[4]={1 , 2, 1 , 3};

However, I am getting this error :

compiling src/SeqClass__cpl.cpp
SeqClass__cpl.cpp
src/SeqClass__cpl.cpp(74) : error C2059: syntax error : '{'
src/SeqClass__cpl.cpp(74) : error C2143: syntax error : missing ';' before '{'
src/SeqClass__cpl.cpp(74) : error C2143: syntax error : missing ';' before '}'
make: *** [WinNT\debug64/SeqClass__cpl.obj] Error 512

I understand that the syntax of my line is wrong, but how should I write it ? It used to work for me like this before.

I hope I made myself clear. I only gave this simple example, but in fact, I am having the same error while trying to initiate matrices of 28*16 shape.

Thank you !


Solution

  • If you want to declare an array, and later assign to it using an initializer list, you could do:

    int i = 0;
    for (int v : {1 , 2, 1 , 3})
      A[i++] = v;