使用Code :: Blocks的C ++程序中的未知问题

时间:2015-01-18 19:50:38

标签: c++ function class codeblocks

我正在尝试调试代码。这很简单,但我被卡住了。

以下是编译器所说的内容:

在功能void initialization()中:

  

第35行错误:预期不合格的ID' - '令牌

以下是代码:

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

class vars
{
public:

    int coins;
    int coins_sec;

    int gtx750ti;  // The amount of GTX750ti owned by the player
    int gtx760;    // The amount of GTX760 owned by the player
    int gtx770;
    int gtx780;
    int gtxtitan;
    int gtxtitanB;
    int gtxtitanZ;
    int r9270;
    int r9270x;
    int r9280;
    int r9280x;
    int r9290;
    int r9290x;
    int r9295x2;

        int gpuNames[14];       // The names of the different GPUs

    int maxPower;   // The maximum power the PSU can deliver to the components
};

void initialization()
{
        vars.gpuNames[0] = "gtx750ti";
        vars.gpuNames[1] = "gtx760";
        vars.gpuNames[2] = "gtx770";
        vars.gpuNames[3] = "gtx780";
        vars.gpuNames[4] = "gtxtitan";
        vars.gpuNames[5] = "gtxtitanB";
        vars.gpuNames[6] = "gtxtitanZ";
        vars.gpuNames[8] = "r9270";
        vars.gpuNames[9] = "r9270x";
        vars.gpuNames[10] = "r9280";
        vars.gpuNames[11] = "r9280x";
        vars.gpuNames[12] = "r9290";
        vars.gpuNames[13] = "r9290x";
        vars.gpunames[14] = "r9295x2";
}

int main()
{
    vars vars;
    initiation();
    return 0;
}

1 个答案:

答案 0 :(得分:2)

正如其他人的评论所说,不管案例如何,都不要使用同名的类和变量。

此外,标识符名称区分大小写。因此“gpuNames”与“gpunames”不同。

那就是说,我建议你把你的名字数组作为静态和常量。这将允许编译器创建数据表并让代码直接访问该表;不会进行任务。

示例:

static const char * gpu_name_table[] =
{
  "gtx750ti";
  "gtx760";
    "gtx770",
    "gtx780",
    "gtxtitan",
    "gtxtitanB",
    "gtxtitanZ",
    "r9270",
    "r9270x",
    "r9280",
    "r9280x",
    "r9290",
    "r9290x",
    "r9295x2",
};
static const unsigned int number_of_gpu_names =
  sizeof(gpu_name_table) / sizeof(gpu_name_table[0]);

上面的数组习惯用法允许您添加GPU名称,而无需更改程序中的任何其他变量。变量number_of_gpu_names由编译器赋值。