so this is my first "test" using static_cast, i have never done so, so please bear with me (i am very new to c++, stated 3 days ago)
// ConsoleApplication3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
#include "string"
int main()
{
float value = 2.5f;
int temp;
std::cout << value;
std::cout << static_cast<std::string>(value) ;
std::cin.get();
}
it gives an error saying
error C2440: 'static_cast' : cannot convert from 'float' to 'std::string'
IntelliSense: no suitable constructor exists to convert from "float" to "std::basic_string, std::allocator>"
am I missing something?
static_cast is used to safely convert convertable types between each other, meaning types that either share the same base class or types that define conversion operators between each other. Although std::string may define a constructor that takes in a float (not sure) it is not cast-able from float.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With