string was not declared in this scope

Hi guys,

I seem to be having some problems with std::to_string function in the string class

|20|error: 'to_string' was not declared in this scope|

I thought maybe it was because my compiler was not set to use C++11 it uses 98 by default,so I changed it to 11 and cleaned then re built the project yet I still get the same error



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

using namespace std;

int factorial(int x){


   if( x <= 1){

      return 1;
   }
   return factorial(x-1) * x;
}

int fact(int x){


   int fac = factorial(x);
   string facString = to_string(x);

}
what you have looks correct, try a deeper rebuild or touch all the files.
What compiler are you using? A what is the exact version of that compiler?

yeah it's very strange that it's not working

my compiler version is mingw 32 bit - gcc (GCC) 7.1.0 (for windows)
Last edited on
How are you telling the compiler to use the C++11 (C++17 suggested) or higher standard?
adam2016 (881)
yeah it's very strange that it's not working

my compiler version is mingw 32 bit - gcc (GCC) 7.1.0 (for windows)

What is the name of IDE that you are using?
How are you telling the compiler to use the C++11 (C++17 suggested) or higher standard?


I just ticked the option in the compiler options/settings in codeblocks,I also tried adding it manually by specifying c++11 as the version in other compiler options but no luck

What is the name of IDE that you are using?


codeblocks

Did anyone notice given the function:

1
2
3
4
5
int fact(int x)
{
   int fac = factorial(x);
   string facString = to_string(x);
}

It returns an int, but where is the function returning anything?

If you are to use "to_string" would not "fac" be a better choice than "x"?

And since there is nothing done with "facString" before the function ends it is lost when the function ends. So, what is the point of using "to_string" here?

Just some thoughts. Correct me if I am wrong.

Andy
Andy, you are 100% correct, but even though the code is effectively nonsense, it should still compile.
it should be called on fac and it should return the value as a string not an int, or it should return fac and not call tostring at all, unclear. But even with all those things, it still should have compiled.
@jonnin,

The only error I received at compile time is that the function did not return a value. When I fixed that it ran and worked fine. I am using VS 2017 with at least C++11 standard if not C++14.

Andy
adam, two things...

1. I don't believe that you are using 7.1.0, because 7.1.0 defaults to C++14.

If you're using 7.1.0, this should compile without having to explicitly say -std=c++11 or beyond.
1
2
3
4
5
6
7
#include <fstream>
#include <string>

int main()
{
    std::ifstream f(std::string("test"));
}



However... maybe I'm wrong. Can you compile your program by command line and add -v
e.g.
g++ -v main.cpp -o main.exe
You can also add -v as an "additional command-line argument" within Code::Blocks.

Tell us what version the output is.
For example, mine says.

C:\code\cplusplus243635>g++ -v main.cpp -o main.exe
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/x86_64-w64-mingw32/7.1.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../src/configure --enable-languages=c,c++ --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --disable-multilib --prefix=/c/temp/gcc/dest --with-sysroot=/c/temp/gcc/dest --disable-libstdcxx-pch --disable-libstdcxx-verbose --disable-nls --disable-shared --disable-win32-registry --with-tune=haswell
Thread model: win32
gcc version 7.1.0 (GCC)
COLLECT_GCC_OPTIONS='-v' '-o' 'main.exe' '-mtune=haswell' '-march=x86-64'

GNU C++14 (GCC) version 7.1.0 (x86_64-w64-mingw32) [I'm using 64-bit]
        compiled by GNU C version 7.1.0, GMP version 6.1.2, MPFR version 3.1.5-p2, MPC version 1.0.3, isl version isl-0.18-GMP



2. Earlier versions of MinGW had awful support for C++11 string-related functionality, but 7.1.0 should be fine.

EDIT: Since you said you're using 32-bit.... maybe 32-bit MinGW still has problems and isn't correctly implementing what GCC says. That's possible. In which case I don't have a solution other than not using 32-bit.
But exhaust your other options first.

When you go into compiler settings on CodeBlocks, are you sure that you're pointing the compiler to your 7.1.0 one? CodeBlocks comes with its own compiler, you want to make sure it's not using that. In fact, I would completely move the compiler directory that comes with codeblocks into the recycle bin. Then try to compile again.

3. If you're still having problems, bypass CodeBlocks completely, and just try to compile the following program via command-line:
1
2
3
4
5
6
#include <string>

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

g++ -v main.cpp -o main.exe

and let us know what happens.
Last edited on
I also tried adding it manually by specifying c++11 as the version in other compiler options but no luck

You mean "-std=c++11" from (right click on the open project) select build options, select "other compiler options tab" and added that statement into Window below the tabs, right?

Also when you "build" the project the version of g++ and all the selected options should appear in the build window. Something like:


1
2
3
4
5
6
7
8
9
10
11

Cleaned "homework - Debug"

-------------- Build: Debug in homework (compiler: GNU GCC Compiler)---------------

g++ -Wall -fexceptions -pedantic-errors -pedantic -Wextra -Wall -g -Wvla -std=c++17  -c/PNY/c++homework/main.cpp -o obj/Debug/main.o
g++  -o bin/Debug/homework obj/Debug/main.o   /opt/gcc-8.1.0/lib64/libstdc++fs.a
Output file is bin/Debug/homework with size 4.56 MB
Process terminated with status 0 (0 minute(s), 1 second(s))
0 error(s), 0 warning(s) (0 minute(s), 1 second(s))
 


As you can see I built the project with the compiler flags shown (-std=c++17) and I'm using gcc-8.1.0.

re-read what I said :)
most of the issues we mentioned are legal c++ code and should have compiled with at most warnings.

unless you changed something, you convert to string, but return int and don't use the string.
you to-string x, when it SEEMS like you wanted the factorial not the input. Hard to say, because you didnt use the string.
and you fixed not returning a value, but did you return a string or the int?

Topic archived. No new replies allowed.