-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

2
  • Get yourself a good C++ book Commented Feb 28, 2012 at 20:11
  • Unrelated: The full error messages for Visual Studio are in the "output" window, not the "error" window. Commented Feb 28, 2012 at 20:11

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

gah, I just remembered on the easier solution. How I can check if user written input int type?
@mwingdahl: I don't think we should recommend sprintf for conversion in C++, use std::stringstream or boost::lexical_cast
@Cehppel: That is a completely different question, and requires it's own page.
@MooingDuck: Good point! My C roots are showing, apparently! :) Edited.
ok, I will create new topic, if my try fails. thanks for all advices
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.