2017-11-23 7 views
1

그래서 쉽게 생각할 수 있습니다.uniform 초기화를 사용하여 객체 및 포인터 객체를 초기화하면 작동하지 않습니다.

참고 : 아래 내용은 C++ 11 이상을 대상으로 한 것입니다.


시작해 보겠습니다. "Employee"라는 클래스가 있습니다.

Employee::Employee(const string& first, const string& last, const string& ssn) 
     : firstName(first), lastName(last), socialSecurityNumber(ssn) {} 
또한

내 주에서 개체를 만들려고 할 때, 나는 다음을 수행하십시오 :

void main() 
{ 
string firstName; 
string lastName; 
string socialSec; 
Employee salariedEmployee{firstName, lastName, socialSec}; 
} 

내가 오류 얻을 :

을 다음과 같이 그 consctructor입니다

error: cannot declare variable 'salariedEmployee' to be of abstract type 'Employee'


그런 다음 객체를 pointe로 만들려고했습니다.

Employee *salariedEmployee{&firstName, &lastName, &socialSec}; 

을 오류를 얻을 : R로는 followes 내가 뭘 잘못 이해하지

error: scalar object 'salariedEmployee' requires one element in initializer


. C++ 11의 이전 버전은 코딩에 익숙했지만 중괄호 (uniform initialization)를 사용하는 새로운 방법을 배우려고합니다. 내가 뭘 잘못하고 있니?

P. 나는 많은 것을 봤지만 나는 무엇을해야하는지에 대해 매우 혼란 스럽다. 내가 저장 한의 resourses의 두이 있습니다 (하지만 훨씬 더 많은 물건을 읽고) :

+0

'Employee'는 순수 가상 함수 (추상 유형)를 가지고 있습니다. 균일 한 초기화와 관련이 없습니다. –

+0

@appleapple 또는 추상 기본 클래스에서 파생되었으며 전체 인터페이스를 구현하지 않았습니다. 어느 쪽이든, 오류는 Dimitris Pantelis가 예상하는 것 이외의 다른 곳에서 발생합니다. – Jodocus

+0

@Jodocus yor 맞아 :) 그걸 잊어 버렸어. –

답변

3

error: cannot declare variable 'salariedEmployee' to be of abstract type 'Employee'

이 오류는 당신이 당신의 생성자를 호출하는 방식과 관련이 없습니다. 단지 완전히 정의되지 않은 유형을 인스턴스화하려는 경우, 일부 메소드는 순수 가상입니다.

#include <string> 

struct Employee 
{ 
    Employee(const std::string& first, const std::string& last, const std::string& ssn); 

    std::string firstName; 
    std::string lastName; 
    std::string socialSecurityNumber; 
}; 

Employee::Employee(const std::string& first, const std::string& last, const std::string& ssn) 
    : firstName(first), lastName(last), socialSecurityNumber(ssn) 
{} 

int main() 
{ 
    std::string firstName; 
    std::string lastName; 
    std::string socialSec; 
    Employee bob{firstName, lastName, socialSec}; 
} 

demo

을하지만 Employee에 순수 가상 fire 방법을 추가하는 경우, 그것은 컴파일 할 수 없게됩니다 :

는 예를 들어,이 작품 demo.

+0

당신 말이 맞아요. Employee에는 "virtual double earnings() const = 0; // pure virtual"과 같은 것이 있습니다. 심지어 순수 가상이지만 그것이 무엇을했는지, 어떻게 사용되었는지 정확히 알지 못한다고합니다. Employee는 실제로 "SalariedEmployee"클래스의 기본 클래스를위한 것입니다. 감사. –