error C2679: binary '<<' : no operator defined which takes a right-hand operand

风泥 2012-07-27 11:03:35
#include <iostream>
#include <cstring>
using namespace std;

class CMyString
{
private:
char *str;
public:
CMyString(char *s = NULL)
{
if(s == NULL)
{
str = new char[20];
str = '\0';
}
else
{
str = new char[strlen(s) + 1];
strcpy(str, s);
}
}
~CMyString()
{
if(str != NULL)
{
delete str;
str = NULL;
}
}

public:
int GetLength() const
{
return strlen(str);
}

bool IsEmpty() const
{
if(strlen == 0)
return true;
}

void Empty()
{
str = '\0';
}

char GetAt(int nIndex) const
{
if(nIndex < 0 || nIndex > (strlen(str) - 1))
{
cout << "下标越界" << endl;
}
else
return str[nIndex];
}

char operator[](int nIndex) const
{
if(nIndex < 0 || nIndex > (strlen(str) - 1))
{
cout << "下标越界" << endl;
}
else
return str[nIndex];
}

void SetAt(int nIndex, char ch)
{
if(nIndex < 0 || nIndex > (strlen(str) - 1))
{
cout << "下标越界" << endl;
}
else
{
str[nIndex] = ch;
}
}

// operator char*() const
// {
// return str;
// }

const CMyString& operator=(const CMyString& stringSrc)
{
delete[] str;
str = new char[strlen(stringSrc.str) + 1];
strcpy(str, stringSrc.str);
return (*this);
}

};

int main()
{
//char *s = "hello";
CMyString s1 = "hello";
CMyString s2 = "world";
s1 = s2;
cout << s1 << endl;//问题就在这个地方
return 0;
}
//会报这样的错误error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class CMyString' (or there is no acceptable conversion) 应该怎么避免了,我用的是VC6.0。
...全文
485 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
风泥 2012-08-13
  • 打赏
  • 举报
回复
嗯 多谢各位大神,最近基本已经弄清楚了
zfk198687 2012-07-28
  • 打赏
  • 举报
回复
楼主要用"<<"输出类,就得重载"<<"操作符!
allenbein 2012-07-27
  • 打赏
  • 举报
回复
cout<< 并不认识你的CMyString 类型,要重载的。
pathuang68 2012-07-27
  • 打赏
  • 举报
回复
另外VC6对STL支持不够好,也需要当心。
pathuang68 2012-07-27
  • 打赏
  • 举报
回复
你要用friend函数为CMyString重载<<操作符才行。
笔记本的风扇控制 ---------------------------------------- 09 November 2006. Summary of changes for version 20061109: 1) ACPI CA Core Subsystem: Optimized the Load ASL operator in the case where the source operand is an operation region. Simply map the operation region memory, instead of performing a bytewise read. (Region must be of type SystemMemory, see below.) Fixed the Load ASL operator for the case where the source operand is a region field. A buffer object is also allowed as the source operand. BZ 480 Fixed a problem where the Load ASL operator allowed the source operand to be an operation region of any type. It is now restricted to regions of type SystemMemory, as per the ACPI specification. BZ 481 Additional cleanup and optimizations for the new Table Manager code. AcpiEnable will now fail if all of the required ACPI tables are not loaded (FADT, FACS, DSDT). BZ 477 Added #pragma pack(8/4) to acobject.h to ensure that the structures in this header are always compiled as aligned. The ACPI_OPERAND_OBJECT has been manually optimized to be aligned and will not work if it is byte-packed. Example Code and Data Size: These are the sizes for the OS- independent acpica.lib produced by the Microsoft Visual C++ 6.0 32- bit compiler. The debug version of the code includes the debug output trace mechanism and has a much larger code and data size. Previous Release: Non-Debug Version: 78.1K Code, 17.1K Data, 95.2K Total Debug Version: 155.4K Code, 63.1K Data, 218.5K Total Current Release: Non-Debug Version: 77.9K Code, 17.0K Data, 94.9K Total Debug Version: 155.2K Code, 63.1K Data, 218.3K Total 2) iASL Compiler/Disassembler and Tools: Fixed a problem where the presence of the _OSI predefined control method within complex expressions could cause an internal compiler error. AcpiExec: Implemented full region support for multiple address spaces. SpaceId is now part of the REGION object. BZ 429 ---------------------------------------- 11 Oc
FASMARM v1.42 This package is an ARM assembler add-on for FASM. FASMARM currently supports the full range of instructions for 32-bit and 64-bit ARM processors and coprocessors up to and including v8. Contents: 1. ARM assembly compatibility 2. UAL and pre-UAL syntaxes 3. IT block handling 4. Alternate encodings 5. Output formats 6. Control directives 7. Data definitions 8. Defining registers lists inside macros 9. Half-precision number formatting 10. Variants supported 11. Further information 12. Version history _______________________________________________________________________________ 1. ARM assembly compatibility There are a few restrictions how the ARM instruction set is implemented. The changes are minor and mostly have a minor impact. For the most part the basic instruction outline is the same. Where possible the original style is used but there are some differences: Not everything matches the ARM ADS assembly style, where possible the original style is used but there are some differences 1) label names cannot begin with a digit 2) CPSIE and CPSID formats are changed, use "iflags_aif" form instead of "aif" (eg. "CPSIE iflags_i" instead of "CPSID i") 3) SRS with writeback must have a separating space after the mode number and before "!" (eg. "SRSDB 16 !" instead of "SRSDB 16!") 4) macro, rept, irp, format, if, virtual etc. are all significant changes from the ARM ADS, so you will need to re-write those sections of existing code Original ARM Syntax | fasmarm Syntax ----------------------+---------------------- cpsie a | cpsie iflags_a | srsdb #29! | srsdb #29 ! ;or, | srsdb 29 ! _______________________________________________________________________________ 2. UAL and pre-UAL syntaxes fasmarm supports the original pre-UAL syntax and the newer UAL syntax. These two syntaxes only affect THUMB encodings. UAL stands for: Universal Assembly Language. pre-UAL syntax is selected wi

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧