0

I am a beginner in c++, so the answer maybe obvious.

I have two functions - one is able to convert int into string and the other is able convert double into string. It would be more elegant however to unite these two into one. I have tried it with the code below, but it yields an error: ‘number’ was not declared in this scope

string number_into_string (void* data, int psize)
{
    if (psize == sizeof(int))
    {
        int* number; number =(int*)data;
    }
    if (psize == sizeof(double))
    {
        double* number; number = (double*)data;
    }
    ostringstream convert;
    convert << number;
    string str = convert.str();
    return str;
}

int main()
{
    double x =1000;
    int y = 5;
    string str_x = number_into_string(&x, sizeof(x));
    string str_y = number_into_string(&y, sizeof(y));
}
tuckermi
  • 839
  • 6
  • 17
Gleb
  • 53
  • 1
  • 6

4 Answers4

9

Since number is declared inside the if ... { brackets, it only exists in that space. Further, you can't have one variable have two different types in C or C++.

(If you have a C++11 compiler, you don't need to do this at all, just use std::to_string which exists for most standard types).

The solution is to not use pointers, and to use a template function:

template<typename T> string number_into_string(T number)
{
    ostringstream convert;
    convert << number; 
    string str = convert.str();
    return str;
}
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
3

Simple, use the std::to_string function.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

A pair of curly braces {} create a nested scope and objects created inside that scope are not visible in the outer scope. You could rearrange your code to something like this:

string number_into_string (void* data, int psize)
{
    ostringstream convert;
    if (psize == sizeof(int))
    {
        convert << *((int*) data);
    }
    if (psize == sizeof(double))
    {
        convert << *((double*) data);
    }
    return convert.str();
}

Personally, I would use a standard library function instead of (badly) reinventing the wheel.

Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
  • Reinventing the wheel here is particularly messy when you consider that the standard makes no promises that `sizeof(int) != sizeof(double)`. In fact, on a system with 64-bit `int`s, they quite likely *will* be the same size. At which point things get interesting... – cHao Jul 31 '13 at 14:15
0

Apart from being quite messy, the main reason for error is variable scope. Every variable has its scope of visibility, and it is not visible beyond that scope.

In function number_into_string you have 3 scopes:

  1. function scope - this is the whole function number_into_string
  2. first if block
  3. second if block

Variables from function scope are visible in both if blocks, but not the other way around.

That means, in your code:

convert << number;

you're using variable which is not known (visible) in current scope.

The other thing is that you just CAN'T do this "generic values thing" in C++ without using templates or putting two copies of that "conversion code" into each if block, which woud invalidate the point of having single "conversion function".

Also, you CAN'T distinguish reliably between double and int by using sizeof.

Jan Spurny
  • 5,219
  • 1
  • 33
  • 47
  • You could pass `typeid(x)` by reference...which would give you a way to distinguish between `int` and `double`. But it still seems a rather iffy idea, unless you absolutely have to have one function for whatever reason. – cHao Jul 31 '13 at 14:40