to_string error

I am writing a code and I am getting an error of to_string was not declared in this scope. Does anyone have any idea how to fix this?

string my_to_string(){

string str="";

str=str+book_id+","+author+","+title+","+to_string(edition)+","+to_string(year)+","+isbn+"\n";

return str;
You need to compile in C++11 mode. If you are using g++ then you can say g++ -std=c++11 ...
Where would I put that in my code? I am not sure what C++11 is.
It doesn't go anywhere in your code.
It's a compiler option.

Are you on windows, mac, or linux?
Are you compiling through an IDE or from the terminal (command line)?
What IDE?
What compiler?
I am on Geany C++ on a Linux. I am supposed to run it through the terminal to have everything show up in a text file. On Geany I am supposed to hit compile, build, and execute and I am supposed to get an input and output file. I am not sure how to run the g++ -std=c++11 on the terminal because when I do that it says fatal error
What version goes g++ say it is? (g++ --version)

Also, try this:
https://stackoverflow.com/a/14555674/8690169
As what is pointed out in the comments, you need to add the flag -std=c++0x. You can set it in the "Build" -> "Set build commands", then modify the commands in following boxs:

Compile:
g++ -Wall -std=c++0x -c "%f"

Build:
g++ -Wall -std=c++0x -o "%e" "%f"
Last edited on
I am not sure if this is right but It says in the About Geany tab, it is Geany 1.27. Once I figure out what version Geany is, by adding the Set build command tabs that should help it work?
If this gets run on a computer other than my own, will it work without changing the command settings?
by adding the Set build command tabs that should help it work?

I'd assume so. Sounds like Geany 1.27 is from 2016, so that should be recent enough to allow most C++11 features.
https://wiki.geany.org/howtos/configurebuildmenu

You want to be able to run it on your own computer. If this is for an assignment, communicate with your instructor or teaching assistant about your problems.

If you still can't get it to work, write your own to_string():

<EDITED OUT INCORRECT CODE: SEE BELOW>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

#include <sstream>
template <typename T>
std::string to_string(const T& value)
{
    ...
}

int main()
{
    std::cout << to_string(314159) << std::endl;
}
Last edited on
If I were to write my own string? Would I follow the example from above? If so, what are the numbers in the to_string above?
Sorry, my previous post is wrong.

Try this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

#include <sstream>
template <typename T>
std::string to_string(const T& value)
{
    std::stringstream ss;
    ss << value;
    return ss.str();
}

int main()
{
    std::cout << to_string(314159) << std::endl;
}


You should be able to use it just like you would the actual version.
So to my code, I added this to the beginning

#include <fstream>

#include <string>

#include <iostream>

#include <vector> //learned in high school

#include <algorithm> //learned in high school

#include <sstream>
template <typename T>
std::string to_string(const T& value)
{
std:stringstream ss;
ss << value;
return ss.str();
}
using namespace std;

and this to my int main

int main()
{
std::cout << to_string(314159) << std::endl;
}

Is this correct?
You have a typo.
std:stringstream ss; --> std::stringstream ss;
Scope resolution is 2x colons (::).
Thank you! Is there any way not to cout the numbers. Also, my program is supposed to input data to a file and output a file. How would I find the file?
My line was just an example of to_string, what you had in your first post should now work. You can just delete the line I have in main, and add in your own stuff.

For File I/O check out
http://www.learncpp.com/cpp-tutorial/186-basic-file-io/
http://www.cplusplus.com/doc/tutorial/files/
What would cause a file output from the code but not have an input file? This is my code for the in and out file.

Input code:
{
vector<Book> books;

ifstream file("bookinput.dat", ios::in);

string str;


Output code:

ofstream myfile;

myfile.open ("bookoutput.dat",ios::out);


for(int i=0; books.size();i++)

myfile << books[i].my_to_string();

myfile.close();

When I go look in my files, there is no input file and I am not sure why
I'm not sure what you mean. You have to create the input file if you want to be able to read it. Make sure the file is in the same place that the program is run from.

It's always a good habit to check if a file was correctly opened:
1
2
3
4
5
6
7
8
9
10
11
12
13
ifstream file("bookinput.dat");
if (!file)
{
    cout << "error opening input file" << endl;
}

// ...

ofstream myfile("bookoutput.dat");
if (!myfile)
{
    cout << "error opening output file" << endl;
}
Last edited on
Topic archived. No new replies allowed.