1

尝试在函数内初始化全局 c++ 矩阵(二维数组)时遇到了这个问题:

这就是我正在做的

#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
......
}

我只得到一堆

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 '}'

我做错了什么或 C++ 限制?

4

2 回答 2

2

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.

于 2012-10-06T18:10:06.310 回答
0

这种“列表赋值”语法仅适用于初始化(即在您定义变量的地方),不适用于赋值。您的赋值语句试图做的是将matrix[5][5]花括号列表给出的值分配给单个元素(实际上并不存在),这不是值的有效语法。

您可以按如下方式分配矩阵:

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.

于 2012-10-06T18:09:25.397 回答