Using #include <string> in C++

0 votes
asked Nov 24, 2017 by Jonjiissmiling c (160 points)
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string movieTitle;
    movieTitle = "Pizza Time";
    cout << "Hello World" << endl;
    cout << "My favorite movie is " << movieTitle << endl;

    return 0;
}

    Displayed Output:

/home/a.out: relocation error: /home/a.out: symbol _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE6assignEPKc, versi
on GLIBCXX_3.4.21 not defined in file libstdc++.so.6 with link time reference

As far as I know the code should work. I don't know the error message involving the program not compiling and running at all???

3 Answers

0 votes
answered Nov 24, 2017 by bader

Hello World

My favorite movie is Pizza Time

0 votes
answered Nov 24, 2017 by anonymous
AS per the error message it shows that you just selected C++ programming headers and libraries, you did not upgrade your glibc libraries. Where it is expecting glibc 3.4 versions might be you are having a lower version of glibc.
commented Nov 24, 2017 by anonymous
How is one supposed to upgrade a glibc library? Is that different than std?
commented Nov 25, 2017 by anonymous
You can download them...Or just upgrade your compiler and IDE.  Perhaps a Visual C++ upgrade for instance.
0 votes
answered Nov 25, 2017 by anonymous
You're right, it should work. I don't know why it doesn't work here but when I copy/paste your code and compile it with g++ myself, then it works.

Anyway, I was able to get your code to work by merging lines 7 and 8:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string movieTitle = "Pizza Time";
    cout << "Hello World" << endl;
    cout << "My favorite movie is " << movieTitle << endl;

    return 0;
}

That seems to work for me.
commented Nov 26, 2017 by Jonjiissmiling c (160 points)
I tried what you wrote and it worked. I emailed them about it, and I think they are working on fixing it. Thanks.
commented Feb 4, 2018 by Shahzab Khan
You can also use this....
#include <iostream>
#include <string>

using namespace std;

int main()
{
        string movieTitle;
        movieTitle= "Pizza time";
        cout<<"HelloWorld\n"<<"My favorite movie is "<<movieTitle<< endl;
        
        return 0;
    
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...