C++ 在ostream中使用运算符const char*
我正在尝试重载运算符C++ 在ostream中使用运算符const char*,c++,operator-overloading,ostream,C++,Operator Overloading,Ostream,我正在尝试重载运算符我将您的所有代码复制到一个文件中,以找出问题所在。在操作符中的测试,当您使用调试器来跨过代码时,调试器在哪条线上声明异常发生?@ SavaVavaCHIK,对不起,我不太熟悉VisualC++调试程序,但它似乎将我指向上面的调试器代码,而不是Error.cpp中的特定行file@SamVarshavchik我想问题出在第57行:return os@SamVarshavchik任何帮助都可以appreciated@elvisi27如果使用std::string,您可以使用con
我将您的所有代码复制到一个文件中,以找出问题所在。在<代码>操作符中的测试,当您使用调试器来跨过代码时,调试器在哪条线上声明异常发生?@ SavaVavaCHIK,对不起,我不太熟悉VisualC++调试程序,但它似乎将我指向上面的调试器代码,而不是Error.cpp中的特定行file@SamVarshavchik我想问题出在第57行:return os@SamVarshavchik任何帮助都可以appreciated@elvisi27如果使用std::string
,您可以使用const char*
解决所有这些问题。实际上,它做的事情和你的班级类似。这将使许多代码变得不必要。(而且,它确实支持流I/O。)似乎它所需要做的只是在if(E.isclear())之后添加“returnOS;”。也非常感谢您对调试器的帮助。
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 ;
}
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));
}
#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;
}
Testing Error Message