我该如何解决这个错误? "Method 'str' could not be resolved"

How can I fix this error? "Method 'str' could not be resolved"

这个问题是什么意思,我该如何解决?
我尝试使用它时,它看起来不符合功能str()
其实我想把rhs的"string"拿来放在this->file里,所以如果你有别的想法也不错

Method 'str' could not be resolved
Method 'c_str' could not be resolved

#include <cstdio>
#include <iostream>
#include <sstream>

class MyFile {
    FILE* file;
    MyFile& operator=(const MyFile& rhs) const;
    MyFile(const MyFile& rhs);
public:
    MyFile(const char* filename) :
            file(fopen(filename, "w")) {
        if (file == NULL) {
            throw std::exception();
        }
    }
    ~MyFile() {
        fclose(file);
    }

    template<typename T>
    MyFile& operator<<(const T& rhs) {
        std::ostringstream ss;
        ss << rhs;
        if (std::fputs(ss.str().c_str(), this->file) == EOF) { // Method 'str' could not be resolved
            throw std::exception();
        }
        return *this;
    }
};

你确定str()是无法解析的函数吗? 相反,我假设 fputs() 无法解析。原因是 fputs 需要一个 const char*,但你给它一个由 str() 返回的 std::string。 试试 fputs(ss.str().c_str(), this->file).

    std::ostringstream oss;
    FILE *file;
    std::string s = oss.str();
    std::cout << s << '\n';
    std::fputs(s.c_str(), file);

将 stringstream 转换为 std::string,然后转换为 const char *