user1725544
user1725544

Reputation: 21

Initialising float matrix inside function

I got this problem when trying to initialize global c++ matrix(2D array) inside a function:

here is what I'm doing

#include <iostream>
#include <math.h>
#include <Windows.h>

using namespace std;

float matrix[5][5];

void setIR(){
    matrix[5][5]= {
        { 17.2, 22.75, 2.5, -9.15, 0.2},
        { 22.75, 145.5, 9.25, 20.75, 5.25 },
        { 2.5, 9.25, 76.5, -15.5, -6.0 },
        { -9.15, 20.75, -15.5, 37.3, -25.65 },
        { 0.2, 5.25, -6.0, -25.65, 41.2 }
        };

int main(){
//rest of the code
......
}

I'm getting only bunch of

1>c:\users\apple\documents\visual studio 2010\projects\jcb\jcb\jcbPIO.cpp(46): error C2059: syntax error : '{'
1>c:\users\apple\documents\visual studio 2010\projects\jcb\jcb\jcbPIO.cpp(46): error C2143: syntax error : missing ';' before '{'
1>c:\users\apple\documents\visual studio 2010\projects\jcb\jcb\jcbPIO.cpp(47): error C2143: syntax error : missing ';' before '}'
1>c:\users\apple\documents\visual studio 2010\projects\jcb\jcb\jcbPIO.cpp(47): error C2143: syntax error : missing ';' before ','
1>c:\users\apple\documents\visual studio 2010\projects\jcb\jcb\jcbPIO.cpp(48): error C2143: syntax error : missing ';' before '{'
1>c:\users\apple\documents\visual studio 2010\projects\jcb\jcb\jcbPIO.cpp(48): error C2143: syntax error : missing ';' before '}'
1>c:\users\apple\documents\visual studio 2010\projects\jcb\jcb\jcbPIO.cpp(48): error C2143: syntax error : missing ';' before ','
1>c:\users\apple\documents\visual studio 2010\projects\jcb\jcb\jcbPIO.cpp(49): error C2143: syntax error : missing ';' before '{'
1>c:\users\apple\documents\visual studio 2010\projects\jcb\jcb\jcbPIO.cpp(49): error C2143: syntax error : missing ';' before '}'
1>c:\users\apple\documents\visual studio 2010\projects\jcb\jcb\jcbPIO.cpp(49): error C2143: syntax error : missing ';' before ','
1>c:\users\apple\documents\visual studio 2010\projects\jcb\jcb\jcbPIO.cpp(50): error C2143: syntax error : missing ';' before '{'
1>c:\users\apple\documents\visual studio 2010\projects\jcb\jcb\jcbPIO.cpp(50): error C2143: syntax error : missing ';' before '}'
1>c:\users\apple\documents\visual studio 2010\projects\jcb\jcb\jcbPIO.cpp(50): error C2143: syntax error : missing ';' before ','
1>c:\users\apple\documents\visual studio 2010\projects\jcb\jcb\jcbPIO.cpp(51): error C2143: syntax error : missing ';' before '{'
1>c:\users\apple\documents\visual studio2010\projects\jcb\jcb\jcbPIO.cpp(51): error C2143: syntax error : missing ';' before '}'

I'm doing something wrong or C++ limitation ?

Upvotes: 1

Views: 208

Answers (2)

Peter Alexander
Peter Alexander

Reputation: 54290

You can only use the {} initialiser syntax on initialisation, and you can only initialise a global variable at its definition.

For this situation, you could initialise a different matrix and then just copy the contents:

void setIR(){
    static const float init[5][5]= {
        { 17.2, 22.75, 2.5, -9.15, 0.2},
        { 22.75, 145.5, 9.25, 20.75, 5.25 },
        { 2.5, 9.25, 76.5, -15.5, -6.0 },
        { -9.15, 20.75, -15.5, 37.3, -25.65 },
        { 0.2, 5.25, -6.0, -25.65, 41.2 }
    };
    memcpy(matrix, init, sizeof(matrix));
}

If you're not a fan of things like memcpy then you could manually copy by looping, or try to use the STL, but in my opinion this is the simplest and shortest solution in this case.

Upvotes: 2

celtschk
celtschk

Reputation: 19731

This "list assignment" syntax only works for initialization (that is, at the place where you define the variable), not for assignment. What your assignment statement tries to do is to assign to the single element matrix[5][5] (which doesn't actually exist) a values given by the curly braces list, which isn't a valid syntax for a value.

You can assign the matrix as follows:

float matrix[5][5];

void setIR()
{
  static float const values[5][5]= {
        { 17.2, 22.75, 2.5, -9.15, 0.2},
        { 22.75, 145.5, 9.25, 20.75, 5.25 },
        { 2.5, 9.25, 76.5, -15.5, -6.0 },
        { -9.15, 20.75, -15.5, 37.3, -25.65 },
        { 0.2, 5.25, -6.0, -25.65, 41.2 }
        };
  for (int i = 0; i < 5; ++i)
    for (int j = 0; j < 5; ++j)
      matrix[i][j] = values[i][j];
}

Of course, given that your values are constants anyway, the best solution is to just give them in the definition of matrix itself. That also saves the time to copy all the data.

Upvotes: 0

Related Questions