c++11 - c++ pass string as refrence in header and cpp -
how pass string reference in constructor in header , cpp using cin line in text file. execute command prompt: program < test.txt
text.txt
aaa bbb ccc
main.cpp
include "read.h" include <iostream> include <string> using namespace std; int main() { string output; read read(line); while (getline(cin, line)) { read.run(); ... ... }
read.cpp
include "read.h" include <iostream> using namespace std; read::read(string& input) : currentline(input) { } void read::run() { cout << "currentline:" << currentline << "\r\n"; }
read.h
class read{ public: std::string currentline; parser(std::string& s); void advance(); }
when execute program in cmd: program < test.txt currentline not have value
string output; read read(line); while (getline(cin, line)) { read.run(); ... ... }
you set read.currentline
input
(in constructor) currentline
isn't reference, copied, resulting in empty string. make currentline
reference too. beware of dangling references though.
Comments
Post a Comment