刚刚在写string类时,发现
my_string acr(“345”);
my_string bcr(“123”);
my_string ccr= acr+bcr;
时出现
error: no matching function for call to ‘my_string::my_string(my_string)’
my_string ccr = acr+bcr;
提示我找不到拷贝构造函数,但是我明明是写了拷贝构造函数的 如下:
my_string::my_string(my_string &data)
{
len = data.len;
ary = new char[data.len +1];
strcpy(ary,data.ary);
}
为什么说找不到呢??
string_fun.h:24:1: note: my_string::my_string(my_string&)
my_string::my_string( my_string &data)
^
string_fun.h:24:1: note: no known conversion for argument 1 from ‘my_string’ to ‘my_string&’
string_fun.h:15:1: note: my_string::my_string(const char*)
my_string::my_string(const char * data)
^
string_fun.h:15:1: note: no known conversion for argument 1 from ‘my_string’ to ‘const char*’
string_fun.h:9:1: note: my_string::my_string()
my_string::my_string()
在之后的提示之中 大概是说我的拷贝构造函数参数不对
但是在
my_string ccr= acr;
时却又可以编译通过 ,问题应该是出在operator+这个运算符重载函数上
my_string operator+(my_string& arr,my_string& drr)
{
my_string crr;
crr.len = arr.len + drr.len;
crr.ary = new char[crr.len+1];
strcpy(crr.ary,arr.ary);
strcat(crr.ary,drr.ary);
return crr;
}
在查过资料后发现
return crr; 中 crr为局部类,在传递过程中,会先创造一个临时类 string tmp = crr (特殊析构方式:先析构crr 再析构tmp)
调用时:
my_string ccr= acr+bcr;
即 调用拷贝构造函数将 临时类对象tmp 拷贝至 ccr;
而我的拷贝构造函数
my_string::my_string(my_string &data)
参数为一个类的引用,在C++机制中 不允许对临时对象进行引用,因为不能修改临时对象里的值
而引用存在修改的风险
故而只要将引用改成常引用即可,告诉编译器不会修改其中的值,即可顺利通过编译
以下是修改后源码
string定义:
#ifndef MY_STRING_H
#define MY_STRING_H
#include <iostream>
using namespace std;
class my_string
{
private:
char *ary;
int len;
public:
my_string();
my_string(const char *data);
~my_string();
my_string(const my_string &data);
my_string& operator=(const my_string& data);
friend my_string operator+(my_string& arr,my_string& drr);
friend ostream& operator<<(ostream& cout,my_string& out);
};
#ifdef __cpulspuls
extern "C"
{
#endif
#ifdef __cpulspuls
}
#endif
#endif
string函数借口:
#ifndef _STRING_FUN_H
#define _STRING_FUN_H
#include "my_string.h"
#include <string.h>
my_string::my_string()
{
ary = NULL;
len = 0;
}
my_string::my_string(const char * data)
{
if(data == NULL) return;
len = strlen(data);
ary = new char[len+1];
strcpy(ary,data);
}
//拷贝构造函数
my_string::my_string(const my_string &data)
{
len = data.len;
ary = new char[data.len +1];
strcpy(ary,data.ary);
}
my_string::~my_string()
{
delete[] ary;
}
my_string& my_string::operator=( const my_string& data)
{
if(this == &data)
return *this;
delete[] this->ary;
this->len = data.len;
this->ary = new char[this->len+1];
strcpy(this->ary,data.ary);
return *this;
}
my_string operator+(my_string& arr,my_string& drr)
{
my_string crr;
crr.len = arr.len + drr.len;
crr.ary = new char[crr.len+1];
strcpy(crr.ary,arr.ary);
strcat(crr.ary,drr.ary);
return crr;
}
ostream& operator<<(ostream& cout,my_string& out)
{
if(out.ary == NULL) return cout;
cout << out.ary;
return cout;
}
#ifdef __cpulspuls
extern "C"
{
#endif
#ifdef __cpluspuls
}
#endif
#endif
应用程序:
#include "string_fun.h"
int main()
{
my_string acr("345");
cout << acr <<endl;
my_string bcr("123");
my_string ccr= acr+bcr;
cout << ccr <<endl;
}
在此之前一直用的多的是C,觉得const此类关键字没啥用,在C++中,对变量的类型更加严格,在以后编程中应多多注意