C++ 오류가 뜨는데 이 오류의 원인이

milk901의 이미지


컴파일을 해봤을떄 에러 메세지가 다음과 같이 뜨더군요 ...

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;

}


댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.