我使用OSX在C ++中编程,但我不熟悉Linux上的C ++编译器。我有一个程序包含两个.cpp文件和一个.h文件,它们在我的Mac上成功编译和运行,但在Ubuntu上编译错误。我确实安装了gcc和g ++编译器和build-essential,以及apt-get更新和更新,在线搜索,但没有白费。我的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)
答案 0 :(得分:1)
您正在尝试使用std::basic_ifstream<char>::basic_ifstream(const string&, const openmode&)
构造函数,并且根据this,这是C ++ 11的添加。
您可能需要在命令行中添加-std=c++11
,因为它不是默认值。