在 ostream 中使用运算符 const char*

Using operator const char* in ostream

本文关键字:const char 运算符 ostream      更新时间:2023-10-16

我正在尝试重载运算符<<以便可以使用cout打印错误。我需要打印m_message 指向的c-string。谁能帮我解决这个问题?

My Error.h标题 :

ifndef ICT_ERROR_H_
#define ICT_ERROR_H_
#include <iostream>
namespace ict {
class Error {
char* m_message;
public:
// constructors
Error();
Error(const char* errorMessage);
// destructor
virtual ~Error();
// deleted constructor and operator=
Error(const Error& em) = delete;
Error& operator=(const Error& em) = delete;
// operator= for c-style strings
void operator=(const char* errorMessage);
// methods
void clear();
bool isClear()const;
void message(const char* value);
// cast overloads
operator const char*() const;
operator bool()const;
};
// operator << overload prototype for cout
std::ostream& operator<<(std::ostream& os, const Error& E);
}
#endif
Error.cpp
#define _CRT_SECURE_NO_WARNINGS 
#include <cstring>
#include "Error.h"
namespace ict{
Error::Error()
{
m_message = nullptr;
}
Error::Error(const char * errorMessage)
{
m_message = nullptr;
message(errorMessage);
}
Error::~Error()
{
delete[] m_message;
}
void Error::operator=(const char * errorMessage)
{
clear();
message(errorMessage);
}
void Error::clear()
{
delete[] m_message;
m_message = nullptr;
}
bool Error::isClear() const
{
bool status = false;
if (m_message==nullptr) {
status = true;
}
return status;
}
void Error::message(const char * value)
{
delete[] m_message;
m_message = new char[strlen(value)+1];
strcpy(m_message,value);
}
Error::operator const char*() const
{
return m_message;
}
Error::operator bool() const
{
return isClear();
}
***std::ostream& operator<<(std::ostream& os, const Error& E) {
if (E.isClear()) {
}
return os << E.operator const char *();
}***
}
Main.cpp
int main(){
Error T("Testing Error Message"); 
cout << T << endl ;
}

当我执行它时,它给出了正确的输出,但它崩溃并出现以下错误:

引发异常:读取访问冲突。

_First是空的。

调试器:

static size_t __CLRCALL_OR_CDECL length(const _Elem *_First)
{   // find length of null-terminated string
//next statement to be executed ---> return (*_First == 0 ? 0
: _CSTD strlen(_First));
}

我将所有代码复制到一个文件中以找出问题所在。缺少operator << ()中的测试(错误是否明确)。但是,在测试中,此分支不应变为活动状态。

#include <iostream>
#include <cstring>
#define _CRT_SECURE_NO_WARNINGS 
namespace ict {
class Error {
char* m_message;
public:
// constructors
Error();
Error(const char* errorMessage);
// destructor
virtual ~Error();
// deleted constructor and operator=
Error(const Error& em) = delete;
Error& operator=(const Error& em) = delete;
// operator= for c-style strings
void operator=(const char* errorMessage);
// methods
void clear();
bool isClear()const;
void message(const char* value);
// cast overloads
operator const char*() const;
operator bool()const;
};
// operator << overload prototype for cout
std::ostream& operator<<(std::ostream& os, const Error& E);
} // namespace ict

namespace ict{
Error::Error()
{
m_message = nullptr;
}
Error::Error(const char * errorMessage)
{
m_message = nullptr;
message(errorMessage);
}
Error::~Error()
{
delete[] m_message;
}
void Error::operator=(const char * errorMessage)
{
clear();
message(errorMessage);
}
void Error::clear()
{
delete[] m_message;
m_message = nullptr;
}
bool Error::isClear() const
{
bool status = false;
if (m_message==nullptr) {
status = true;
}
return status;
}
void Error::message(const char * value)
{
delete[] m_message;
m_message = new char[strlen(value)+1];
strcpy(m_message,value);
}
Error::operator const char*() const
{
return m_message;
}
Error::operator bool() const
{
return isClear();
}
std::ostream& operator<<(std::ostream& os, const Error& E) {
if (E.isClear()) return os;
return os << E.operator const char *();
}
} // namespace ict
int main(){
ict::Error T("Testing Error Message"); 
std::cout << T << std::endl;
return 0;
}

我在Windows 2013(64位)上使用VS10编译了它并启动了调试器。

如您所见,我在main()中添加了return 0;.实际上,允许将其排除在外,但放置断点是一个很好的点。因此,我得到了以下输出:

Testing Error Message

嗯。那么,您描述的问题在哪里?我不喜欢这个设计(品味问题),但它按预期工作。我寻找潜在的泄漏或错误:

如果用nullptr调用Error::message(),那么strlen()(和strcpy())可能会崩溃。如果您使用类似"不要使用nullptr调用Error::message()"之类的内容来记录 API。我觉得这已经足够了。

要么没有提供所有信息,要么没有在调试器中测试此示例

您真正需要知道的是调试器的工作原理:

  • 单击文本编辑器左侧的灰色条将放置一个断点。在调试模式下,执行将在断点处(自动)停止。相应的源代码可见(升高编辑器选项卡并适当滚动文本。

  • F9...切换当前行中的断点

  • F10...单步执行(单步 - 将函数作为一个语句执行)

  • F11...步入(单步 - 输入功能)

  • 换档F11...跳出(执行代码直到从当前函数返回)

  • F5...执行代码(连续执行直到断点)

所有这些命令都可以在菜单栏和工具栏上找到。但是,记住上述键进行调试更方便。

此外,熟悉本地、监视和调用堆栈。