我有这个地图,在MSVC10中编译得很好:
std::map<std::string, std::ofstream> m_logFiles;
Run Code Online (Sandbox Code Playgroud)
但是在使用g ++ 4.5并启用了C++ 0x的ubuntu上,我收到以下错误消息:
/usr/include/c++/4.5/bits/ios_base.h| 785|error:'std :: ios_base :: ios_base(const std :: ios_base&)'是私有的
通过使用指针而不是对象,我解决了问题.
在网上搜索,我了解到流不是要复制的(为什么有很好的解释).但我的问题是,std :: ofstream是一个可移动的类型吗?如果是,它不应该允许它作为标准容器中的模板参数使用吗?
如果是,那么在这一点上g ++背后是MSVC10吗?(这可以解释为什么它适用于MSVC).我知道要求编译器编写者完全实现甚至不是最终的东西是愚蠢的,但我对未来感到好奇.
使用g ++ 4.6.1没有帮助.
编辑:阅读我进一步挖掘的评论,发现插入导致问题,而不是地图的声明.
阅读Cubbi的链接我尝试了以下内容:
#include <string>
#include <fstream>
#include <map>
using namespace std;
int main()
{
map<string, ofstream> m_logFiles;
ofstream st;
m_logFiles.insert(make_pair<string, ofstream>(string("a"), move(st)));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但仍然没有运气.g ++抱怨使用b删除的拷贝构造函数.
我有这个代码的问题:
#include <fstream>
struct A
{
A(std::ifstream input)
{
//some actions
}
};
int main()
{
std::ifstream input("somefile.xxx");
while (input.good())
{
A(input);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
G ++输出这个:
$ g++ file.cpp
file.cpp: In function `int main()':
file.cpp:17: error: no matching function for call to `A::A()'
file.cpp:4: note: candidates are: A::A(const A&)
file.cpp:6: note: A::A(std::ifstream)
Run Code Online (Sandbox Code Playgroud)
将其更改为此后编译(但这不能解决问题):
#include <fstream>
struct A
{
A(int a)
{
//some actions
}
};
int main()
{
std::ifstream input("dane.dat");
while (input.good())
{
A(5);
}
return 0;
} …Run Code Online (Sandbox Code Playgroud)