新微赢技术网

标题: 运行下这个,看看怎么回事 [打印本页]

作者: 鈊賥wo埋單    时间: 2009-11-3 03:31
标题: 运行下这个,看看怎么回事
今天在两台机子上面运行 结果一个报错 另外一个没报错 一个是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
我这运行没问题,但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
看了半天,郁闷半天
是个八哥....
郁闷
还好问了aogun怎么回事,要不我就疯了
作者: 乄獨萊◇獨徍    时间: 2009-11-3 03:31
谢谢两位
下次我会注意的
作者: 阳光aiq浪子    时间: 2009-11-3 03:31
s3=s1+s2; //line 95 here is error, no match for 'operator='
作者: ══════    时间: 2009-11-3 03:31
以下是引用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
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
谢谢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
you should know the meaning of const and you should know what is standard in C/C++
作者: →莲佳    时间: 2009-11-3 03:31
谢谢kai




欢迎光临 新微赢技术网 (http://bbs.weiying.cn/) Powered by Discuz! X3.2