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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -