Can't getline() from istringstream (C++) - c++

I have a previously declared char c[64]; and I'm trying to look at the first word of the output of a pipe:
read(pipe_replacement_to_main[READ_END], c, BUF_SIZE);
istringstream response_stream(string(c));
string response_string;
getline(response_stream, response_string, ' ');
And gcc gives me the following at that fourth line:
error: no matching function for call to ‘getline(std::istringstream (&)(std::string), std::string&, char)’
I can't even figure out how it's trying to call the function. Did I declare the istringstream wrong?

Most vexing parse, add a pair of parenthesis inside the constructor of response_stream.
istringstream response_stream((string(c)));

A nice demonstration of the true "power" of C++.
The way you declared the response_stream variable, it is actually a function rather than type istringstream.

Related

Why do I get "Non-standard syntax; use '&' to create pointer to member" and "no overloaded function takes 2 arguments" errors?

I'm getting these two errors in my code:
Error C3867 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>::c_str': non-standard syntax; use '&' to create a pointer to member 59
Error C2661 'Product::Product': no overloaded function takes 2 arguments 59
It seems like when I'm trying to call my non-default constructor it's only getting 2 arguments even though I'm trying to pass it 4. This is just speculation, but I suspect maybe I need to add some NULL checkers or something? but I don't see how any of the arguments I'm passing it could be NULL so I'm stuck.
Here's my declaration and definition for my non-default constructor:
Product(bool restocking, string name, int quantity, double price); //Declaration
Product::Product(bool restocking, string name, int quantity, double price):InventoryItem(restocking), quantity_(quantity), price_(price) { } //Definition
Product is a derived from InventoryItem
Here's the troublesome piece of code:
void InventorySystem::BuildInventory(void) {
int i{ 0 };
string name_buffer;
string quantity_buffer;
string price_buffer;
ifstream fin("in_inventory.txt");
if (fin) {
while (getline(fin, name_buffer, ';') && i < g_kMaxArray) {
getline(fin, quantity_buffer, ';');
getline(fin, price_buffer, '\n');
p_item_list_[i] = new Product(false, name_buffer, atoi(quantity_buffer.c_str), atof(price_buffer.c_str)); \\ Error on this line
i++;
item_count_++;
}
}
else {
cout << "Error: Failed to open input file." << endl;
}
fin.close();
}
cstr() is a function, so make sure you call it to get the result (rather than treating as a member variable)
p_item_list_[i] = new Product(false, name_buffer, atoi(quantity_buffer.c_str()), atof(price_buffer.c_str()));
Use empty parentheses to call a member function with no parameters:
... atoi(quantity_buffer.c_str()) ...
If the compiler sees c_str without parentheses, it makes a very unreasonable assumption that you want to refer to the function itself, using a pointer to it. This is a rarely-used feature.
To complicate the matters even more, there are two possible syntaxes for pointer to member function, and one of them is non-standard. This is what the compiler complains about. You don't need any of this, so add parentheses to tell the compiler that you want to call the function and not take a pointer to it.
The pair () is the function call operator. Without it you only get the function pointer, no calls are made
But don't use atoi(). See the reason why it should be avoided. Use stoi() instead, and use stod() to get a double
p_item_list_[i] = new Product(false, name_buffer,
stoi(quantity_buffer), stod(price_buffer));
As you can see, the code is much cleaner because there's no .c_str() everywhere, as the sto* family receives std::string directly (which is much better than receiving a const char*)
Another note: don't use such a long lines. No one likes horizontal scrolling

No instance of overloaded function for getline

I want to read wstring from a txt file.
I store the file's name in a variable called SourceFileName and it is wstring varaible. In the txt file, it has the following strings:
112:abc
221:ghj
....
Now I want to read these numbers and its corresponding strings from the txt file into wstring strings, but when I was trying to do that, VS notifies me that no instance of overloaded function for std::getline macthes argument list.
std::wifstream map;
map.open(SourceFileName.c_str());
std::wstring fileID;
std::wstring fileName;
std::getline(map, fileID, L":");//error happens here.
How to fix it?
I also tried std::getline(map, fileID, ":");, doesn't work too.
There is no overload of std::getline that would accept a string literal as the third argument. Both overloads that have it require the delimiter to be a character:
std::getline(map, fileID, L':');
Hint: When the compiler does not find a matching overload, it helps to take a look at what type of arguments the overloads do accept.

C++ - No matching function for call to 'getline'

I cannot figure out the proper syntax for a file istream getline() call
I've tried so many variations of calling getline() with all different kinds of parameters and after looking at several different pieces of documentation and it just won't work.
std::ifstream in("file.txt");
char tmp;
std::getline(tmp, in);
This one results in
../directory/file.cpp:178:2: error: no matching function for call to 'getline'
std::getline(tmp, in);
^~~~~~~~~~~~
But other documentation says
std::ifstream in("file.txt");
char tmp;
in.getline(tmp);
which also spits out
../directory/file.cpp:179:5: error: no matching member function for call to
'getline'
in.getline(tmp);
^~~~~~~~~~~~
All I need to do is read a file line by line and I can't figure it out. Could someone please point me in the right direction? I can provide more information if needed.
getline() reads string, but you pass it a single char.
Use it like this:
std::ifstream in("file.txt");
std::string tmp;
std::getline(in, tmp);

C++ Getline issues (No instance of overloaded function "getline"

I know I know.
This question has been asked before, but I've looked at all the answers and none seem to solve my problem. When I use the getline function to get the contents of a line in the file, it doesn't work.
getline(file, line);
'File' is declared here:
ifstream File;
File.open("fruit.txt");
and 'line' is declared here:
int line = 0;
Getline is underlined in red with this message:
getline
no instance of overloaded function "getline" matches the argument list
argument types are :(std::ifstream, int)
What this means is no instance of getline has the argument list of the file stream and an integer.
This makes no sense as all the other questions on this matter state exactly that, that the arguments are the file stream and an integer.
What am I doing wrong?
EDIT:
Here is the full code:
ifstream fruitFile;
fruitFile.open("fruit.txt");
int line = 0;
int C_FRUIT = getline(fruitFile, line);
fruitFile.close();
The first line should be a number, and I need it.
getline() will read one line of text. It can't read directly an int. This is why you get your error message.
You have to be aware that there are two getline(). There is one which is istream::getline() and std::getline(). Both have different signatures. The first is a member function of a stream and is defined in the stream header; the latter is defined in the <string> header.
But pay attention: the return value of std::getline() is not an int ! It's a stream reference. This is why you get a second compiler error.
Finally if you want to read an integer x, it's easier to use extractors:
int value;
fruitFile >> value;
fruitFile.ignore(SIZE_MAX, '\n'); // in case you'd need to go to next line
Or if you really want to read an int in a full line:
string line;
getline(fruitFile, line);
stringstream sst(line); // creates a string stream: a stream that takes line as input
sst >> value;
The second argument of getline needs to be a string: http://www.cplusplus.com/reference/string/string/getline/
I think what you try to achieve is:
ifstream fruitFile;
fruitFile.open("fruit.txt");
int line = 0;
fruitFile >> line
fruitFile.close();
I faced the same error. add this to your code to solve the problem
Add the string library
include <string>
Add the below function call, where string_variable should be of type string.
std::getline(cin, sting_variable)

How do I fix a "No instance of overloaded function" error with cin.getline?

string add_text()
{
const int string_size(30);
string add_string;
cin.getline (add_string,string_size,'\n');
return add_string;
}
When I compile the program with the function above, I get an error. Note that I am using using namespace std; earlier in the program. If it helps I am using Code Blocks and have also tried compiling the program using the Visual Studio 11 Professional Beta.
The Error:
C:\Users\Jonathan\Documents\Code_Blocks\test_12\main.cpp|32|error: no matching function for call to 'std::basic_istream<char, std::char_traits<char> ::getline(std::string&, const int&, char)'|
Any help with this issue will be greatly appreciated, thanks in advance!
You're calling std::istream::getline(char *, streamsize) - you can see it requires a char * instead of the std::string you're passing.
Use std::getline(std::istream&, std::string&, char) instead, like so:
getline(cin, add_string, '\n');
istream::getline doesn't take a std::string as parameter, but a char*. You can fix it by calling the function correctly.
So, provided you know the max size of the line:
char buff[256]; //or whatever length
cin.getline (buff,string_size,'\n');
string add_string(buff);
Don't use member .getline()s, but global std::getline().
Member .getline()s can only use char * as buffer, while std::getline() can use std::string.
cin.getline is a member function. As such, it is not overloaded to account for every possible type in existence. So, developers overload the global version (i.e., std::getline).