2013-03-04 2 views
-1

나는 이것에 대해 많은 질문이 있지만 나는 그것을 얻지 못한다는 것을 알고있다. D : 미안하지만, 나는 이것에 대해 새롭다. 이해하지 못합니다 ... 이것은 숙제입니다. 타이머 알람을 설정해야하는데, 갑자기이 오류가 나타났습니다. (학교 컴퓨터에서 일할 때 갑자기 나타났습니다.) 나는 어떻게 수정해야할지 모르겠다. 나 좀 도와 줘!C++ 오류 c2512 : 기본 생성자를 사용할 수 없음

#include "stdafx.h" 
#include <iostream> 
using namespace std; 

class Display { 
public: 
    Display(int lim); 
    void Increment(); 
    void SetValue(int val); 
    int GetValue(); 
    void Show(); 
    int GetLimit(); 

private: 
    int limit, value; 
}; 

class Timer { 
public: 
    Timer(); 
    void Increment(); 
    void Set(); 
    void SetAlarm(); 
    void Show(); 
    void ShowAlarm(); 

private: 
    Display hours, minutes, seconds, alarmH, alarmM, alarmS; 
}; 

Display::Display(int lim) { 
    value=0; 
    limit=lim; } 

void Display::Increment() { 
    value++; 
    if (value==limit) 
     value=0; } 

void Display::SetValue(int val) { 
    if (val<0) 
     val=-val; 
    value=(val%limit); } 

int Display::GetValue() { 
    return value; } 

void Display::Show() { 
    if (value<10) 
     cout<<"0"; 
    cout<<value; } 

int Display::GetLimit() { 
    return limit; } 

Timer::Timer():hours(24), minutes(60), seconds(60) { 
} 

void Timer::Increment() { 
    seconds.Increment(); 
    if(seconds.GetValue()==0) { 
     minutes.Increment(); 
     if(minutes.GetValue()==0) 
      hours.Increment();} } 

void Timer::Show() { 
    hours.Show(); 
    cout<<':'; 
    minutes.Show(); 
    cout<<':'; 
    seconds.Show(); } 

void Timer::ShowAlarm() { 
    alarmH.Show(); 
    cout<<':'; 
    alarmM.Show(); 
    cout<<':'; 
    alarmS.Show(); } 

void Timer::Set() { 
    int setting; 
    cout<<"Poner horas a que valor?\n"; 
    cout<<"Ingresa un entero entre 0 y "; cout<<hours.GetLimit()<<": "; 
    cin>>setting; 
    hours.SetValue(setting); 
    cout<<"Poner minutos a que valor?\n"; 
    cout<<"Ingresa un entero entre 0 y "; cout<<minutes.GetLimit()<<": "; 
    cin>>setting; 
    minutes.SetValue(setting); 
    cout<<"Poner segundos a que valor?\n"; 
    cout<<"Ingresa un entero entre 0 y "; cout<<seconds.GetLimit()<<": "; 
    cin>>setting; 
    seconds.SetValue(setting); } 

void Timer::SetAlarm() { 
    int setting; 
    cout<<"Poner horas a que valor?\n"; 
    cout<<"Ingresa un entero entre 0 y "; cout<<hours.GetLimit()<<": "; 
    cin>>setting; 
    alarmH.SetValue(setting); 
    cout<<"Poner minutos a que valor?\n"; 
    cout<<"Ingresa un entero entre 0 y "; cout<<minutes.GetLimit()<<": "; 
    cin>>setting; 
    alarmM.SetValue(setting); 
    cout<<"Poner segundos a que valor?\n"; 
    cout<<"Ingresa un entero entre 0 y "; cout<<seconds.GetLimit()<<": "; 
    cin>>setting; 
    alarmS.SetValue(setting); } 

void main() { 
    Timer t; 
    cout<<"Aqui esta el valor incial del timer: "; 
    t.Show(); 
    cout<<"\n\n"; 
    t.Set(); 
    cout<<"Aqui estan los nuevos valores: "; 
    t.Show(); 
    cout<<"\n\n"; 
    cout<<"Lo corremos por 10 segundos...\n"; 
    for (int i=0;i<=10;i++) { 
     t.Increment(); 
     t.Show(); 
     cout<<'\n'; } 
} 
+0

당신의 오류 가능성이 오류 라인을 가리켜 야합니다 고려하고있는 명확 할 수 없습니다. – Rapptz

+2

힌트 : 'Display'클래스의 멤버가 있지만 'Display'에는 기본 생성자가 없습니다. – Rapptz

+0

네, 그렇습니다 만, 그건 의미가 없습니다 ... 그것은 61 행에 있습니다. Timer :: Timer() : 시간 (24), 분 (60), 초 (60) { } 하지만 Display에는 기본 생성자가 없습니다 ... – Opponent019

답변

4

귀하의 Timer 클래스는 데이터 멤버로 표시 객체를 가지고 있으며, 타이머의 생성자가 명시 적으로 모든 (AlarmH, AlarmM, 알람)를 구성하지 않습니다 이 내 코드입니다. 따라서 컴파일러는 Display의 기본 생성자를 사용하여 구성하려고하지만 거기에는 구성자가 없습니다. 이는 Display에 대해 (기본값이 아닌) 생성자를 제공했기 때문에 컴파일러가 자동으로 기본 생성자를 생성하지 않습니다 (Display에 대해 생성자를 전혀 제공하지 않은 경우 그렇게합니다).

해결책 : 명시 적으로 디스플레이에 대한 기본 생성자를 제공하십시오.

선언 :

class Display { 
public: 
    Display(); // Default constructor 
    Display(int lim); 
    void Increment(); 
    void SetValue(int val); 
    int GetValue(); 
    void Show(); 
    int GetLimit(); 

private: 
    int limit, value; 
}; 

정의 :

Display::Display(){} // Default constructor 

Display::Display(int lim) { 
    value=0; 
    limit=lim; } 

// etc. 
+0

고마워요! 그것이 비주얼에서 완전히 고쳐지지는 않았지만 그것을 지웠다. 이제오류 LNK2019 : 미해결 외부 기호 "public : __thiscall Display :: Display (void)"(?? 0Display @@ QAE @ XZ)가 참조되었다. 함수 "public : __thiscall Timer :: Timer (void)"(0Timer @@ QAE @ XZ) S : – Opponent019

+0

@Eirikra 내 대답은 Display()에 대한 선언 만있었습니다. 정의를 제공하는 방법을 보여주기 위해 편집했습니다. – JBentley

+0

ooooooh, 하하는 나도 그걸 넣어 줘야한다는 것을 몰랐어, 고마워! :) – Opponent019

관련 문제