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

时间:2016-08-14 16:47:18

标签: 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.级别:初学者

2 个答案:

答案 0 :(得分:3)

文件顶部的简单移动#include "stdafx.h",您的代码将被编译。

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

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

档案 - &gt;新项目 - &gt;选择您的设置,然后输入确定 - &gt;下一个 - &gt;取消选中&#34;预编译标题&#34; - &GT;完成

答案 1 :(得分:1)

编译器将忽略#include "stdafx.h"行之前的任何内容(使用预编译头文件时)。

我建议您实际使用预编译的标头,因此请将标准库标头include移至stdafx.h文件。

相关问题