gpt4 book ai didi

c++ - 该函数尝试引用已删除的函数,但该函数已设置为删除

转载 作者:行者123 更新时间:2023-11-28 04:53:37 26 4
gpt4 key购买 nike

我正在尝试检查 errormessage 类中的所有函数是否都在主函数中工作,但问题是我在位于 cout << 旁边的“T”和“e”处收到一条错误消息 在 Main.cpp 中

说我正在尝试引用已删除的函数 sict::ErrorMessage::ErrorMessage(const sict::ErrorMessage&)。

但是,我已经在header中设置了删除这个函数。关于为什么会发生此错误的任何想法?

头文件

#ifndef SICT_ERROR_H_
#define SICT_ERROR_H_
namespace sict {
class ErrorMessage {
char* m_message;
public:

ErrorMessage(const char* errorMessage = nullptr);
ErrorMessage(const ErrorMessage& em) = delete;
ErrorMessage& operator=(const ErrorMessage& em) = delete;
virtual ~ErrorMessage();
void clear();
bool isClear() const;
void message(const char* str);
const char* message() const;
};
std::ostream& operator<<(std::ostream& ostr, ErrorMessage);
}
#endif

Cpp文件

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

ErrorMessage::ErrorMessage(const char* errorMessage) {
m_message = nullptr;
message(errorMessage);
}
void ErrorMessage::clear() {
delete[] m_message;
m_message = nullptr;
}
void ErrorMessage::message(const char* str) {
delete[] m_message;
m_message = new char[strlen(str) + 1];
strcpy(m_message, str);
}
const char* ErrorMessage::message() const {
return m_message;
}
ErrorMessage::~ErrorMessage() {
clear();
}
bool ErrorMessage::isClear() const {
if (m_message == nullptr) {
return true;
}
return false;
}
std::ostream& operator<<(std::ostream& ostr, ErrorMessage error) {
if (!(error.isClear())) {
ostr << error.message();
}
return ostr;
}


}

main.cpp

#include <iostream>
#include "ErrorMessage.h"

using namespace std;
using namespace sict;

int main() {
ErrorMessage T("Testing Error Message Module");
ErrorMessage e;
int ret = 0;
bool ok = true;
cout << T << endl << e << endl << "isClear(): " << (e.isClear() ? "Passed!" : "Failed!") << endl;
if (!e.isClear()) ok = false;
cout << endl;
cout << "===========| Long Message\r";
for (int i = 0; i < 10000000; i++) {
if (i % 1000000 == 0) {
cout << "*";
cout.flush();
}
e.message("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;
if (e.isClear()) ok = false;
cout << endl;

e.message("Short Message");
cout << e << endl << e.message() << endl << "isClear(): " << (e.isClear() ? "Failed!" : "Passed!") << endl;
if (e.isClear()) ok = false;

e.clear();
cout << e << endl << "isClear(): " << (e.isClear() ? "Passed!" : "Failed!") << endl;
if (!e.isClear()) ok = false;
cout << endl;

if (ok) {
cout << "You passed all the tests!" << endl;
}
else {
cout << "You did not pass all the tests" << endl;
ret = 1;
}

return ret;
}

最佳答案

你的问题在这里:

std::ostream& operator<<(std::ostream& ostr, ErrorMessage error)

你的 <<正在接受 ErrorMessage按值,所以它必须复制 Te为了传递给函数(因此尝试使用已删除的复制构造函数)

尝试通过 const 引用传递:

std::ostream& operator<<(std::ostream& ostr, const ErrorMessage& error)

关于c++ - 该函数尝试引用已删除的函数,但该函数已设置为删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47645082/

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