0

This has been driving me nuts for the past couple of hours. I'm still quite new to C++ so the answer may be very simple.

This is for a header file:

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

using namespace std;

class Band

{

public:

    int blk;
    int brn;
    int r;
    int o;
    int y;
    int gn;
    int blu;
    int p;
    int gy;
    int whi;

    Band();
    {
            blk = 0;
            brn = 1;
            r = 2;
            o = 3;
            y = 4;
            gn = 5;
            blu = 6;
            p = 7;
            gy = 8;
            whi = 9;
    }
};

The second { bracket right under Band(); is receiving an error saying "expected a declaration." The whole code is there so nothing should be missing. Thank you for the help!

4

3 回答 3

2

删除分号。

Band(); // this one
{
于 2013-11-10T19:55:57.207 回答
0

你的错误在这里:

Band();
   // ^
{
    // ...

分号在这里是多余的!要么在头文件中声明构造函数(使用分号),然后省略正文。或者您在头文件中有定义并省略分号。

还可以考虑使用成员初始化器列表来初始化您的类成员变量,而不是在构造函数主体中执行此操作。这可能更有效,尤其是对于非原始类型。

于 2013-11-10T19:56:03.903 回答
0

您只需要将构造函数更改为

Band()
{
        blk = 0;
        brn = 1;
        r = 2;
        o = 3;
        y = 4;
        gn = 5;
        blu = 6;
        p = 7;
        gy = 8;
        whi = 9;
};
于 2013-11-10T20:07:47.270 回答