lundi 17 avril 2017

Why below C++ code gives error as mentioned?

Why does the following code give

adf asdf terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid Aborted (core dumped) output.

It is aborting after printing everything.

Based on other stackoverflow answers on similar questions, it seems I should be calling std::string constructor with NULL value, but I am unable to understand where can I be doing that in the below code.

using namespace std;

    string gen_assembly(string ir){
      std::stringstream ss;
      ss.str(ir);
      std::string to;

        while(getline(ss, to)){
            cout<<to<<endl;
        }
        return 0;
    }


    int main(){
        string ir = "adf\nasdf";
        gen_assembly(ir);
        return 0;
    }

Aucun commentaire:

Enregistrer un commentaire