找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1450|回复: 9

运行下这个,看看怎么回事

[复制链接]
发表于 2009-11-3 03:31:03 | 显示全部楼层 |阅读模式 IP:江苏扬州
今天在两台机子上面运行 结果一个报错 另外一个没报错 一个是VC++6.0的英文版 没报错 中问版的报错了 郁闷 怎么回事
#include <iostream>
using namespace std;
class String
{
public:
String() { inside= new char(50);}//ok
String(char *get)
{for(int i=0;i<strlen(get);i++)
inside[i]=get[i];}
String(String& s1)
{
inside=new char (s1.length());
inside=s1.inside;
}//ok
int find(String& s2);
int length();//ok
~String () {}
void operator =(String& c1);//ok
String operator +(String& c2);//ok
friend istream& operator >>(istream &cin,String &o1);//ok
friend ostream& operator <<(ostream &cout,String &o2);//ok
bool operator > (String c3);
bool operator < (String c4);
char operator [](int x);//ok
private:
char *inside;
};
int String::find(String& s2)
{
if(s2.length()>length())
return -1;
else
{
int t=0,i=0;
for(;i<length(),t<s2.length();i++)
if(inside[i]==s2.inside[t])
t++;
if(t==s2.length())
return i-t;
else
return -1;
}
}
int String::length()
{
int len(0);
for(int i=0;inside[i]!='\0';i++,len++)
;
return len;
}
void String::operator =(String& c1)
{
inside=c1.inside;
}
String String::operator +(String& c2)
{
int t=length();
for(int nu=0;nu<c2.length()+1;nu++)
inside[nu+t]=c2.inside[nu];
return *this;
}
istream& operator >>(istream& cin,String& o1)
{
cin>>o1.inside;
return cin;
}
ostream& operator <<(ostream& cout,String& o2)
{
cout<<o2.inside;
return cout;
}
bool String::operator >(String c3)
{
if(strcmp(inside,c3.inside)==1)
return true;
else
return false;
}
bool String::operator <(String c4)
{
if(strcmp(inside,c4.inside)==-1)
return true;
else
return false;
}
char String::operator [](int x)
{
return *(inside+x);
}
int main()
{
String s1,s2,s3;
cin>>s1>>s2;
s3=s1+s2;
cout<<s3<<endl;
for(int i=0;i<s3.length();i++)
cout<<s3[i];
cout<<endl;
if(s1>s2)
cout<<s1<<" is more lager than "<<s2<<endl;
//cout<<s3.find(s2);
return 0;
}
发表于 2009-11-3 03:31:05 | 显示全部楼层 IP:江苏扬州
我这运行没问题,但song4说他编译有问题,根据song4那边编译出错信息看是友元不能访问私有成员的问题,VC的报错类似于:
error C2248: 'inside' : cannot access private member declared in class 'String'

如果你也是这个问题的话那么就不是因为中文和英文VC的区别,而是vc6的bug,你把sp6 for vc6.0的补丁装上应该没事了

最后,申明一下,如果你的编译器编译代码出了问题,你想要别人帮你解答的话应该把你看到的出错信息贴出来,否则别人不好下手帮你解决,因为问题都不知道是什么
回复

使用道具 举报

发表于 2009-11-3 03:31:06 | 显示全部楼层 IP:江苏扬州
看了半天,郁闷半天
是个八哥....
郁闷
还好问了aogun怎么回事,要不我就疯了
回复

使用道具 举报

发表于 2009-11-3 03:31:07 | 显示全部楼层 IP:江苏扬州
谢谢两位
下次我会注意的
回复

使用道具 举报

发表于 2009-11-3 03:31:08 | 显示全部楼层 IP:江苏扬州
s3=s1+s2; //line 95 here is error, no match for 'operator='
回复

使用道具 举报

发表于 2009-11-3 03:31:09 | 显示全部楼层 IP:江苏扬州
以下是引用kai在2006-6-30 9:53:50的发言:


s3=s1+s2; //line 95 here is error, no match for 'operator='
代码中已经重载了

void operator =(String& c1);//ok
String operator +(String& c2);//ok
friend istream& operator >>(istream &cin,String &o1);//ok
friend ostream& operator <<(ostream &cout,String &o2);//ok
回复

使用道具 举报

发表于 2009-11-3 03:31:10 | 显示全部楼层 IP:江苏扬州
it is depend on which compiler you uesed. When you use gcc, you will get error.
strictly speaking, you should define your copy constructor and assignment function in this form

Classname(const Classname & object); // copy constructor
// pay attention to "const", you should not forget this keyword

Classname & operator=(const Classname & object); // assignment
// pay attention to "const", you should not forget this keyword

I have his class rewritten, take a look.


程序代码:


#include <iostream>
using namespace std;

class String
{
public:
String()
{
inside= new char[50];
inside[49] = '\0';
}
String(const char * get)
{
int length = strlen(get);
inside = new char[length + 1];
strcpy(inside, get);
inside[length] = '\0';
}
String(const String & s1) // this is a copy constructor,
// it must defined it with using keyword "const",
// when you write your code strictly.
// with keyword const to
// tell your compiler,
// this parameter will be just used but not changed.
{
int size = strlen(s1.inside) + 1;
inside = new char[size];
strcpy(inside, s1.inside);
inside[size-1] = '\0';
}//ok
int find(String & s2);
int length();//ok

~String ()
{
if(inside)
delete [] inside;
}
String & operator=(const String & c1);//ok
String & operator +(const String & c2);//ok
friend istream& operator >>(istream &cin,String &o1);//ok
friend ostream& operator <<(ostream &cout,String &o2);//ok

bool operator > (String c3);
bool operator < (String c4);
char operator [](int x);//ok
private:
char *inside;
};

int String::find(String & s2)
{
if(s2.length()>length())
return -1;
else
{
int t=0,i=0;
for(;i<length(),t<s2.length();i++)
if(inside[i]==s2.inside[t])
t++;
if(t==s2.length())
return i-t;
else
return -1;
}
}

int String::length()
{
return strlen(inside);
}

String & String::operator=(const String & c1)
{
if((strcmp(inside, c1.inside) == 0) && inside == c1.inside)
return *this;

int size = strlen(c1.inside) + 1;
inside = new char[size];
strcpy(inside, c1.inside);
inside[size - 1] = '\0';
return *this;
}


String & String::operator +(const String & c2)
{
int size = length() + strlen(c2.inside) + 1;
char * temp = new char[size];
strcpy(temp, inside);
strcat(temp, c2.inside);
inside = temp;

return *this;
}

istream& operator >>(istream& cin,String& o1)
{
cin>>o1.inside;
return cin;
}

ostream& operator <<(ostream& cout,String& o2)
{
cout<<o2.inside;
return cout;
}


bool String::operator >(String c3)
{
if(strcmp(inside,c3.inside)==1)
return true;
else
return false;
}
bool String::operator <(String c4)
{
if(strcmp(inside,c4.inside)==-1)
return true;
else
return false;
}
char String::operator [](int x)
{
return *(inside+x);
}

int main()
{
char c[] = "hello 000 world";
char c1[] = "wor";
String s1(c);
String s2(c1);
String s3 = s1 + s2;

cout<<s3<<endl;
for(int i=0;i<s3.length();i++)
cout<<s3[i];
cout<<endl;
if(s1>s2)
cout<<s1<<" is more lager than "<<s2<<endl;
cout<<s3.find(s2);
return 0;
}
回复

使用道具 举报

发表于 2009-11-3 03:31:13 | 显示全部楼层 IP:江苏扬州
谢谢kai的讲解
可是我还是有点不明白
你在上面的构造函数里面
String(const String & s1) // this is a copy constructor,
// it must defined it with using keyword "const",
// when you write your code strictly.
// with keyword const to
// tell your compiler,
// this parameter will be just used but not changed.
上面的红色部分我不明白
就算我没告诉它这个是CONST的
只要下面没有错误的使用应该也不会出错才对
我用的是VC++6.0
回复

使用道具 举报

发表于 2009-11-3 03:31:14 | 显示全部楼层 IP:江苏扬州
you should know the meaning of const and you should know what is standard in C/C++
回复

使用道具 举报

发表于 2009-11-3 03:31:16 | 显示全部楼层 IP:江苏扬州
谢谢kai
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 13:28 , Processed in 0.194364 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表