2014-02-12 3 views
-1

컴퓨터 프로그램 클래스로 컴퓨터 프로그램을 작성하고는 있지만 회원 프로그램과 관련된 오류로 인해 프로그램을 실행할 수 없습니다. 나는 오류 메시지가 계속회원 기능이 작동하지 않는 이유는 무엇입니까?

:이 메시지가하고 나는 그것을 어떻게 해결할 수 잘못 수행 한 어떤

"오류 오류 C3490 그것이 CONST 객체를 통해 액세스되고 있기 때문에 '굴'은 수정할 수 없습니다" ?

이 내 코드입니다 : 당신은 CONST 방법 내부 "서재"라는 이름의 멤버를 수정하려고하는, 당신은 단지 데이터에 액세스하는 const를 내부에 아무것도 수정할 수 없습니다

// implement all class member functions here, gcd function is given 

int Fraction::gcd() 
{ 
if(num == 0) return 1; 

     int a = max(abs(num), abs(den)); 
     int b = min(abs(num), abs(den)); 

while(b != 0) 
{ 
    int result = a%b; 
    a = b; 
    b = result; 
} 

return a; 
} 
void Fraction::input(istream& in) 
{ 
char op; 
in >> num; 
in >> op; 
in >> den; 
if(den == 0) 
{ 
    cout << "Wrong input. Exit the program" << endl; 
    exit(1); 
} 
if(den < 0) 
{ 
num *= -1; 
den *= -1;} 
} 
void Fraction::output(ostream& out) 
{ 
    if(den == 0) 
out << "0"; 
    else 
out << num << "/" << den; 
} 

Fraction::Fraction(int numerator, int denominator) 
{ 
    assert(denominator !=0); 
    num = numerator; 
    den = denominator; 
if(den < 0) 
{ 
    num *=-1; 
    den *= -1; 
} 
} 
bool Fraction::less(const Fraction& other) const 
{ 
Fraction temp(other.num , other.den); 
int num_1(num); 
if(den != temp.den) 
{ 
    num_1 *= temp.den; 
    temp.num *= num; 
} 
if(num_1 < temp.num) 
    {return true;} 
else 
    {return false;} 
} 
    Fraction Fraction::reciprocal() const 
{ 
    Fraction tempa(num, den); 
    int temp(tempa.num); 
    tempa.num = tempa.den; 
    tempa.den = temp; 
    num = den; 
if(den < 0) 
{ 
    num *= -1; 
    den *= -1; 
} 
return tempa; 
    } 
Fraction Fraction::neg() const 
    { 
    num*=-1; 
    return; 
    } 
    Fraction::Fraction(int numerator) 
    { 
    num = numerator; 
    den = 1; 
    } 
    Fraction::Fraction() 
    { 
    num = 0; 
    den = 1; 
} 
Fraction Fraction::add(const Fraction& other) const 
{ 
int num_1 = num *other.den; 
int num_2 = den*other.den; 
int denom = den*other.den; 
Fraction result((num_1+num_2), denom); 
    result.reduce(); 
    return result; 
} 
Fraction Fraction::div(const Fraction& other) const 
{ 
    Fraction temp(num, den); 
    other = other.reciprocal(); 
    Fraction result(temp.mul(other)); 
    return result; 
} 
Fraction Fraction::sub(const Fraction& other) const 
{ 
    int num_1 = num *other.den; 
    int num_2 = den*other.den; 
    int denom = den*other.den; 
    Fraction result((num_1-num_2), denom); 
    result.reduce(); 
    return result; 
} 
Fraction Fraction::mul(const Fraction& other) const 
{ 
    int num_0 = num*other.num; 
    int denom = den*other.den; 
    Fraction result(num_0, denom); 
    result.reduce(); 
    return result; 
} 
void Fraction::reduce() 
{ 
    int r = gcd(); 
    num /= r; 
    den /= r; 
    if(num == 0) 
    den = 1; 

} 
bool Fraction::less_or_equal(const Fraction& other) const 
{ 

} 
bool Fraction::greater_or_equal(const Fraction& other) const 
{ 

} 
bool Fraction::equal(const Fraction& other) const 
{ 

} 
bool Fraction::not_equal(const Fraction& other) const 
{ 

} 
bool Fraction::greater(const Fraction& other) 
+3

부적절한 코드를 제거하고 올바르게 포맷하십시오. – olevegard

+1

메시지가 말한 것처럼'const' 객체를 통해'den'을 수정하려고했습니다. 그것이 당신이 잘못한 것입니다. 위의 내용에 대해 명확하지 않은 것이 있습니까? (예 답 : "수정"이 무엇인지 모르겠다; "const"가 무엇인지 모르겠다. 어떤 객체가 const인지 모르겠다. 왜 잘못되었는지 모른다.) –

답변

2

. const를 없애거나 const를 사용하지 않고 함수를 복제하려면 함수를 수정해야합니다.

코드를 읽기가 어렵지만이 코드는 "reciprocal()"메소드에서 발생하는 것 같습니다.

+1

또는 멤버를 '변경할 수 있음'으로 선언하십시오. – olevegard

+3

그리고 이것은 num 멤버와 마찬가지로 발생합니다. 그리고 해결책은 그것들을 non-const로 만들거나 멤버를 변경 가능하게 만드는 것이 아닙니다. 함수를 보라. 그들은 계산 된 결과의 값 - 복사본을 반환하고, 기존의 객체를 변경시키지 않아야합니다. 적어도 그들 중 일부는. – WhozCraig

+0

물론 이것은 클래스 내부의 모든 변수에 적용됩니다. –

1

const 한정자로 선언 된 멤버 함수는 기본 객체를 변경할 수 없습니다. 예를 들어 함수의 역수 예선 CONST

Fraction Fraction::reciprocal() const 
{ 
Fraction tempa(num, den); 
int temp(tempa.num); 
tempa.num = tempa.den; 
tempa.den = temp; 
num = den; 
if(den < 0) 
{ 
num *= -1; 
den *= -1; 
} 
return tempa; 
    } 

로 선언하고 자체 내에서 당신은 당신이 그것을 정의해야 const를 한정자와 함수는 데이터 멤버의 서재를 변경할 수 있음을하려면 변수 서재

if(den < 0) 
{ 
num *= -1; 
den *= -1; 
} 

을 수정하려고 변경할 수 있습니다. 예 :

mutable int den; // denominator of the fraction 

회원 기능을 재 설계하는 것이 더 좋을지라도.

1

: 그것이 말하는 정확히 무엇을

"오류 오류 C3490 그것이 CONST 객체를 통해 액세스되고 있기 때문에 '굴'은 수정할 수 없습니다": CONST로 표시된 멤버 함수에서, 당신은 시도했습니다 멤버 변수의 값을 변경합니다. 너 내가 볼 수있는 몇 번 해봤 네. 예를 들어,) (상호 : 메소드 분수 봐 :

Fraction Fraction::reciprocal() const // NOTE: CONST METHOD 
{ 
    Fraction tempa(num, den); 
    int temp(tempa.num); 
    tempa.num = tempa.den; 
    tempa.den = temp; 
    num = den; 
    if(den < 0) 
    { 
     num *= -1; 
     den *= -1; // NOTE: ATTEMPT TO MODIFY den, WHICH IS CONST 
    } 
} 

귀하의 컴파일러는 당신에게 그것에 대해 불평하는 인스턴스의 정확한 행 번호를 알려 주어야합니다. 한 가지 해결책은 해당 함수에서 const 지정자를 제거하는 것입니다.

그러나, den이 "논리 상태"(즉, 캐시 값 또는 다른 중간 값)의 일부가 아닌 경우 일반적으로 변수를 mutable으로 대신 선언 할 수 있습니다.

관련 문제