相关疑难解决方法(0)

为什么我不能移动std :: ofstream?

看看之前的答案,似乎虽然std::ostream不是可移动的,但std::ofstream应该是.但是,这段代码

#include <fstream>

int main()
{
    std::ofstream ofs;
    std::ofstream ofs2{std::move(ofs)};
}
Run Code Online (Sandbox Code Playgroud)

似乎没有在我试过的任何版本的gcc或clang中编译(使用--std = c ++ 11或--std = c ++ 14).编译器错误有所不同,但这是我得到的gcc 4.9.0

6 : error: use of deleted function 'std::basic_ofstream::basic_ofstream(const std::basic_ofstream&)'
Run Code Online (Sandbox Code Playgroud)

根据标准,这是预期的行为吗?

请注意,之前问过一个非常类似的问题(std :: ofstream是否可以移动?)但是从那时起标准似乎发生了变化(详见 为什么不能移动std :: ostream?)使这些答案过时.当然,这些答案都没有解释为什么上面的代码不能编译.

在尝试使用容器时遇到了这个问题ofstream,因为上述情况不起作用.

c++ gcc fstream clang c++11

20
推荐指数
1
解决办法
4730
查看次数

ofstream不在linux上工作

我有一个简单的测试代码:

#include <string>
#include <iostream>
#include <fstream>

int main() {
std::ofstream strm = std::ofstream("test.txt");
strm << "TEST123";
strm.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我在Windows上编译它,它完美地工作.但是当我使用以下命令在debian上编译它时:g ++ - 4.7 -std = c ++ 0x -lpthread TestStream.cpp -ldl -o TestStream比它提供以下输出: 在此输入图像描述

我用google搜索这个错误无济于事.有谁知道如何解决这个问题?我在我的项目中使用了很多流,并且想在linux上编译它.

编辑:所以我现在感谢WinterMute编译,但现在它打印空文件.我该如何解决?

编辑2:不知道为什么,但第二次编译它的工作.谢谢!

c++ linux g++-4.7

3
推荐指数
1
解决办法
6652
查看次数

标签 统计

c++ ×2

c++11 ×1

clang ×1

fstream ×1

g++-4.7 ×1

gcc ×1

linux ×1