c++ - 无法在 Ubuntu 上编译 C++ 程序

标签 c++ linux c++11

我使用 OSX 在 C++ 中编程,但我是 Linux 上的 C++ 编译器的新手。我有一个包含两个 .cpp 文件和一个 .h 文件的程序,它们在我的 Mac 上编译并成功运行,但在 Ubuntu 上出现编译错误。确实安装了gcc和g++编译器和build-essential,也安装了apt-get update和update,网上搜了下,没用。我的 Ubuntu 是否缺少一些库包或需要一些额外的配置来解决问题?

这是我的程序在 Ubuntu 上的编译错误:

WordCombine.cpp: In function ‘int BuildTrie(const string&, Trie*&, std::vector<std::basic_string<char> >&)’:
WordCombine.cpp:21:37: error: no matching function for call to ‘std::basic_ifstream<char>::basic_ifstream(const string&, const openmode&)’
  ifstream ifs(filename, ifstream::in);
                                     ^
WordCombine.cpp:21:37: note: candidates are:
In file included from WordCombine.cpp:10:0:
/usr/include/c++/4.8/fstream:467:7: note: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
       basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
       ^
/usr/include/c++/4.8/fstream:467:7: note:   no known conversion for argument 1 from ‘const string {aka const std::basic_string<char>}’ to ‘const char*’
/usr/include/c++/4.8/fstream:453:7: note: std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT = char; _Traits = std::char_traits<char>]
       basic_ifstream() : __istream_type(), _M_filebuf()
       ^
/usr/include/c++/4.8/fstream:453:7: note:   candidate expects 0 arguments, 2 provided
/usr/include/c++/4.8/fstream:427:11: note: std::basic_ifstream<char>::basic_ifstream(const std::basic_ifstream<char>&)
     class basic_ifstream : public basic_istream<_CharT, _Traits>
           ^
/usr/include/c++/4.8/fstream:427:11: note:   candidate expects 1 argument, 2 provided

这是我的 g++ -v 信息:

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.2-19ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) 

最佳答案

您正在尝试使用 std::basic_ifstream<char>::basic_ifstream(const string&, const openmode&)构造函数,并根据 this ,那是 C++11 的补充。

您可能需要添加 -std=c++11到您的命令行,因为它不是默认设置。

关于c++ - 无法在 Ubuntu 上编译 C++ 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29344723/

相关文章:

C++0x 线程没有加速

c++ - std::atomic 信号安全吗?

c++ - 我如何创建一个循环,在找到字符串中的特定字符后插入一个字符?

python - 如何拖放带有自定义项目小部件的 QListWidget 项目?

Linux,如何执行可执行/不可执行文件?

linux - 获取不同 HPUX unix 服务器的 CPU 和内存信息

c++ - C++ 模板函数的重载

c++ - Fscanf 未从文件的最后一行读取预期值 (C/C++)

c++ 模板:boost::mpl::transform with template 模板参数

c - 这个程序的输出结果是这样的