// file string.h
#ifndef STRING_H
#define STRING_H
class String
{
public:
string(char const *chars="");
void displayString();
void changeString(int idx,char ch);
private:
char *ptrChars;
};
#endif
// file string.cpp
#include<cstring>
#include<cstdio>
#include"string.h"
String::string(char const *chars)
{
ptrChars=new char[std::strlen(chars) +1];
std::strcpy(ptrChars,chars);
}
void String::displayString()
{
std::puts(ptrChars);
}
void String::changeString(int idx,char ch)
{
ptrChars[idx]=ch;
}
// file main.cpp
#include "string.h"
int main()
{
String str("B++");
str.changeString(0,'c');
str.displayString();
}
编译总出现这个问题:
error C2664: '__thiscall String::String(const class String &)' : cannot convert parameter 1 from 'char [4]' to 'const class String &'
Reason: cannot convert from 'char [4]' to 'const class String'
咋搞...?