C++ 在atom中编译,但不是终端 #包括 #包括 使用名称空间std; int main(){ ofstream fout=ofstream(“fout.txt”); }

C++ 在atom中编译,但不是终端 #包括 #包括 使用名称空间std; int main(){ ofstream fout=ofstream(“fout.txt”); },c++,g++,atom-editor,C++,G++,Atom Editor,这段代码在atom中使用rgbkrk的脚本包(它使用g++)编译得很好,但是当我尝试在终端上使用g++编译它时,我得到以下输出。不知道我做错了什么 #include <iostream> #include <fstream> using namespace std; int main () { ofstream fout = ofstream("fout.txt"); } $g++test.cpp-o hw 在test.cpp中包含的文件中:1: 在/Applic

这段代码在atom中使用rgbkrk的脚本包(它使用g++)编译得很好,但是当我尝试在终端上使用g++编译它时,我得到以下输出。不知道我做错了什么

#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream fout = ofstream("fout.txt");
}
$g++test.cpp-o hw
在test.cpp中包含的文件中:1:
在/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin//包含的文件中/include/c++/v1/iostream:39:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/./include/c++/v1/streambuf:493:64:错误:
基类“std::_1::ios_base”具有私有副本构造函数
_LIBCPP外部模板(类LIBCPP外部模板类型与基本ios)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/。/include/c++/v1/iosfwd:147:32:注意:
首先在“std::_1::basic_ios”的隐式复制构造函数中
这里需要
类_LIBCPP_TEMPLATE_VIS basic_of stream;
^
test.cpp:6:19:注意:在隐式复制构造函数中
“std::uuu 1::基本流”第一个
这里需要
ofstream fout=ofstream(“fout.txt”);
^
/Applications/Xcode.app/Contents/Developer/toolschains/XcodeDefault.xctoolschain/usr/bin/。/include/c++/v1/ios:313:5:注意:
这里宣布为私人
ios_base(const ios_base&);/=删除;
^
生成1个错误。

首先,这实际上不是GCC。我知道看起来是这样,因为您执行了
g++
,但输出表明您在macOS上,而在macOS上
g++
实际上是伪装的叮当声。是的,这很愚蠢

其次,在C++17之前,您不能对不可复制的东西(如流)使用这样的复制初始化。尽管多余的拷贝经常被省略,但它们总是可能的

因此,不是:

$ g++ test.cpp -o hw
In file included from test.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:39:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/streambuf:493:64: error: 
      base class 'std::__1::ios_base' has private copy constructor
_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ios<char>)
                                                               ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:147:32: note: 
      in implicit copy constructor for 'std::__1::basic_ios<char>' first
      required here
    class _LIBCPP_TEMPLATE_VIS basic_ofstream;
                               ^
test.cpp:6:19: note: in implicit copy constructor for
      'std::__1::basic_ofstream<char, std::__1::char_traits<char> >' first
      required here
  ofstream fout = ofstream("fout.txt");
                  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:313:5: note: 
      declared private here
    ios_base(const ios_base&); // = delete;
    ^
1 error generated.
你应该写一个简单的声明:

ofstream fout = ofstream("file.txt");
或:

(ew)

在C++17中,规则发生了变化,省略现在是强制性的,并且不再要求假设副本成为可能。因此,在C++17模式下,您的代码应该可以工作

您的手动
g++
调用不包含任何打开C++17模式的开关,这显然不是您的Clang版本的默认模式

始终明确指定所需的语言标准版本。我的转到命令是:

ofstream fout{"file.txt"};

首先,这实际上不是GCC。我知道看起来是这样,因为您执行了
g++
,但输出表明您在macOS上,而在macOS上
g++
实际上是伪装的叮当声。是的,这很愚蠢

其次,在C++17之前,您不能对不可复制的东西(如流)使用这样的复制初始化。尽管多余的拷贝经常被省略,但它们总是可能的

因此,不是:

$ g++ test.cpp -o hw
In file included from test.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:39:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/streambuf:493:64: error: 
      base class 'std::__1::ios_base' has private copy constructor
_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ios<char>)
                                                               ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:147:32: note: 
      in implicit copy constructor for 'std::__1::basic_ios<char>' first
      required here
    class _LIBCPP_TEMPLATE_VIS basic_ofstream;
                               ^
test.cpp:6:19: note: in implicit copy constructor for
      'std::__1::basic_ofstream<char, std::__1::char_traits<char> >' first
      required here
  ofstream fout = ofstream("fout.txt");
                  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:313:5: note: 
      declared private here
    ios_base(const ios_base&); // = delete;
    ^
1 error generated.
你应该写一个简单的声明:

ofstream fout = ofstream("file.txt");
或:

(ew)

在C++17中,规则发生了变化,省略现在是强制性的,并且不再要求假设副本成为可能。因此,在C++17模式下,您的代码应该可以工作

您的手动
g++
调用不包含任何打开C++17模式的开关,这显然不是您的Clang版本的默认模式

始终明确指定所需的语言标准版本。我的转到命令是:

ofstream fout{"file.txt"};

@肖恩,你想要答案吗section@Shawn我喜欢使用额外警告标志的微妙建议。我喜欢使用额外警告标志的微妙建议。