C++编译的时候总是说ambiguous symbol 和 cannot convert parameter 1 from 'class string' to 'const ch 10
#include<iostream>(就是定义一个模仿string的类)#include<string.h>usingnamespacestd;classstring{p...
#include<iostream>(就是定义一个模仿string的类)
#include<string.h>
using namespace std;
class string
{
public:
string(){char *str="";}
string(char *str);
string(const string&);
string& operator + ( string&);
string& operator = ( string&);
bool operator ==( string&);//compare two strings
string& operator () (int a,int b);//return a sub-string。。。截取一段的意思
friend ostream& operator>>(ostream& output, string& c)
{
cout<<"处理后的字符串为"<<endl;
output<<c.strings<<endl;
return output;
}
friend istream& operator<<(istream& input, string& c)
{
cout<<"输入字符串"<<endl;
input>>c.strings;
}
private:
char *strings;
// int strlenth;
};
string::string(char *str)
{
int i=strlen(str);
strings=new char[i+1];
strcpy(strings,str);
}
string::string(const string& str1)
{
int len=strlen(str1.strings);
strings=new char[len+1];
strcpy(strings,str1.strings);
}
string& string::operator +(string& str1)
{
char t[50];
strcpy(t,strings);
int i=strlen(strings),j=strlen(str1);
strings=new char[i+j+1];
strcpy(strings,t);
strcat(strings,str1);
return *this;
}
string& string::operator =(string& str1)
{
int i=strlen(str1);
strings=new char[i+1];
strcpy(strings,str1);
return *this;
}
bool string::operator ==(string& str1)
{
if(strcmp(strings,str1)==0)
return true;
else
return false;
}
string& string::operator ()(int a,int b)
{
char t[50];
for(int i=a;i<=b;i++)
t[i]=*(strings+i);
strings=new char;
strcpy(strings,t);
return *this;
}
这是我写的代码(VC 6.0)..里面总是说
D:\VC\C\MSDev98\MyProjects\string gai\1.cpp(41) : error C2872: 'string' : ambiguous symbol
D:\VC\C\MSDev98\MyProjects\string gai\1.cpp(45) : error C2664: 'strlen' : cannot convert parameter 1 from 'class string' to 'const char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 展开
#include<string.h>
using namespace std;
class string
{
public:
string(){char *str="";}
string(char *str);
string(const string&);
string& operator + ( string&);
string& operator = ( string&);
bool operator ==( string&);//compare two strings
string& operator () (int a,int b);//return a sub-string。。。截取一段的意思
friend ostream& operator>>(ostream& output, string& c)
{
cout<<"处理后的字符串为"<<endl;
output<<c.strings<<endl;
return output;
}
friend istream& operator<<(istream& input, string& c)
{
cout<<"输入字符串"<<endl;
input>>c.strings;
}
private:
char *strings;
// int strlenth;
};
string::string(char *str)
{
int i=strlen(str);
strings=new char[i+1];
strcpy(strings,str);
}
string::string(const string& str1)
{
int len=strlen(str1.strings);
strings=new char[len+1];
strcpy(strings,str1.strings);
}
string& string::operator +(string& str1)
{
char t[50];
strcpy(t,strings);
int i=strlen(strings),j=strlen(str1);
strings=new char[i+j+1];
strcpy(strings,t);
strcat(strings,str1);
return *this;
}
string& string::operator =(string& str1)
{
int i=strlen(str1);
strings=new char[i+1];
strcpy(strings,str1);
return *this;
}
bool string::operator ==(string& str1)
{
if(strcmp(strings,str1)==0)
return true;
else
return false;
}
string& string::operator ()(int a,int b)
{
char t[50];
for(int i=a;i<=b;i++)
t[i]=*(strings+i);
strings=new char;
strcpy(strings,t);
return *this;
}
这是我写的代码(VC 6.0)..里面总是说
D:\VC\C\MSDev98\MyProjects\string gai\1.cpp(41) : error C2872: 'string' : ambiguous symbol
D:\VC\C\MSDev98\MyProjects\string gai\1.cpp(45) : error C2664: 'strlen' : cannot convert parameter 1 from 'class string' to 'const char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 展开
1个回答
展开全部
你的代码中,operator+() 函数中有如下代码:
int i=strlen(strings),j=strlen(str1);
其中,strlen(str1) 是不对的。
int i=strlen(strings),j=strlen(str1);
其中,strlen(str1) 是不对的。
追问
额。。为什么不对啊?
追答
你的 str1 的类型是 class string, 而 strlen 函数是libc中的函数,其传入的参数必须是 char *,所以就不对了,除非你自己再 重载一个 接受 class string为参数的 strlen 函数。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询