Windows 8上的Dev c++ 5.11出现错误答案
Dev c++ 5.11 on Windows 8 Produces wrong answer
#include<iostream>
#include<conio.h>
class Number
{
private:
int x, y;
public:
Number()
{
x = y = 100;
}
void avg()
{
std::cout<<"x = "<<std::cout<<x;
std::cout<<std::endl;
std::cout<<"Y = "<<std::cout<<y;
std::cout<<std::endl;
std::cout<<"Average = "<<std::cout<<(x+y)/2;
}
};
main()
{
Number n;
n.avg();
}
程序运行,但显示错误的答案,可能是显示内存位置的地址,而不是显示分配的值100。请纠正我为什么它是这样的行为?
std::cout << "x = " << std::cout << x;
是错误的。你需要
std::cout << "x = " << x;
否则,当调用operator<<
时,...<< std::cout
中的std::cout
流对象隐式转换为(void*)
,因此显示指针(地址)。
到void*
的转换由于历史原因(安全的bool习惯用法)而存在,但在c++ 11中由于引入了显式转换操作符而被删除,因此您的代码不应该在c++ 11中编译。
相关文章:
- 如何在文件和行号中抛出错误
- cmath抛出错误C2062、C2059、C2143和C2447.cmath包含在矢量文件中
- 如何通过参数抛出错误消息
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 将错误返回给调用方而不是立即在 C++ 中抛出错误是否是一种好的做法
- 有没有办法让编译器在我放置字符串而不是 nlohmann::json 对象时抛出错误?
- 为什么 __gcd() 在 macOS mojave 中抛出错误?
- 在函数中使用 const int size 参数创建数组会在 Visual Studio 中抛出错误 C++:表达式的计
- 使用 memcpy() 复制到 std::chrono::milliseconds 会给出错误 -Werror=clas
- 在编译时,C++项目抛出错误 C2228,这是预期的,因为控件在运行时未达到该点
- 为什么int myVar{5}给出错误并且不起作用?
- 阶乘问题在 c++ 中给出错误的输出
- C++如果采用类类型的函数被传递派生类型,有没有办法给出错误?
- 大数的阶乘给出错误的输出
- Visual Studio中的wxwidgets应用程序给出错误"LNK2019 unresolved external symbol"
- 如何用 C 编译,但如果使用任何C++,则会抛出错误?
- Freeopcua C++ 客户端和 Python Opcua 的组合会在 getChild() 上抛出错误
- C++11 std::thread给出错误:没有匹配的函数来调用std::hread::thread
- C++Mac OS X 10.11.2 vm_read给出错误的值
- #include <iostream> 使用 -std=c++11 给出错误