내 프로그램을 실행할 때 프로그램이 축소됩니다. 내가 if((str1->compare(*str2))==0){...}
라인을 주석 처리하면 정상적으로 작동합니다. 비교 후 생성 및 삭제하는 string *의 두 요소를 비교하는 방법을 모르겠습니다.Char *와 문자열 및 연산자
main.cpp: In function `int operator==(const Integer&, const Integer&)':
main.cpp:18: warning: taking address of temporary
main.cpp:19: warning: taking address of temporary
Integer.h
class Integer {
public:
Integer(int val, char *opis):m_val(val),m_opis(opis)
{
this->m_val = 0;
this->m_opis = strdup("0");
}
friend int operator==(const Integer&,const Integer&);
private:
int m_val;
char *m_opis;
}
MAIN.CPP
int operator==(const Integer&a, const Integer&b){
string *str1 = &string (a.m_opis);
string *str2 = &string (b.m_opis);
if((str1->compare(*str2))==0){return 1 ;} //<- Here is my problem i think.
delete str1;
delete str2;
return 0;
}
}
//Objects and comparing
Integer o1(15,"lala");
Integer o2(150,"lala");
Integer o3;
cout<<o1==o2;
컴파일러에서 들으시 고, 18 번과 19 번 줄에서 임시 주소 *를 가져 가지 마십시오. –
왜 문자열 인수의 주소를 가져 와서 삭제하려고합니까? 그들? 포인터를 사용할 이유가 없습니다. –
귀하의 컴파일러가 귀하에게 경고 !!!!!!! –