-2

I am trying to compile 3 files total and can not get it to. The code works in visual++. I have uploaded all 3 files in the same dir and used the following command.

g++ -o edit Album.cpp lab8.cpp

My file names are listed below Album.cpp Album.h lab8.cpp

Note the code was written in visual studio C++ and compiled just fine there.

Results in the following

lab8.cpp: In function ‘std::vector read_album_file(std::string)’: lab8.cpp:142:25: error: no matching function for call to ‘std::basic_ifstream::basic_ifstream(std::string&)’ ifstream read (filename);// the ifstream is used to read from the file ^ lab8.cpp:142:25: note: candidates are: In file included from lab8.cpp:38: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; 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 ‘std::string {aka std::basic_string}’ 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] basic_ifstream() : __istream_type(), _M_filebuf() ^ /usr/include/c++/4.8/fstream:453:7: note: candidate expects 0 arguments, 1 provided /usr/include/c++/4.8/fstream:427:11: note: std::basic_ifstream::basic_ifstream(const std::basic_ifstream&) class basic_ifstream : public basic_istream<_CharT, _Traits> ^ /usr/include/c++/4.8/fstream:427:11: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string}’ to ‘const std::basic_ifstream&’

Dale
  • 1
  • 2

1 Answers1

0

Look at the constructor prototype of ifstream. It takes a const char * and a optional argument, soyou need to write filename.c_str()

Dante
  • 404
  • 2
  • 10
  • Thanks it works now! Wonder why it worked in Visual and not unix with those mistakes. – Dale Oct 13 '14 at 04:13
  • No problem. That's a good question I don't know about that, might be a different implementation but you should check – Dante Oct 13 '14 at 04:34
  • @Dale Because this is valid in C++11 but not in C++03. It's a different version of C++. If you'd provided the `-std=c++11` flag to GCC then it would have worked (since it looks like you're using GCC 4.8). – Lightness Races in Orbit Feb 07 '17 at 12:55