getline error

closed account (o1Tp216C)
Hi, I'm writing a program that allows me to read a text.
Now it doesn't do any of this because I'm stuck at "getline" an error ([Error] no matching function for call to 'getline (std :: string &, std :: string &)') I've tried with all the tutorials but don't want any know dev to accept getline, how do i do it?

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
int main(){
	std::string take_code;
	std::cin>>take_code;
	std::string code;
	getline(take_code,code);
	std::cout<<code;
	return 0;
}
https://www.cplusplus.com/reference/string/string/getline/
You need to getline from somewhere that's a stream.

Maybe
1
2
3
4
5
6
std::string take_code;
std::cin>>take_code;
std::ifstream in(take_code);
std::string code;
getline(in,code);

closed account (o1Tp216C)
it still not working, is saying to me ([error] variable ‘std::ifstream in’ has initializer but incomplete type) i researched for this but i dont understand what mean
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

int main(){
std::string take_code;
std::cin>>take_code;
std::ifstream in (take_code);
std::string code;
getline(in,code);
std::cout<<code;
return 0;
}


meanwhile i found a method to reduce or simplify the code but i dont know if this will change the resolution of this problem
variable ‘std::ifstream in’ has initializer but incomplete type

add
#include <fstream>
closed account (o1Tp216C)
([error] no matching function for call to ‘std::basic_ifstream<char>::basic_ifstream(std::string&)’)

so i dont think that include fstream improve things now
Okay, now compile with at least C++11. -std=c++11 if GCC.

Or, do std::ifstream in(take_code.c_str());
Last edited on
closed account (o1Tp216C)
unbelievable it still doesnt work!! maybe we have to see this problem in a different view
Why don't you show us the latest code and the way you compile the program.
closed account (o1Tp216C)
ok i actually tried it on dev instead of the forum compiler and it work in part, in fact then skip all the code under the cin
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
#include <fstream>

int main(){
	std::string take_code;
	std::cin>>take_code;
	std::ifstream in (take_code.c_str());
	std::string code;
	getline(in,code);
	std::cout<<code;
	return 0;
}
Check if your file is being opened correctly.

1
2
3
4
5
6
std::ifstream in(take_code.c_str());

if (!in)
{
    std::cout << "ERROR: Cannot find file!\n"
}


Using the forum compiler (cpp.sh you mean?) won't give any good result here, since there wouldn't be a file you can create to open.
Last edited on
Topic archived. No new replies allowed.