-1

c++ problem different basics types during converting variables Yes, stupid problem, but I'm newbie in c++ and IDK what's the problem.

I have this code:

#include <iostream>
using namespace std;
int main()
{
float a = 54;
cout << a;
double(a);
cout << a;
return 0;
}

and this errors:

  • error C2371: 'a' : redefinition; different basic types line 7
  • error C2088: '<<' : illegal for class line 8

why it wrote: different basics types, when I converting only float to double? and what does it mean second error? what class line?

and I have this question too: Can I convert 2 variables with different basic types f.e. int to string? and is it same as converting f.e. double to float or different?

Here is print screen during debugging project in VC++ 2010

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
Cehppel
  • 3
  • 4

1 Answers1

0

You've already declared 'a' as a float, and the compiler thinks you're trying to redeclare it as a double. Try this:

#include <iostream>
using namespace std;
int main()
{
    float a = 54;
    cout << a;
    double b(a);
    cout << b;
    return 0;
}

As far as your second question goes:

Can I convert 2 variables with different basic types f.e. int to string? and is it same as converting f.e. double to float or different?

The answer is that you cannot convert them implicitly or even directly (and have them preserve their meaning, that is), but you can certainly use facilities such as std::stringstream, etc. to do the conversion for you. Conversion from float to double, on the other hand, is an implicit conversion that the compiler will do for you.

mwigdahl
  • 16,268
  • 7
  • 50
  • 64