尝试使用 ostream 保存文件内容时出错
Error when try to save the content of a file using ostream
我用 C++ 编写了一个程序,并使用 gcc 7.3 对其进行了编译。这是一个在文件中写入字符串的简单程序。
但是只有在使用 gcc 7.3 编译时才会产生编译器错误。使用旧编译器4.8.5编译成功
编译错误如下
In member function 'void CDemoMap::saveFile(std::__cxx11::string&)':
..\src\VerifyProgram.cpp:51:9: error: no match for 'operator<<'
(operand types are 'std::ostream {aka std::basic_ostream}' and
'std::ostream {aka std::basic_ostream}')
cout << print(coutFile)
有谁能帮我解决这个问题吗?
代码如下
#include <map>
#include <iostream>
#include <ostream>
#include <fstream>
using namespace std;
class CDemoMap
{
public:
map<int,int> m_sMap;
void saveFile(std::string &);
std::ostream& print(std::ostream &s);
};
std::ostream& operator << (ostream& s, const CDemoMap &m)
{
if (m.m_sMap.size())
{
s << "-----------------\nSOCKET FQDN MAP\n-----------------\n";
s << "fqdn host:port timestamp\n";
for (map<int,int>::const_iterator iter = m.m_sMap.cbegin(); iter !=
m.m_sMap.cend(); ++iter)
{
s << iter->first << " " << (iter->second);
}
s << endl;
}
return s;
}
std::ostream& CDemoMap::print(std::ostream &s)
{
return s << (*this);
}
void CDemoMap::saveFile(std::string & test)
{
char outFile[50];
snprintf(outFile, sizeof(outFile), "Data:%s", test.c_str());
std::ofstream coutFile;
coutFile.open("Test.txt", std::ios::app);
cout << print(coutFile);
coutFile.close();
}
int main() {
CDemoMap cSocket;
string str = "Hello";
cSocket.saveFile(str);
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
在 4.8.5 以下行:
cout << print(coutFile);
翻译成:
void* v = print(coutFile);
std::cout << v;
因为在 C++11 之前有运算符将 ostream 转换为 void*
以检查流是否没有错误 from reference:
operator void*() const;
(1) (until C++11)
explicit operator bool() const;
(2) (since C++11)
Checks whether the stream has no errors.
1) Returns a null pointer if fail() returns true, otherwise returns a
non-null pointer. This pointer is implicitly convertible to bool and
may be used in boolean contexts.
2) Returns true if the stream has no
errors and is ready for I/O operations. Specifically, returns !fail().
自 C++11 起,代码无法编译,因为转换为 void* 被禁用。
为什么要将 return 类型的打印 - ostream 传递给另一个 ostream?
它应该是:
print(coutFile); // there is no need to pass ostream to cout
我用 C++ 编写了一个程序,并使用 gcc 7.3 对其进行了编译。这是一个在文件中写入字符串的简单程序。 但是只有在使用 gcc 7.3 编译时才会产生编译器错误。使用旧编译器4.8.5编译成功
编译错误如下
In member function 'void CDemoMap::saveFile(std::__cxx11::string&)': ..\src\VerifyProgram.cpp:51:9: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream}' and 'std::ostream {aka std::basic_ostream}') cout << print(coutFile)
有谁能帮我解决这个问题吗? 代码如下
#include <map>
#include <iostream>
#include <ostream>
#include <fstream>
using namespace std;
class CDemoMap
{
public:
map<int,int> m_sMap;
void saveFile(std::string &);
std::ostream& print(std::ostream &s);
};
std::ostream& operator << (ostream& s, const CDemoMap &m)
{
if (m.m_sMap.size())
{
s << "-----------------\nSOCKET FQDN MAP\n-----------------\n";
s << "fqdn host:port timestamp\n";
for (map<int,int>::const_iterator iter = m.m_sMap.cbegin(); iter !=
m.m_sMap.cend(); ++iter)
{
s << iter->first << " " << (iter->second);
}
s << endl;
}
return s;
}
std::ostream& CDemoMap::print(std::ostream &s)
{
return s << (*this);
}
void CDemoMap::saveFile(std::string & test)
{
char outFile[50];
snprintf(outFile, sizeof(outFile), "Data:%s", test.c_str());
std::ofstream coutFile;
coutFile.open("Test.txt", std::ios::app);
cout << print(coutFile);
coutFile.close();
}
int main() {
CDemoMap cSocket;
string str = "Hello";
cSocket.saveFile(str);
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}
在 4.8.5 以下行:
cout << print(coutFile);
翻译成:
void* v = print(coutFile);
std::cout << v;
因为在 C++11 之前有运算符将 ostream 转换为 void*
以检查流是否没有错误 from reference:
operator void*() const; (1) (until C++11) explicit operator bool() const; (2) (since C++11) Checks whether the stream has no errors.
1) Returns a null pointer if fail() returns true, otherwise returns a non-null pointer. This pointer is implicitly convertible to bool and may be used in boolean contexts.
2) Returns true if the stream has no errors and is ready for I/O operations. Specifically, returns !fail().
自 C++11 起,代码无法编译,因为转换为 void* 被禁用。
为什么要将 return 类型的打印 - ostream 传递给另一个 ostream? 它应该是:
print(coutFile); // there is no need to pass ostream to cout