truongth
unread,Aug 9, 2010, 2:49:36 AM8/9/10Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to OOP-He2010
cho em hỏi lỗi <error C2110: '+' : cannot add two pointers> là lỗi gì
và tại sao lại xuất hiện lỗi trong đoạn code này đc ko ạ?
CODE
#include <conio.h>
#include <iostream>
#include <string>
using namespace std;
class _STRING
{
private:
char *aCont;
int nLen;
public:
void Set(char s[]);
_STRING();
_STRING(char a[]);
_STRING(char a);
_STRING operator=(_STRING a);
_STRING operator+(_STRING a);
void Gets();
friend _STRING operator+(_STRING a,_STRING b);
};
_STRING operator+(_STRING a,_STRING b)
{
_STRING kq;
if (kq.aCont!=NULL)
{
delete kq.aCont;
kq.aCont = new char[strlen(b.aCont) + strlen(a.aCont)];
strcpy(kq.aCont,a.aCont);
strcat(kq.aCont,b.aCont);
kq.nLen = strlen(b.aCont) + strlen(a.aCont);
}
return kq;
}
void _STRING::Set(char s[])
{
aCont = s;
nLen = strlen(s);
}
_STRING::_STRING()
{
aCont = new char[9];
nLen = 0;
}
_STRING::_STRING(char a[])
{
Set(a);
}
_STRING::_STRING(char a)
{
char b[1];
b[0] = a;
Set(b);
}
_STRING _STRING::operator=(_STRING a)
{
if (this!=NULL)
{
delete aCont;
aCont = a.aCont;
nLen = a.nLen;
}
return *this;
}
_STRING _STRING::operator+(_STRING a)
{
_STRING kq;
if (kq.aCont!=NULL)
{
delete kq.aCont;
kq.aCont = new char[strlen(aCont) + strlen(a.aCont)];
kq.aCont = strcpy(kq.aCont,aCont);
kq.aCont = strcat(kq.aCont,a.aCont);
kq.nLen = strlen(aCont) + strlen(a.aCont);
}
return kq;
}
void _STRING::Gets()
{
cout << aCont << endl;
cout << nLen << endl;
}
void main()
{
_STRING x,y="DH CNTT",z="DHQG";
y.Gets();
z.Gets();
x = "asy" + "asda"; // LỖI XUẤT HIỆN TẠI DÒNG NÀY
x.Gets();
cin.ignore();
}