代码之家  ›  专栏  ›  技术社区  ›  Glaz

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

  •  0
  • Glaz  · 技术社区  · 7 年前

    “一些非常长的错误消息” “隆隆隆隆隆隆隆隆隆隆隆隆隆隆隆隆”

    标题

    #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
    

    清洁石油产品

    #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;
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   templatetypedef    7 年前

    赋值运算符中有错误:

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

    我想你是想写信 message(errorMessage) ,设置存储的错误消息,而不是 message() ,返回存储的错误消息。