如何将#include <iostream>添加到项目中?

时间:2017-07-13 01:20:44

标签: c++ visual-studio iostream

我刚开始学习C ++,我试图用Visual Studio 2017创建一个简单的代码。

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
int x;
std::cout >> "Enter Age";
std::cin >> x;

    if (x >= 18) {
        std::cout << "You are an adult." << endl;
    }
    else {
        std::cout << "You are a child." << endl;
    }

    return 0;
}

但我认为我收到很多错误,例如 iostream不存在,因为我没有在头文件或iostream.h中看到它 我尝试创建一个新的头文件,只需输入#include <iostream>但这似乎不起作用。这是我的错误。

 1>c:\users\munta\onedrive\documents\visual studio 
 2017\projects\consoleapplication3\consoleapplication3\source.cpp(8): error 
 C2784: 'std::basic_istream<char,_Traits> &std::operator >>
 (std::basic_istream<char,_Traits> &,signed char &)': could not deduce 
 template argument for 'std::basic_istream<char,_Traits> &' from 
 'std::ostream'
 1>c:\program files (x86)\microsoft visual 
 studio\2017\community\vc\tools\msvc\14.10.25017\include\istream(1068): 
 note: see declaration of 'std::operator >>'
 1>c:\users\munta\onedrive\documents\visual studio 
 2017\projects\consoleapplication3\consoleapplication3\source.cpp(8): error 
 C2784: 'std::basic_istream<char,_Traits> &std::operator >>
 (std::basic_istream<char,_Traits> &,signed char *)': could not deduce 
 template argument for 'std::basic_istream<char,_Traits> &' from 
 'std::ostream'
 1>c:\program files (x86)\microsoft visual 
 studio\2017\community\vc\tools\msvc\14.10.25017\include\istream(1061): 
 note: see declaration of 'std::operator >>'
 1>c:\users\munta\onedrive\documents\visual studio 
 2017\projects\consoleapplication3\consoleapplication3\source.cpp(8): error 
 C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>
 (std::basic_istream<_Elem,_Traits> &,_Elem &)': could not deduce template 
 argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ostream'
 1>c:\program files (x86)\microsoft visual 
 studio\2017\community\vc\tools\msvc\14.10.25017\include\istream(1036): 
 note: see declaration of 'std::operator >>'
 1>c:\users\munta\onedrive\documents\visual studio 
 2017\projects\consoleapplication3\consoleapplication3\source.cpp(8): error 
 C2784: 'std::basic_istream<_Elem,_Traits> &std::operator >>
 (std::basic_istream<_Elem,_Traits> &,_Elem *)': could not deduce template 
 argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ostream'
 1>c:\program files (x86)\microsoft visual 
 studio\2017\community\vc\tools\msvc\14.10.25017\include\istream(995): note: 
 see declaration of 'std::operator >>'
 1>c:\users\munta\onedrive\documents\visual studio 
 2017\projects\consoleapplication3\consoleapplication3\source.cpp(8): error 
 C2676: binary '>>': 'std::ostream' does not define this operator or a 
 conversion to a type acceptable to the predefined operator
 1>Done building project "ConsoleApplication3.vcxproj" -- FAILED.

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

选中此项:std::cout >> "Enter Age";。我想它应该是std::cout << "Enter Age";

BTW,与问题无关,因为您正在撰写std::coutstd::cin,那么如果您与std::endl而不是endl保持一致,那会更好。

答案 1 :(得分:0)

问题在第8行。它应该是:

std::cout << "Enter Age";