I need to create a bunch of different files and I want to name them something like:
"signal1.dat", "signal2.dat", "signal3.dat", ... , "signal100.dat"
This is what I have so far
std::stringstream ss;
std::string out = "signal";
std::string format = ".dat";
std::string finalName;
int TrkNxtSig = 1;
...
[code]
...
ss << out << TrkNxtSig << format;
finalName = ss.str();
ofstream output(finalName);
TrkNxtSig++;
I get this error
error: no matching function for call to ‘std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(std::string&)’
/usr/include/c++/4.4/fstream:623: note: candidates are: std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/fstream:608: note: std::basic_ofstream<_CharT, _Traits>::basic_ofstream() [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/iosfwd:84: note: std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(const std::basic_ofstream<char, std::char_traits<char> >&)
I'm not sure what that error message is telling me. What is wrong?
It's telling you that there's no constructor for ofstream that takes a std::string as parameter.
Use ofstream output(finalName.c_str()); instead.
This has been fixed in C++11. If you're using gcc, try passing it -std=c++0x.
Related
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!!
I'm using a Unix shell compiler and need to import a Windows .dat file for input. Unfortunately this means there exists native '\r\n' components for carriage returns in the input file.
I'm hoping to scrub these out with something along the lines of the following:
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inputFile;
inputFile.open("myFile.dat");
string array[100];
int i = 0;
while(getline(dataIn, str))
{
str.erase(remove(str.begin(), str.end(), '\n'), str.end());
str.erase(remove(str.begin(), str.end(), '\r'), str.end());
array[0] = str;
i++;
}
return 1;
}
However this is providing the following error:
error: cannot convert ‘__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >’ to ‘const char*’ for argument ‘1’ to ‘int remove(const char*)’
for the first erase(), followed by
error: request for member ‘erase’ in ‘temp.std::basic_string<_CharT, _Traits, _Alloc>::c_str [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]()’, which is of non-class type ‘const char*’
for the second.
I've attempted str.c_str().erase but this has resulted in duplicates of the second error. Any suggestions would be greatly appreciated...
Two problems in the code:
You if you want to use the algorithm function remove, you need to add #include <algorithm>.
To ensure the ::remove (which is a function that removes the file named by the char * argument) isn't picked up, use std::remove.
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.
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()
I have some text files in a folder named foo1, foo2,...,foo5. I tried to write a C++ program
to print out the contents of the files but the compiler is giving an error.
Here is the program.
int main(int argc, char *argv[])
{
string common="foo";
for(int count=1;count<=5;++count)
{
//Convert count to an string.
stringstream ss;
ss<<count;
string numstring=ss.str();
string filename=common+numstring;
ifstream infile(filename);
string line;
//Print out the lines from the file.
while(getline(infile,line))
{
cout<<line<<endl;
}
}
return 0;
}
The compiler is giving an error like
g++ -Wall c++.cpp
c++.cpp: In function ‘int main(int, char**)’:
c++.cpp:26: error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&)’
/usr/include/c++/4.4/fstream:454: note: candidates are: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/fstream:440: note: std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/iosfwd:81: note: std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)
Can someone help me out with this? The output of the program should be what I would get if I typed cat foo* at the terminal.
Replace:
ifstream infile(filename);
with
ifstream infile(filename.c_str());
The error message that you posted shows you this. fstream is a typedef for basic_fstream<>. Your error message says, in essence, that there is no fstream constructor that takes a string&, but there is one that takes a char*.
You can't do this:
ifstream infile(filename);
The ifstream class doesn't know how to take a string as a filename. I does know how to take a character array:
ifstream infile(filename.c_str());
You should have worked this out with a single input file, before you introduced the iteration. Start simple and build up. Test all the way. Never add to code that doesn't work.