在Unix/Linux(Ubuntu)中编译头文件和两个.cpp文件

Compile header file and two .cpp files in Unix/Linux (Ubuntu)

本文关键字:文件 cpp 两个 Unix Linux Ubuntu 编译      更新时间:2023-10-16

我试图编译总共3个文件,但无法编译。代码在visual++中工作。我已经上传了同一目录中的所有3个文件,并使用了以下命令。

g++-o编辑相册.cpp lab8.cpp

下面列出了我的文件名相册.cpp相册.hlab8.cpp

请注意,代码是在visual studio C++中编写的,在那里编译得很好。

导致以下

lab8.cpp:在函数"std::vector read_album_file(std::string)"中:lab8.cpp:142:25:错误:对"std::basic_ifstream::basic_ifstream(std::string&)"的调用没有匹配的函数ifstream读取(文件名);//ifstream用于从文件中读取^lab8.cpp:142:25:注意:候选者是:在lab8.cpp:38:0:/usr/include/c++/4.8/fstream:467:7:注意:std::basic_ifstream&lt_CharT,_Traits>::basic_ifstream(const char*,std::ios_base::openmode)[其中_CharT=char;_Traits=std::char_Traits;std::ios _base::openmode=std:basic_ifstream(const char*__s,ios_base::openmode __mode=ios_base::in)^/usr/include/c++/4.8/fstream:467:7:注意:参数1从"std::string{aka std::basic_string}"到"const char*"没有已知的转换/usr/include/c++/4.8/fstream:453:7:注意:std::basic_ifstream&lt_CharT,_Traits>::basic_ifstream()[其中_CharT=char;_Traits=std::char_Traits]basic_ifstream():__istream_type(),_M_filebuf()^/usr/include/c++/4.8/fstream:453:7:注意:候选者需要0个参数,提供1个/usr/include/c++/4.8/fstream:427:11:注意:std::basic_ifstream::basic_ifstream(const std::basic_ifstream&)class basic_ifstream:公共basic_stream&lt_CharT,_Traits>^/usr/include/c++/4.8/fstream:427:11:注意:参数1从"std::string{aka std::basic_string}"到"const std::basic_ifstream&"没有已知的转换

看看ifstream的构造函数原型。它需要一个const char *和一个可选参数,因此您需要编写filename.c_str()