phép overload + 2 chuỗi

1 view
Skip to first unread message

truongth

unread,
Aug 9, 2010, 2:49:36 AM8/9/10
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();
}

Pro khanh

unread,
Aug 9, 2010, 4:15:53 AM8/9/10
to oop-h...@googlegroups.com
thư viện của bạn thiếu rồi đó. phải .h nữa chứ.
#include <conio.h>   
#include <iostream.h>   
#include <string.h>.
Mình chỉ thấy vậy. Phần tiếp theo là ở bạn.
Sorry if i wrong.

truongth

unread,
Aug 10, 2010, 12:39:10 AM8/10/10
to OOP-He2010

tùy vào bạn dùng trình biên dịch gì thôi, mình đang dung VS2010 nên ko
cần .h, với lại lỗi này ko phải lỗi do khai báo thư viện.

Toan Nguyen Van

unread,
Aug 10, 2010, 11:48:44 AM8/10/10
to oop-he2010, truongth91
Em xem slide so 8/23 của Buoi5.pdf
Reply all
Reply to author
Forward
0 new messages