| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 927 人关注过本帖
标题:运行下这个,看看怎么回事
只看楼主 加入收藏
huanguyu
Rank: 1
等 级:新手上路
帖 子:90
专家分:0
注 册:2006-4-26
收藏
 问题点数:0 回复次数:10 
运行下这个,看看怎么回事

今天在两台机子上面运行 结果一个报错 另外一个没报错 一个是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;
}

搜索更多相关主题的帖子: 运行 
2006-06-28 20:03
aogun
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:638
专家分:0
注 册:2006-4-5
收藏
得分:0 

我这运行没问题,但song4说他编译有问题,根据song4那边编译出错信息看是友元不能访问私有成员的问题,VC的报错类似于:
error C2248: 'inside' : cannot access private member declared in class 'String'

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

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


世界上总共有 10 种人,一种懂得什么是二进制 ,一种不懂。
2006-06-29 11:56
song4
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:38
帖 子:1533
专家分:4
注 册:2006-3-25
收藏
得分:0 
看了半天,郁闷半天
是个八哥....
郁闷
还好问了aogun怎么回事,要不我就疯了

嵌入式 ARM 单片机 驱动 RT操作系统 J2ME LINUX  Symbian C C++ 数据结构 JAVA Oracle 设计模式 软件工程 JSP
2006-06-29 11:59
huanguyu
Rank: 1
等 级:新手上路
帖 子:90
专家分:0
注 册:2006-4-26
收藏
得分:0 

谢谢两位
下次我会注意的


看越多书就发现自己越无知 于是就越想知道更多
2006-06-29 16:59
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 


s3=s1+s2; //line 95 here is error, no match for 'operator='

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-06-30 09:53
aogun
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:638
专家分:0
注 册:2006-4-5
收藏
得分:0 
以下是引用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

世界上总共有 10 种人,一种懂得什么是二进制 ,一种不懂。
2006-06-30 09:59
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
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;
}

[此贴子已经被作者于2006-6-30 11:33:13编辑过]


自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-06-30 11:30
huanguyu
Rank: 1
等 级:新手上路
帖 子:90
专家分:0
注 册:2006-4-26
收藏
得分:0 
谢谢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

看越多书就发现自己越无知 于是就越想知道更多
2006-06-30 13:31
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
you should know the meaning of const and you should know what is standard in C/C++

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-06-30 13:35
huanguyu
Rank: 1
等 级:新手上路
帖 子:90
专家分:0
注 册:2006-4-26
收藏
得分:0 

谢谢kai


看越多书就发现自己越无知 于是就越想知道更多
2006-06-30 17:10
快速回复:运行下这个,看看怎么回事
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016812 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved