2014-03-26 5 views
0

myint.cpp: In function MyInt operator+(const MyInt&, const MyInt&)': myint.cpp:193: error: passing const MyInt' as this' argument of void MyInt::Grow()' discards qualifiers myint.cpp:194: error: passing const MyInt' as this' argument of void MyInt::Grow()' discards qualifiers myint.cpp:219: error: array' undeclared (first use this function) myint.cpp:219: error: (Each undeclared identifier is reported only once for each function it appears in.) myint.cpp: In function MyInt operator*(const MyInt&, const MyInt&)': myint.cpp:252: error: passing const MyInt' as this' argument of void MyInt::Grow()' discards qualifiers myint.cpp:262: error: passing const MyInt' as this' argument of void MyInt::Grow()' discards qualifiers myint.cpp:290: error: invalid conversion from int*' to int' myint.cpp:290: error: initializing argument 1 of MyInt::MyInt(int)'bigint 클래스에서 +와 *에 연산자 오버로딩을하는 동안 한정자가 버려진 곳에서 오류가 발생합니다.

모두 도움이 될 수 있으므로 제 실수입니다.

내 코드는 codepad에서

+0

당신은 그 멤버 함수들을'const' 할 필요가 있다고 생각합니다. – 0x499602D2

답변

0

귀하의 줄 번호는 게시 된 오류와 일치하지 않는 CPP 파일을 여기 http://codepad.org/TlTFLjYd입니다.

myint.cpp : 193 (줄 171) : MyInt operator+ (const MyInt& x, const MyInt& y)에서 x.Grow();을 호출하지만 x은 const입니다. const 개체는 수정할 수 없습니다. operator+을 구현하는 다른 방법으로 x 또는 y을 수정하지 않아야합니다. 예를 들어, MyInt z = x;으로 시작한 다음 z에서 작업하십시오.

myint.cpp : 219 (줄 197) : 당신은 array.numArray이라고 씁니다. 그러나 array을 선언 한 적이 없습니다. 에서 이전 당신의 일부 코드가 있습니다 (명확성을 위해 나를 추가 중괄호) :

if(x.maxSize > y.maxSize) 
{ 
    MyInt array[x.maxSize]; 
} 
else 
{ 
    MyInt array[y.maxSize]; 
} 

이 배열은 그들이에서 선언 된 블록에 로컬, 더 이상이 블록 이후에 존재하지합니다.

또한 변수를 배열 크기로 사용하는 것은 불법입니다. 컴파일러는이를 비표준 확장자로 구현할 수 있습니다.

이 모든 문제를 해결하려면, 하나의 옵션은 다음과 같습니다

std::vector<MyInt> array (std::max(x.maxSize, y.maxSize)); 

myint.cpp : 290 (라인 216). 함수는 MyInt을 반환하지만 배열 인 return array;을 호출합니다. 나는 당신이 여기서 무엇을 의미하는지 확신 할 수 없기 때문에 픽스를 정말로 추천 할 수는 없습니다. 그러나 배열이 MyInt이 아닌 int의 배열을 원한다고 생각한 다음이 새로운 배열을 numArray이되도록 MyInt으로 되돌리려 고합니다.