게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
c++중수님! 조언부탁드립니다
게시물ID : freeboard_335846짧은주소 복사하기
작성자 : coolwine
추천 : 0
조회수 : 315회
댓글수 : 9개
등록시간 : 2009/04/20 23:07:42
연산자 오버로딩에 관한 이러저러한 내용인데요!
'munja' : cannot access private member declared in class 'CString'이게 뜨네요!
전역함수operator<<에 대해 클래스에서 friend선언 해줬는데 왜 접근못한댈까요???
고수님들 인도하여 주옵소서
  
#include <iostream>
#include <cstring>
using namespace std; 
class CString{
private:
char *munja;
int len;
public:
CString(char *s="Unkown", int b=0){
len=strlen(s);
munja=new char[len+1];
strcpy(munja,s);
}
CString operator+(const CString &a){      //+연산자 오버로딩
char *mun;
mun=new char[len+a.len];
strcpy(mun,munja);
strcat(mun,a.munja);
return CString(mun,len+a.len);
}
CString &operator=(const CString &a){
delete [] munja; //munja가 디폴트로 받는 Unkown을 삭제
len=a.len;
munja=new char[len+1];
strcpy(munja,a.munja);
return (*this);
}
CString(const CString &a){ //복사 생성자.
len=a.len;
munja=new char[len+1];
strcpy(munja,a.munja);
}
char &operator[](int a){ return munja[a];}      //[]첨자 연산자 오버로딩

friend ostream &operator<<(ostream &out, const CString S0);

//void Print(){
// for(int i=0;i<len;i++)
// cout<<munja[i];
// cout<<endl;
//}
};
ostream &operator<<(ostream &out, const CString S0){
out<<S0.munja;
return out;
}
int main(void)
{
CString s1 = "Good "; // 생성자
CString s2 = "Morning";
CString s3; // 디폴트 생성자

s3 = s1 + s2; // + 연산자, = 연산자

CString s4 = s3; // 복사 생성자
s4[1] = 'v'; // [] 연산자
s4[2] = 'v';

// s1.Print();
// s2.Print();s3.Print();s4.Print();
cout << s1 << endl; // << 연산자
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
// s1 += s2; // += 연산자
// if (s1 == s3) // == 연산자
// cout << "equal!" << endl;
return 0;
}
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호