首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

操作符'=' 重定义?解决办法

2013-03-06 
操作符'' 重定义?//this is String.cpp#include windows.hclass String{private:char * m_pC

操作符'=' 重定义?


//this is String.cpp
#include <windows.h>
class String
{
private:
char * m_pCh;
int len;
static int num_strings;//number of string
static const int CINLIM=80;//cin input limit
public:
String();
String(const char * s);
String(const String &s);
int Length()const{return len;};
String & operator=(const String &s);
String & operator=(const char* s);
char & operator[](int i);
const char & operator[](int i)const;
friend bool operator<(const String &s1,const String & s2);
friend bool operator>(const String & s1,const String & s2);
friend bool operator==(const String & s1,const String & s2);
static int HowMany();
};

String & String::operator=(const char *s)
{
    delete[] m_pCh;
    len  = strlen(s);
    m_pCh=new char[len+1];
    strcpy(m_pCh,s);
    return *this;
}

main.cpp里仅包含String.cpp,构建了一个String对象

[解决办法]
String & String::operator=(const char *s)
{
    delete[] m_pCh;
    len  = strlen(s);
    m_pCh=new char[len+1];
    strcpy(m_pCh,s);
    return *this;
}

放到cpp中,不要放在头文件中
[解决办法]
请问楼上,放在cpp和放在.h中有什么区别呢?

热点排行