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

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&’

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

Related

basic_string.h errors on porting from with gcc 4.6.3 to 5.4.0

Re posted (although not easy to format when some of the g++ output lines seen to be several 100 chars long!).
I have a c++ program that compiles fine on an Ubuntu 12.04 using g++ version 4.6.3. I decided to use a more up to date O/S and g++ - but i'm getting a few strange compile errors. Now I'm using Ubuntu 16.04 and g++ 5.4.0. What is strange is the initial error message I get to .cpp source file does not point to a line of code that should generate that type of message. The errors I see are:
In file included from /usr/include/c++/5/string:52:0,
from pkcs11_test.h:17,
from pkcs11_attr.h:13,
from pkcs11_attr.cpp:16:
/usr/include/c++/5/bits/basic_string.h: In instantiation of ‘std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
[with _CharT = unsigned char; _Traits = std::char_traits<unsigned char>; _Alloc = sec_allocator<unsigned char>]’:
pkcs11_attr.cpp:48:9: required from here
/usr/include/c++/5/bits/basic_string.h:399:62: error: no matching function for call to
‘std::__cxx11::basic_string<unsigned char, std::char_traits<unsigned char>, sec_allocator<unsigned char>
>::_Alloc_hider::_Alloc_hider(std::__cxx11::basic_string<unsigned char, std::char_traits<unsigned char>,
sec_allocator<unsigned char> >::pointer, const allocator_type&)’
: _M_dataplus(_M_local_data(), __str._M_get_allocator()) // TODO A traits
/usr/include/c++/5/bits/basic_string.h:108:2: note: candidate: std::__cxx11::basic_string<_CharT, _Traits,
_Alloc>::_Alloc_hider::_Alloc_hider(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::pointer, const _Alloc&) [with
_CharT = unsigned char; _Traits = std::char_traits<unsigned char>; _Alloc = sec_allocator<unsigned char>;
std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::pointer = unsigned char*]
_Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc())
/usr/include/c++/5/bits/basic_string.h:108:2: note: no known conversion for argument 2 from
‘const allocator_type {aka const std::allocator<unsigned char>}’ to ‘const sec_allocator<unsigned char>&’
/usr/include/c++/5/bits/basic_string.h:106:14: note: candidate: std::__cxx11::basic_string<unsigned char,
std::char_traits<unsigned char>, sec_allocator<unsigned char> >::_Alloc_hider::_Alloc_hider(const
std::__cxx11::basic_string<unsigned char, std::char_traits<unsigned char>, sec_allocator<unsigned char> >::_Alloc_hider&)
struct _Alloc_hider : allocator_type // TODO check __is_final
/usr/include/c++/5/bits/basic_string.h:106:14: note: candidate expects 1 argument, 2 provided
makefile:16: recipe for target 'pkcs11_attr.o' failed
make: *** [pkcs11_attr.o] Error 1
The error message doesn't actually point to sensible line it points to the return statement - as per below.
The code is:
byteString CkAttribute::asByteString () const {
byteString retVal;
if( pValue )
copyCkBStringToBString( (unsigned char *)pValue, ulValueLen, retVal );
// end if
<<<<< error points to the below line>>>>
return retVal;
}
The 12.04 Ubuntu is 32 bit and the new 16.04 Ubuntu is 64 bit. But i have installed g++multilib and tried to compile it with -m32 on the new platform. However, I get the exact same error. The program I'm compiling is quite big and consists of over 12 .cpp source files - all of which compile just fine - it is just this one error in this one source file. It is driving me mad!!

Shortening GCC error messages

Whenever gcc can't find a matching overload for a function with multiple overloads, it gives lines and lines of errors, explaining which overload was tried and why it was not used.
While often it is useful, it is also often not, as the problem as a simple typo at the call site. And in this particular case, it is not even helpful, because it will take considerable time to even find out which line is ultimately responsible for this issue.
Is there any command line switch to GCC to shorten the output and only include the actual triggering line? For example:
#include <string>
#include <iostream>
struct Z{};
void foo() {
std::string s;
Z z;
std::cout << z; // typo - meant s
}
See error output: https://godbolt.org/g/wz5vL2
Small addition: third party solutions (STLFilt, gccfilter, etc) do not fit the bill, because a) my work environment is not welcoming towards installing 3rd party apps and b) they tend to become unmaintained and stop working with the next compiler upgrade
One way is to use -Wfatal-errors. It changes the error message from
<source>: In function 'void foo()':
<source>:11:15: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Z')
std::cout << z; // typo - meant s
~~~~~~~~~~^~~~
In file included from /opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/iostream:39:0,
from <source>:2:
/opt/compiler-explorer/gcc-7.3.0/include/c++/7.3.0/ostream:108:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
operator<<(__ostream_type& (*__pf)(__ostream_type&))
many more lines of errors
to
<source>: In function 'void foo()':
<source>:11:15: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Z')
std::cout << z; // typo - meant s
~~~~~~~~~~^~~~
compilation terminated due to -Wfatal-errors.
Compiler returned: 1
The only downside is you will only get the first error. If your compile times are long then this isn't the greatest as you wouldn't be able to fix any other errors until you fix that first one and recompile.

Error compiling Nana example (nana v1.1.2/1.1.3) with gcc 4.8.4/clang3.4

I tried the latest nana (1.1.2/1.1.3) downloaded from Github. The library compiles fine. When I try to compile and link any sample program, with linux (gcc 4.8.4, clang 3.4) I get the following errors:
enter In file included from /installs/nana/include/nana/gui/wvl.hpp:20:
In file included from /installs/nana/include/nana/gui/widgets/form.hpp:16:
In file included from /installs/nana/include/nana/gui/widgets/widget.hpp:17:
/installs/nana/include/nana/internationalization.hpp:69:7: error: invalid operands to binary expression ('std::wstringstream' (aka 'basic_stringstream<wchar_t>') and 'nana::string' (aka 'basic_string<char>'))
ss << nana::string(nana::charset(arg));
~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8 /system_error:186:5: note: candidate function [with _CharT = wchar_t, _Traits = std::char_traits<wchar_t>] not viable: no known conversion from 'nana::string' (aka 'basic_string<char>') to 'const std::error_code' for 2nd argument
operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/ostream:108:7: note: candidate function not viable: no known conversion from 'nana::string' (aka 'basic_string<char>') to '__ostream_type &(*)(__ostream_type &)' for 1st argument
operator<<(__ostream_type& (*__pf)(__ostream_type&))
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/ostream:117:7: note: candidate function not viable: no known conversion from 'nana::string' (aka 'basic_string<char>') to '__ios_type &(*)(__ios_type &)' for 1st argument
operator<<(__ios_type& (*__pf)(__ios_type&))
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/ostream:127:7: note: candidate function not viable: no known conversion from 'nana::string' (aka 'basic_string<char>') to 'std::ios_base &(*)(std::ios_base &)' for 1st argument
operator<<(ios_base& (*__pf) (ios_base&))
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/ostream:166:7: note: candidate function not viable: no known conversion from 'nana::string' (aka 'basic_string<char>') to 'long' for 1st argument
operator<<(long __n)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/ostream:170:7: note: candidate function not viable: no known conversion from 'nana::string' (aka 'basic_string<char>') to 'unsigned long' for 1st argument
operator<<(unsigned long __n)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/ostream:174:7: note: candidate function not viable: no known conversion from 'nana::string' (aka 'basic_string<char>') to 'bool' for 1st argument
operator<<(bool __n)here
CMAKE:
cmake_minimum_required(VERSION 3.2)
project(nana_ui)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11
-DSTD_CODECVT_NOT_SUPPORTED")
SET(NANA_BASE "/installs/nana")
include_directories(${NANA_BASE}/include
/usr/include/freetype2)
link_directories(${NANA_BASE}/lib)
set(SOURCE_FILES main.cpp)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${NANA_BASE}/lib/libnana.a
X11 pthread rt Xft png asound)
Example cpp:
#include <nana/gui.hpp>
#include <nana/gui/widgets/label.hpp>
int main()
{
using namespace nana;
form fm;
label lb(fm, fm.size());
lb.caption(L"Hello, World");
fm.show();
exec();
}
Any Help is appreciated.
Adding #define NANA_UNICODE in config.hpp, then recompile the library and examples

C++ writing to file error in Linux/Ubuntu?

I have been trying to learn C++ recently, but I have stumbled across some errors. For example, when I try to run this code to ask the user what they want outputted to a file:
#include <iostream>
#include <cstdio>
using namespace std;
main() {
string output; //Declare variables before starting
FILE * file = fopen("newfile.txt","w"); //creates file
cout << "Entire something that you want to be written to the file: " << endl;
cin.getline(output, 256); //Asks what you want to put into file
fprintf(file, output); //Puts output into file
fclose(file); //closes file
return 0;
}
using
g++ -o main test.cpp
I get this error:
test.cpp: In function ‘int main()’:
test.cpp:10:25: error: no matching function for call to ‘std::basic_istream<char>::getline(std::string&, int)’
cin.getline(output, 256);
^
test.cpp:10:25: note: candidates are:
In file included from /usr/include/c++/4.8/iostream:40:0,
from test.cpp:1:
/usr/include/c++/4.8/istream:618:5: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(std::basic_istream<_CharT, _Traits>::char_type*, std::streamsize, std::basic_istream<_CharT, _Traits>::char_type) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::char_type = char; std::streamsize = long int]
basic_istream<char>::
^
/usr/include/c++/4.8/istream:618:5: note: candidate expects 3 arguments, 2 provided
/usr/include/c++/4.8/istream:427:7: note: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::getline(std::basic_istream<_CharT, _Traits>::char_type*, std::streamsize) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>; std::basic_istream<_CharT, _Traits>::char_type = char; std::streamsize = long int]
getline(char_type* __s, streamsize __n)
^
/usr/include/c++/4.8/istream:427:7: note: no known conversion for argument 1 from ‘std::string {aka std::basic_string<char>}’ to ‘std::basic_istream<char>::char_type* {aka char*}’
test.cpp:11:22: error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘const char*’ for argument ‘2’ to ‘int fprintf(FILE*, const char*, ...)’
fprintf(file, output);
^
Could someone please help me? And please forgive me if this is something that can be easily solved, I am fairly new to C++ and do not quite understand it yet.
The header for string is missing:
#include <string>
Without it, sring isn't defined, and everywhere you use output, you'll have errors
With the include you'll have a lot less errors. But this line has another issue (as πάντα ῥεῖ already pointed out):
cin.getline(output, 256);
because cin.getline() expects a char* and the length. If you want to use a string, you have to use the function getline(), without size (limited to strings maximume size) and on an istream:
getline(cin, output);
Last remark: you are of course free to mix c-style io and streams. But you could win from getting used to streams for all your file io.
The error occurs at the line
cin.getline(output, 256);
According to the documentation for std::istream::getline, the first argument for cin.getline() should be a char * and not a std::string as you have declared it.
Try changing the declaration of output to a char * like so
char[256] output;
Edit: Using std::getline as the others have said would be a better idea though.

Compile errors with linked files in C++

Edit: I've reduced a majority of the errors with the std:: prefix on string declarations. However, there seems to be a problem with a few functions, particularly the definitions of those functions who have a string as a parameter.
Edit #2: Updated my code (I DID have the std:: prefix on my function declarations, but hadn't reflected it in my post). Please see the very bottom for errors displayed when I add the std:: prefix to string parameters in the problematic functions.
I have a header file movie.h with the following code (relevant code):
#include <string>
class Movie
{
public:
void addMovieName(std::string movie);
void addLastName(std::string nameLast);
void addFirstName(std::string nameFirst);
private:
string movieName,
directorLastName,
directorFirstName,
directorFullName;
};
And an implementation file movie.cpp like this (relevant code):
#include "movie.h"
// addFirstName, addLastName, and addMovie name all do the same things
// so I'm only including one since they all generate the same error
void Movie::addFirstName(string nameFirst)
{
directorFirstName = nameFirst.resize(10, ' ');
}
Upon compilation, I get the following errors:
g++ -c movie.cpp -o movie.o
movie.cpp:225: error: variable or field ‘addFirstName’ declared void
movie.cpp:225: error: ‘int Movie::addFirstName’ is not a static member of ‘class Movie’
movie.cpp:225: error: ‘string’ was not declared in this scope
movie.cpp:226: error: expected ‘,’ or ‘;’ before ‘{’ token
movie.cpp:240: error: variable or field ‘addLastName’ declared void
movie.cpp:240: error: ‘int Movie::addLastName’ is not a static member of ‘class Movie’
movie.cpp:240: error: ‘string’ was not declared in this scope
movie.cpp:241: error: expected ‘,’ or ‘;’ before ‘{’ token
movie.cpp:255: error: variable or field ‘addMovieName’ declared void
movie.cpp:255: error: ‘int Movie::addMovieName’ is not a static member of ‘class Movie’
movie.cpp:255: error: ‘string’ was not declared in this scope
movie.cpp:256: error: expected ‘,’ or ‘;’ before ‘{’ token
make: *** [movie.o] Error 1
Some of who have said I need to prepend std:: to the string parameters in the function definitions.
Upon doing this:
// adding std:: prefix
void Movie::addFirstName(std::string nameFirst)
{
directorFirstName = nameFirst.resize(10, ' ');
}
I get the following errors. Note that I only changed it for a single function. The first errors I don't understand, whereas the rest remain the same as before.
g++ -c movie.cpp -o movie.o
movie.cpp: In member function ‘void Movie::addFirstName(std::string)’:
movie.cpp:227: error: no match for ‘operator=’ in ‘((Movie*)this)->Movie::directorFirstName = nameFirst.std::basic_string<_CharT, _Traits, _Alloc>::resize [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](10u, 32)’
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:485: note: candidates are: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:493: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/basic_string.h:504: note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
movie.cpp: At global scope:
movie.cpp:240: error: variable or field ‘addLastName’ declared void
movie.cpp:240: error: ‘int Movie::addLastName’ is not a static member of ‘class Movie’
movie.cpp:240: error: ‘string’ was not declared in this scope
movie.cpp:241: error: expected ‘,’ or ‘;’ before ‘{’ token
movie.cpp:255: error: variable or field ‘addMovieName’ declared void
movie.cpp:255: error: ‘int Movie::addMovieName’ is not a static member of ‘class Movie’
movie.cpp:255: error: ‘string’ was not declared in this scope
movie.cpp:256: error: expected ‘,’ or ‘;’ before ‘{’ token
make: *** [movie.o] Error 1
You have not specified the correct namespace for string, string is a member of the std namespace. In this case you need to write std::string since I see no "using namespace std;" in your code.
replace:
string movieName,
directorLastName,
directorFirstName,
directorFullName;
with:
std::string movieName,
directorLastName,
directorFirstName,
directorFullName;
Why are all your private fields pointers? Storing plain ints and floats as fields is perfectly okay. Moreover, you don't need to "initialize" strings in your constructor. If you don't, their default constructors will be called automatically (strings will be empty). And why do you pad strings? Even if you need it when displaying, pad them there.
No one has answered the question yet, so I figured I'd just put it here myself since I figured it out. Basically, my solution was:
Use using std::string in header and implementation files.
Secondly, the declaration:
stringOne = stringTwo.resize(some_number, ' ');
...fails because resize() is a void returning function. Replacing that with two separate statements; namely:
stringOne = stringTwo;
stringOne.resize(/* blah */);
...solves the rest of the errors. Credit to #Jesse for mentioning this in the comments.