2013-03-05 128 views
1

刚刚开始将我的项目移至Xcode,并且在尝试构建我的项目时,我的构造函数定义中出现错误。 在默认构造函数上,我得到“预期的成员名称或';'后声明说明符”,并在其他构造我得到:C++ Xcode构造函数错误

  1. ‘预期‘)’’
  2. 字段有不完全类型‘按钮::按钮’
  3. 非交友类成员‘串’不能有资格的名字


#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 

任何想法会出错?

+7

摆脱'Button ::'。 – chris 2013-03-05 15:42:42

+0

谢谢。我如何在Visual Studio中使用它,而不是xCode? – user1356791 2013-03-05 15:45:27

+5

你在VS中不需要它 – badgerr 2013-03-05 15:48:23

回答

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