gpt4 book ai didi

c++ - 打印较大的字符串 C++ 时出现问题

转载 作者:行者123 更新时间:2023-11-28 05:04:52 25 4
gpt4 key购买 nike

我编写了一个类,用于跟踪错误并在必要时打印出错误消息。现在我正在针对测试仪运行它以确定它是否可以正确检测错误并打印消息,但是我在打印很长的字符串时遇到了问题。该代码在打印诸如“Short Message”之类的小字符串时工作正常,但在针对诸如“短消息”之类的大字符串进行测试时“一些错误信息真的很长“长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长长”它失败了,我不确定我做错了什么。这是我的代码:

标题

#ifndef SICT_ERRORMESSAGE_H__
#define SICT_ERRORMESSAGE_H__
#include <iostream>

namespace sict {
class ErrorMessage {
char* message_;
public:
ErrorMessage();
ErrorMessage(const char* errorMessage);
ErrorMessage& operator=(const ErrorMessage& em) = delete;
ErrorMessage& operator=(const char* errorMessage);
virtual ~ErrorMessage();
void clear();
bool isClear()const;
void message(const char* value);
const char* message() const;
std::ostream& write(std::ostream& ostr)const;
};
std::ostream& operator<<(std::ostream& os, const ErrorMessage& err);
}
#endif

CPP

#define CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include "ErrorMessage.h"
namespace sict{

ErrorMessage::ErrorMessage() {
message_ = nullptr;
}

ErrorMessage::ErrorMessage(const char* errorMessage) {
message_ = nullptr;
message(errorMessage);
}

ErrorMessage& ErrorMessage::operator=(const char* errorMessage) {
clear();
message();
return *this;
}

ErrorMessage::~ErrorMessage() {
delete [] message_;
}

void ErrorMessage::clear() {
delete [] message_;
message_ = nullptr;
}

bool ErrorMessage::isClear() const {
if(message_ == nullptr) {
return true;
}
else {
return false;
}
}
void ErrorMessage::message(const char* value) {
delete [] message_;
message_ = new char[strlen(value) + 1];
strcpy(message_, value);
}

const char* ErrorMessage::message() const {
return message_;
}

std::ostream& ErrorMessage::write(std::ostream& ostr)const {
if (!isClear()) {
ostr << message_;
}
return ostr;
}

std::ostream& operator<<(std::ostream& os, const ErrorMessage& err) {
return err.write(os);
}
}

主要

#include <iostream>
#include "ErrorMessage.h"
using namespace std;
using namespace sict;
int main() {
ErrorMessage T("Testing Error Message");
ErrorMessage e;
cout << T << endl << e << endl << "isClear(): " << (e.isClear() ? "Passed!" : "failed!") << endl;
cout << "===========|\r";
for (int i = 0; i < 10000000; i++) {
if (i % 1000000 == 0) {
cout << "*";
cout.flush();
}
e = "Some error message that is really long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long"
" long long long long long long long long long long long long long";
}
cout << '*' << endl;
cout << e << endl << "isClear(): " << (e.isClear() ? "Failed!" : "Passed!") << endl;
e.message("Short Message");
cout << e << endl << e.message() << endl << "isClear(): " << (e.isClear() ? "Failed!" : "Passed!") << endl;
e.clear();
cout << e << endl << "isClear(): " << (e.isClear() ? "Passed!" : "Failed!") << endl;
return 0;
}

最佳答案

你的赋值运算符有错误:

ErrorMessage& ErrorMessage::operator=(const char* errorMessage) {
clear();
message(); // <-- Here!
return *this;
}

我认为您打算编写 message(errorMessage),它设置存储的错误消息,而不是 message(),它返回存储的错误消息。

尝试改变它,看看是否能解决问题。

关于c++ - 打印较大的字符串 C++ 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45058071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com