Reputation: 123
I am attempting to create a function in C++ that reads all lines from a file, then concatenates all of the lines into a single string, then returns the string. Here is the code I am using to accomplish this:
// at the top of the file
#include "string_helper.hpp" // contains definition for function
#include <vector>
#include <string.h> // used in another function within the same file
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
// the code I believe to be problematic
ifstream input("file.txt");
string result(0);
string line;
while (std::getline(&input,&line)) {
result += line;
}
When I attempt to compile this code, however, I receive this error:
<project root>/src/string_helper.cpp:52:35: error: no matching function for call to ‘getline(std::ifstream (*)(std::__cxx11::string), std::__cxx11::string*)’
while (std::getline(&input,&line)) {
^
In file included from /usr/include/c++/6.3.1/string:53:0,
from include/string_helper.hpp:22,
from <project root>/src/string_helper.cpp:19:
I looked at www.cplusplus.com, and it listed the definition of getline as:
istream& getline (istream& is, string& str);
I am confused, because I am using the & symbol in the declaration of the while loop, but the compiler still says that I am using the incorrect arguments.
Edit: It turns out I accidentally created a function. The real declaraction for input
was:
ifstream input(string(filename));
Because I had to parse filename
from a char *
. I did not include this in the original code, because I was trying to make it generic so it would apply to many people. I am relatively new to C++. Simply creating the string outside of the declaration of input
fixed the problem. So I did:
string fname(filename);
ifstream input(fname);
I am sorry.
Upvotes: 2
Views: 12824
Reputation: 11931
Put this code. It will work temporarily you can make it generic.
//#include "string_helper.hpp" // contains definition for function
#include <vector>
#include <string.h> // used in another function within the same file
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
int main()
{
ifstream input("file.txt");
string result;
char line[100];
while (input.getline(line,'\n'))
{
result += line;
}
cout<<"string = "<<result<<endl;
}
Upvotes: 0
Reputation: 6978
Try to make sure that your std::getline(...)
is using the correct type of variables as its parameters.
std::basic_istream<CharT,Traits>& input
std::basic_string<CharT,Traits,Allocator>& str
CharT delim
std::basic_istream<CharT,Traits>&& input
std::basic_string<CharT,Traits,Allocator>& str
CharT delim
std::basic_istream<CharT,Traits>& input
std::basic_string<CharT,Traits,Allocator>& str
std::basic_istream<CharT,Traits>& input
std::basic_string<CharT,Traits,Allocator>& str
CharT delim
You should be able to just change your parameters from their referenced addresses (&
) to their actual value (no &
). When a function asks for a &value
, you can just pass it the value directly, typically.
http://en.cppreference.com/w/cpp/string/basic_string/getline
#include <string>
#include <fstream>
int main()
{
std::ifstream input("C:\\temp\\file.txt");
std::string result;
std::string line;
while (std::getline(input, line)) {
result += line;
}
printf("Made it!\n");
return 0;
}
Upvotes: 0
Reputation: 21619
You are passing the addresses of your variables. Somewhat confusingly for a beginner &
means reference in the context of a variable type but means address of in the context of using a variable.
for that call to work getline would need to accept pointer arguments, which it does not and so there is no matching function call, given the arguments provided.
You don't have to do anything special to pass a reference. The distinction between a reference and a value being passed to a function is made by the function and not the caller.
So just remove those ampersands and it will work ok.
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream input("file.txt");
string result;
string line;
while (getline(input, line))
{
result += line;
}
cout << result << '\n';
}
You also don't need to initialise the result
string. It is initialised to an empty string by default.
Upvotes: 1
Reputation: 167
GetLine's parameters are references. In C++, you do not need to explicitly give a function a reference. The compiler will do that for you.
Therefore, your function call:
std::getline(&input,&line)
will become:
std::getline(input, line)
Upvotes: 3