2013-04-17 1 views
1

제 문제는 클래스에 대해 많이 알지 못합니다. 그래서, 나는이 생성자를 작동 시키려고 노력하고있다. 기본 생성자와 파생 클래스의 생성자가 필요하므로 구현하지 않아도됩니다. 나는 그것을 정의 할 수있다. 나는 그것을 구현할 수 없다. 컴파일러가 중괄호를 기대하고 있다고 나에게 말하고있다. 나는 그것이 나에게주고 오류를 이해하지 못하는 SHAPE.H #ifdef와 SHAPE.H #DEFINE이 코드를 사용할 때 계속 오류가 발생합니다. 상속 된 클래스에서 생성자를 사용하려고합니다. C++

#include<string> 
using namespace std; 
class QuizShape 
{ 
    private:  
     char outer, inner; 
     string quizLabel; 

    public: 
     //Constructor 
     QuizShape(); 
}; 

class Rectangle : public QuizShape 
{ 
    public: 
     int height, width; 

     //Getter & setter methods 
     int getHeight() const; 
     void setHeight(int); 
     int getWidth() const; 
     void setWidth(int); 

     //Constructor for Rectangle 
     Rectangle() : QuizShape(); 
}; 

class Square : public Rectangle 
{ 
    public: 
     //constructors 
     Square() : Rectangle(); This area here is where the error comes // IT says it expects a { but I'm not allowed to define the constructor in line. 
     Square(int w, int h) : Rectangle (height , width); 
}; 

class doubleSquare : public Square 
{ 
//Fill in with constructors 
}; 

#endif // 이는 . 나는 그것도 다시 정의하지 않을 것이라고 확신한다.

답변

0

생성자 초기화 목록을 정의로 이동하십시오. 예를 들어, Square을 위해 :

//declarations 
Square(); 
Square(int w, int h); 

//definitions 
Square() : Rectangle() {/*body*/} 
Square(int w, int h) : Rectangle(w, h) {/*body*/} //assuming you meant w, h 

뿐만 아니라 선언에서 초기화 목록과 다른 생성자 동안 그 작업을 수행합니다.

1

생성자를 정의해야합니다. Pls는 생성자가 정의되고 사용되는 방식의 변화를 관찰합니다.

#include<string> 
    using namespace std; 
    class QuizShape 
    { 
     private:  
      char outer, inner; 
      string quizLabel; 

     public: 
      //Constructor 
      QuizShape(); 
    }; 

    class Rectangle : public QuizShape 
    { 
     public: 
      int height, width; 

      //Getter & setter methods 
      int getHeight() const; 
      void setHeight(int); 
      int getWidth() const; 
      void setWidth(int); 

      //Constructor for Rectangle 
      Rectangle() { } 
      Rectangle(int h, int w): height(h), width(w) { } 
    }; 

    class Square : public Rectangle 
    { 
     public: 
      //constructors 
      Square() { } // 
      Square(int w, int h) : Rectangle (h, w) {} 
    }; 

    class doubleSquare : public Square 
    { 
    //Fill in with constructors 
    }; 
+0

정말 고마워요. 그냥 호기심에서 벗어 났을 때 Square (int w, int h) : Rectangle (h, w) {} 그게 바로 정의 되나요? – varrick

+1

대부분 환영합니다. 함수를 정의하는 것은 "{}"입니다 (빈 정의 임에도 불구하고). ": Rectangle (h, w)"를 호출하면 기본 클래스 Rectangle의 매개 변수화 된 생성자가 호출됩니다. – Arun

+0

그럼, 다른 .cpp 파일에 여전히 구현할 수 있습니까? – varrick

관련 문제