1

プロジェクトをXcodeに移行し始めたところですが、プロジェクトをビルドしようとすると、コンストラクター定義でエラーが発生します。デフォルトのコンストラクターでは、「期待されるメンバー名または';」を取得します。宣言指定子の後」と他のコンストラクターで次のようになります。

  1. "期待される ')'"
  2. フィールドのタイプが不完全です'Button:: Button'
  3. 非フレンドクラスのメンバー'string'は修飾名を持つことができません


#include <string>

#ifndef BUTTON_H
#define BUTTON_H

namespace interface1
{
class Button
{
    private:
        std::string name;
        float xPos;
        float yPos;
        float width;
        float height;

    public:
        //Default constructor
        Button::Button();
        //Constructor with id, x, y, width and height parameters
        Button::Button(std::string, float, float, float, float); 

        //Getters and setters
        std::string getName();
        float getX();
        void setX(float);
        float getY();
        void setY(float);
        float getWidth();
        void setWidth(float);
        float getHeight();
        void setHeight(float);
        bool isClicked(int, int);
        void draw(int, float, float, float, float);
};
}
#endif

何がうまくいかないのか分かりますか?

4

1 に答える 1

2

コンストラクターはクラス定義内にあるため、他のメンバー関数と同様に、Button::プレフィックスは必要ありません。一部のコンパイラはまだ追加の資格を受け入れますが、受け入れないコンパイラもあります。

class Button {
    Button(); //already in class scope, so no extra qualification needed
};

一方、クラス外でこれらのメンバーを定義する場合は、資格が必要です。それ以外の場合は、新しい関数を作成します(少なくとも、戻り型を持つ非コンストラクターの場合)。

class Button {
    Button();
    void foo();
};

void foo(){} //new function, not Button::foo()
void Button::foo(){} //definition of member function
Button::Button(){} //definition of constructor
于 2013-03-05T18:09:54.510 に答える