尽管语法正确,但代码无法运行

时间:2016-10-02 18:34:47

标签: c++

所以我试图在Visual Studio中运行它,但它在输出控制台中给了我几个错误:

1>------ Build started: Project: ConsoleApplication1, Configuration: Release     x64 ------
1>  stdafx.cpp
1>  ConsoleApplication1.cpp
1>ConsoleApplication1.cpp(6): error C2039: 'cout': is not a member of 'std'
1>  predefined C++ types (compiler internal)(209): note: see declaration of 'std'
1>ConsoleApplication1.cpp(6): error C2065: 'cout': undeclared identifier
1>ConsoleApplication1.cpp(6): warning C4554: '<<': check operator precedence for possible error; use parentheses to clarify precedence
1>ConsoleApplication1.cpp(6): error C2039: 'endl': is not a member of 'std'
1>  predefined C++ types (compiler internal)(209): note: see declaration of 'std'
1>ConsoleApplication1.cpp(6): error C2065: 'endl': undeclared identifier

我的代码如下:

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

void test(int x, int y)
{
    std::cout << x + y << std::endl;

}
int main()
{
    test(1, 2);

    return 0;


}

2 个答案:

答案 0 :(得分:1)

您没有阅读您正在使用的程序的文档。

#include "stdafx.h"必须首先

答案 1 :(得分:0)

更改头文件包含的顺序。预编译的头文件包含必须始终是源文件中的第一个非注释。