【发布时间】:2010-10-31 19:42:43
【问题描述】:
我正在学习 C++,但遇到了一个我不明白的错误。
这是我的源代码,包括cmets(我正在学习的个人参考。)
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
float h; //a float stands for floating point variable and can hold a number that is a fraction. I.E. 8.5
double j; //a double can hold larger fractional numbers. I.E. 8.24525234
char f; // char stands for character and can hold only one character (converts to ASCII, behind scenes).
f = '$'; //char can hold any common symbol, numbers, uppercase, lowerver, and special characters.
h = "8.5";
j = "8.56";
cout << "J: " << j << endl;
cout << "H: " << h <<endl;
cout << "F: " << f << endl;
cin.get();
return 0;
}
我在编译时收到以下错误:
错误 C2440:“=”:无法从 'const char [4]' 到 'float' 没有可以进行这种转换的上下文
还有
错误 C2440:“=”:无法从 'const char [5]' 到 'double' 没有可以进行这种转换的上下文
你们能指出我正确的方向吗? 我刚刚了解了 const(可能是 20 分钟前),我不明白为什么以前的程序不能正常工作。
【问题讨论】:
-
双引号内的文字是字符串文字,与数值不同。
-
这个问题是如此简单的语言问题,任何介绍性教程都会涵盖。不要认为它属于这里。
标签: c++ compiler-construction floating-point