如何修复此错误? "Method 'str' could not be resolved"

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

本文关键字:could not be resolved str 何修复 错误 Method      更新时间:2023-10-16

这个问题的含义是什么,我该如何解决?
当我尝试使用它时,它似乎不符合str()的功能。
事实上,我想从rhs中取出"字符串"并将其放入this->file,所以如果您有其他想法,那也很好。

无法解析
方法 'str' 无法解析方法 'c_str'

#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);

隐蔽的 ostringstream 到 std::string 和 than 在常量字符中转换 *