c++ - 这段C++代码有什么问题?

标签 c++ visual-c++

#include <iostream>
#include <math.h>
#include "stdafx.h"

using namespace std;

int main()
{
    float a, b;
    cout << "Enter The Number: ";
    cin >> a;
    b = sqrt(a);
    cout << "The Square Root of The Number Is: " << b;
    return 0;
}

编译器给出错误:

consoleapplication1.cpp(10): error C2065: 'cout': undeclared identifier  
consoleapplication1.cpp(11): error C2065: 'cin': undeclared identifier
consoleapplication1.cpp(12): error C3861: 'sqrt': identifier not found
consoleapplication1.cpp(13): error C2065: 'cout': undeclared identifier

请告诉我错误以及为什么我包含“stdafx.h”以及为什么它在引号中?使用 Visual Studio 2015。级别:初学者

最佳答案

只需将 #include "stdafx.h" 移动到文件顶部,您的代码就会编译。

stdafx.h 包含预编译头,如果你想删除它,你必须在项目属性中禁用它。

更好的选择是在 visual studio 中创建新项目时禁用它

文件 -> 新建项目 -> 选择你的设置并输入 OK -> 下一步 -> 这里取消选中“预编译头文件” -> 完成

关于c++ - 这段C++代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38944371/

相关文章:

c++ - 嵌套类作为C++中成员函数的参数

c++ - 严重错误C1083

c++ - find() 相等运算符重载

C++ 备注 : operands are evaluated in unspecified order

c++ - 如何在 C++ 中比较两个映射迭代器?

c++ - 即使为其相关的 dll 正确生成了 lib 文件,CMake 生成的 MSVC 项目也找不到符号

c++ - 如何正确使用 boost::make_shared_ptr?

visual-c++ - 从数组列表c++写入文本文件

c++ - CMFCPropertyGridProperty 数字输入

c++ - 使用相同的函数对 vector<X> 和 set<X> 中的自定义比较器进行排序