c++ 运算符重载遇到一个错误D:\c++\实验八.cpp(96) : fatal error C1004: unexpec...
#include<iostream>using namespace std;
#define pi 1024
class mystring
{
public:
mystring():pstr(NULL),length(0){}
~mystring(){
lengrh=0;
if(pstr!=NULL){
delete[] pstr;
}
mystring(int a){
length=a;
}
void set(int a){
length=a;
}
void setp(char c[]){
pstr=new char;
strcpy(pstr,c);
}
friend istream& operator>> (istream&,mystring&);
friend ostream& operator<< (ostream&,mystring&);
bool operator ==(mystring&);
bool operator !=(mystring&);
bool operator >(mystring&);
bool operator <(mystring&);
mystring& operator +=(mystring &a){
this->pstr+=a.pstr;
this->length+=a.length;
return *this;
}
mystring operator =(mystring &a){
this->pstr=a.pstr;
this->length=a.length;
return *this;
}
private:
char* pstr;
int length;
};
friend istream& operator>> (istream &in,mystring &a){
in>>a.pstr>>a.length;
return in;
}
friend ostream& operator<< (ostream &out,mystring &a){
out<<a.pstr<<a.length<<endl;
return out;
}
bool mystring::operator ==(mystring& a){
if(this->pstr==a.pstr&&this->length==a.length){
return true;
}
else{
return false;
}
}
bool mystring::operator !=(mystring& a){
if(this->pstr!=a.pstr||this->length!=a.length){
return true;
}
else{
return false;
}
}
bool mystring::operator >(mystring a){
if(this->pstr>a.pstr&&this->length>a.length){
return true;
}
else{
return false;
}
}
bool mystring::operator <(mystring a){
if(this->pstr<a.pstr&&this->length<a.length){
return true;
}
else{
return false;
}
}
int main()
{
mystring my;
int a;
char c;
cin>>my;
my.set(a);
my.setp(c);
cout<<my;
return 0;
}
我写的程序如上(用的vc6.0)
下面是报错
--------------------Configuration: 实验八 - Win32 Debug--------------------
Compiling...
实验八.cpp
D:\c++\实验八.cpp(96) : fatal error C1004: unexpected end of file found
执行 cl.exe 时出错.
实验八.exe - 1 error(s), 0 warning(s)
这个咋解决呀?大佬们!!
+=重载里面两个指针相加? yuxijian2020 发表于 2021-4-22 23:30
+=重载里面两个指针相加?
#include<iostream.h>
#include<string>
#include<stdlib.h>
class Mystring
{
public:
Mystring();
friend istream& operator>>(istream&,Mystring&);
friend ostream& operator<<(ostream&,Mystring&);
Mystring operator+=(Mystring&);
void operator=(Mystring&);
bool operator==(Mystring&);
bool operator!=(Mystring&);
bool operator>(Mystring&);
bool operator<(Mystring&);
~Mystring();
private:
char*pstr;
int length;
};
Mystring::Mystring()
{
pstr=NULL;
cout<<"调用构造函数!"<<endl;
}
istream& operator>>(istream&in,Mystring&a)
{
cout<<"enter length"<<endl;
in>>a.length;
a.pstr=new char;
cout<<"enter pstr"<<endl;
in>>a.pstr;
return in;
}
ostream& operator<<(ostream&out,Mystring&b)
{
out<<b.length<<" ";
out<<b.pstr<<endl;
return out;
}
Mystring Mystring::operator+=(Mystring&a)
{
char* temp = new char;
strcpy(temp,pstr);
strcat(temp,a.pstr);
pstr = temp;
length = length + a.length;
return *this;
}
void Mystring::operator=(Mystring&b)
{
this->length=b.length;
this->pstr=new char;
strcpy(this->pstr,b.pstr);
}
bool Mystring::operator==(Mystring&a)
{
if(length==a.length&&strcmp(pstr,a.pstr)==0)
return true;
else
return false;
}
bool Mystring::operator!=(Mystring&a)
{
if(this->length!=a.length||strcmp(this->pstr,a.pstr)!=0)
return true;
else
return false;
}
bool Mystring:: operator>(Mystring&a)
{
if(strcmp(pstr,a.pstr)>0&&this->length>a.length)
return true;
else
return false;
}
bool Mystring::operator<(Mystring&a)
{
if(strcmp(pstr,a.pstr)<0&&this->length<a.length)
return true;
else
return false;
}
Mystring::~Mystring()
{
//delete[]pstr;
cout<<" Mystring 调用析构函数!"<<endl;
}
int main()
{
Mystring a,b,c,d;
cin>>a>>b;
cout<<"a:"<<a<<endl;
cout<<"b:"<<b<<endl;
c=a;
cout<<"c:"<<c<<endl;
d=b+=c;
cout<<"d:"<<d<<endl;
if(a<d) cout<<"a<d yes"<<endl;
else cout<<"a<d no"<<endl;
if(a>b) cout<<"a>b yse"<<endl;
else cout<<"a>b no"<<endl;
if(a==c) cout<<"a==c yse"<<endl;
else cout<<"a==c no"<<endl;
if(b!=c) cout<<"b!=c yes"<<endl;
else cout<<"b!=c no"<<endl;
return 0;
}大佬 后面我改成了这个样子 基本可以运行了,,
页:
[1]