friend有关
string_value.h
-----------------------------------------
#ifndef STRING_VALUE_H
#define STRING_VALUE_H
class StringValue
{
friend StringValue operator+(StringValue const &v1,StringValue &v2);
public:
StringValue();
StringValue(const char *cstr);
StringValue(const StringValue &str);
~StringValue();
StringValue &operator=(const StringValue &str);
inline const char *c_str() const;
private:
inline void Init(const char *cstr);
char *_buf;
};
#endif
string_value.cpp
-----------------------------------
const char *StringValue::c_str() const
{
return _buf;
}
StringValue operator+(const StringValue &v1,const StringValue & v2)
{
cout < < "operator +( " < < v1.c_str() < < ", " < < v2.c_str() < < ") " < < endl;
StringValue result; //empty
int len= static_cast <int> (strlen(v1._buf) + strlen(v2._buf));
char *buf=new char[len + 1];
strcpy(buf,v1.c_str());
strcpy(buf,v2.c_str());
result._buf=buf;
cout < < "Returning bu value " < < endl;
return result;
}
问题:
1.
warning C4996: 'strcpy ' was declared deprecated
e:\tools\studytools\microsoft visual studio 8\vc\include\string.h(73) : see declaration of 'strcpy '
Message: 'This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details. '
2.
error C2248: 'StringValue::_buf ' : cannot access private member declared in class 'StringValue '
c:\cpp.pjt\vs 2005\stringvalue\stringvalue\string_value.h(19) : see declaration of 'StringValue::_buf '
c:\cpp.pjt\vs 2005\stringvalue\stringvalue\string_value.h(5) : see declaration of 'StringValue '
怎么解决?
[解决办法]
问题1,当没看见即可
问题2,friend StringValue operator+(StringValue const &v1,StringValue &v2);
v2前面少了一个cosnt
[解决办法]
StringValue operator+(const StringValue &v1,const StringValue & v2)
==================================
StringValue operator+(const StringValue &v1,StringValue & v2)
[解决办法]
class StringValue
{
friend StringValue operator+(const StringValue const &v1,const StringValue &v2);