Mam taką klasę:
// 12.2
#ifndef twelve_two_h
#define twelve_two_h
#include <iostream>
class String
{
private:
char * str;
int len;
static int num_strings;
static const int CINLIM = 80;
public:
String(const char * s);
String();
String(const String & s);
~String();
int lenght() const {return len;}
void stringlow();
void stringup();
int has(char c) const;
String & operator=(const String & s);
String & operator=(const char * s);
char & operator[](int i);
const char & operator[](int i) const;
String operator+(const String & st) const;
friend bool operator<(const String & st1, const String & st2);
friend bool operator>(const String & st1, const String & st2);
friend bool operator==(const String & st1, const String & st2);
friend String operator+(const char * s, const String & st);
friend std::ostream & operator<<(std::ostream & os, const String & st);
friend std::istream & operator>>(std::istream & is, String & st);
static int HowMany();
};
#endif
i chodzi mi o funkcję zaprzyjaźnioną, przeciążającą operator+
String operator+(const char * s, const String & st)
{
String temp;
delete [] temp.str;
temp.len = std::strlen(s) + std::strlen(st.str);
temp.str = new char [temp.len + 1];
std::strcpy(temp.str, s);
std::strcat(temp.str, st.str);
return temp;
}
Komilator wypluwa coś takiego:
D:\cpp\cw>g++ -Wall 12.2.cpp 12.2.main.cpp
12.2.h: In function 'String operator+(const char*, const String&)':
12.2.h:11:10: error: 'char* String::str' is private
12.2.cpp:136:17: error: within this context
12.2.h:12:7: error: 'int String::len' is private
12.2.cpp:137:7: error: within this context
12.2.h:11:10: error: 'char* String::str' is private
12.2.cpp:137:45: error: within this context
12.2.h:11:10: error: 'char* String::str' is private
12.2.cpp:138:7: error: within this context
12.2.h:12:7: error: 'int String::len' is private
12.2.cpp:138:28: error: within this context
12.2.h:11:10: error: 'char* String::str' is private
12.2.cpp:139:19: error: within this context
12.2.h:11:10: error: 'char* String::str' is private
12.2.cpp:140:19: error: within this context
12.2.h:11:10: error: 'char* String::str' is private
12.2.cpp:140:27: error: within this context
Mówi, że nie mogę dla obiektu temp używać jego składowych prywatnych. Ale czemu, przecież to funkcja zaprzyjaźniona?
ps.
Co ciekawe, Visual C++ 2010 EE nie wykrywa błędów, tylko g++ się czepia...