-1

デフォルトのパラメータが必要な関数があります:

LINE 13:
    void genRand(double offset_x = 0.0, double offset_y = 0.0);

これは機能です:

LINE 84:
    void game::genRand(double offset_x = 0.0, double offset_y = 0.0) {
        perlin.SetFrequency(0.1);
        for(int _x=0; _x<dimensions.x/32; _x++){
            for(int _y=0; _y<dimensions.y/32; _y++){
                vec.push_back((perlin.GetValue(_x+offset_x, _y+offset_y, 0.2)+1.f)*2/2);
            }
        }
    }

エラー:

make
g++ main.cpp -w -std=c++11 -lsfml-graphics -lsfml-window -lsfml-system -lnoise -o main
main.cpp:84:64: error: default argument given for parameter 1 of ‘void game::genRand(double, double)’ [-fpermissive]
 void game::genRand(double offset_x = 0.0, double offset_y = 0.0) {
                                                                ^
main.cpp:13:14: note: previous specification in ‘void game::genRand(double, double)’ here
         void genRand(double offset_x = 0.0, double offset_y = 0.0);
              ^
main.cpp:84:64: error: default argument given for parameter 2 of ‘void game::genRand(double, double)’ [-fpermissive]
 void game::genRand(double offset_x = 0.0, double offset_y = 0.0) {
                                                                ^
main.cpp:13:14: note: previous specification in ‘void game::genRand(double, double)’ here
         void genRand(double offset_x = 0.0, double offset_y = 0.0);
              ^

私は何が間違っていたのか理解できません。

4

1 に答える 1

1

関数の定義(本体)を別に書く場合は、それ以上持ち込まdefault parameterないでください。

実際、デフォルトのパラメーター値は宣言に表示される必要があります。これは、呼び出し元が見る唯一のものであるためです。

繰り返される関数の引数リストのデフォルト値にコメントすることをお勧めします。

void foo(int x = 42,
         int y = 21);

void foo(int x /* = 42 */,
         int y /* = 21 */)
{
   ...
}
于 2015-01-11T10:44:41.393 に答える