复制构造函数小问. 书上的程序编译错误!
#ifndef STRING_H
#define STRING_H
class String
{
public:
String( String const & );
~String();
private:
char *ptrChars;
};
#endif
#include <cstring>
#include "string.h"
String::String(String const &str )
{
ptrChars = new char[std::strlen(str.ptrChars) + 1 ];
std::strcpy( ptrchars, str.ptrChars );
}
String :: ~String()
{
delete [] ptrChars;
}
#include "string.h"
int
main()
{
String firstString("C++");
String copyofFirstString( firstString );
}
错误提示如下:
f:\learningc++\复制构造函数\main.cpp(6) : error C2664: “String::String” : 不能将参数 1 从“const char [4]”转换为“const String &”
原因如下: 无法从“const char [4]”转换为“const String”
无构造函数可以接受源类型,或构造函数重载决策不明确
string.cpp
f:\learningc++\复制构造函数\string.cpp(7) : error C2065: “ptrchars” : 未声明的标识符
正在生成代码...
自己还是不很明白...请各位赐教.