C++ 오류가 뜨는데 이 오류의 원인이
컴파일을 해봤을떄 에러 메세지가 다음과 같이 뜨더군요 ...
error C2248: 'str' : cannot access private member declared in class 'String'
see declaration of 'str'
char * 형으로 선언한 str은 friend 선언을 해줘서 허용이 된다고 생각하는데 왜 이게 오류가 발생하는지 알려주시면
감사하겠습니다..
String.h
#ifndef __STRING_H__
#define __STRING_H__
#include "BankingCommonDecl.h"
class String{
private:
int len;
char * str;
public:
String();
String(const char *s);
String(const String &s);
~String();
String& operator=(const String &s);
String& operator+=(const String &s);
bool operator==(const String &s);
String operator+(const String &s);
friend ostream& operator<< (ostream& os,const String& s);
friend istream& operator>> (istream& is,String &s);
};
#endif
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
String.cpp
#include "String.h"
String :: String()
{
len = 0;
str = NULL;
}
String :: String(const char *s)
{
len = strlen(s);
str = new char[len];
strcpy(str,s);
}
String :: String(const String &s)
{
len = s.len;
str = new char[len];
strcpy(str,s.str);
}
String :: ~String()
{
if(str != NULL)
{
delete []str;
}
}
String& String::operator=(const String &s)
{
if(str != NULL)
{
delete []str;
}
len = s.len;
str = new char[len];
strcpy(str,s.str);
return *this;
}
String& String::operator+=(const String &s)
{ // str1 += str2;
len += (s.len-1);
char * tempstr = new char[len];
strcpy(tempstr,str);
strcat(tempstr,s.str);
if(str != NULL)
{
delete []str;
}
str = tempstr;
return *this;
}
bool String::operator==(const String &s)
{
return strcmp(str,s.str) ? false : true;
}
String String::operator+ (const String &s) // str1+ str2;
{
char * tempstr = new char[len+s.len-1];
strcpy(tempstr,str);
strcat(tempstr,s.str);
String temp(tempstr);
delete []tempstr;
return temp;
}
ostream& operator<< (ostream& os , const String& s)
{
os<
return os; // 이부분에서 오류발생 ..
}
istream& operator>> (istream& is,String &s)
{
char str[100];
is>>str;
s = String(str);
return is;
}
댓글 달기