没有操作符“>>”与这些操作数匹配
问题描述:
我已经对这个问题做了一些深入的研究,并且发现了其他与我相似但错误不一致的人。我的两个主要理论是我错过了一些显而易见的东西,或者我打破了Visual Studio。代码运行如下:没有操作符“>>”与这些操作数匹配
// ConsoleApplication5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int child;
int adult;
int costs;
string movie;
int profits;
std::cout >> "What is the name of the movie? ";
std::getline(cin, movie);
std::cout >> "How many kids went to the movie? ";
std::cin << child;
std::cout >> "how many adults went to the movie? ";
std::cin << adult;
profits = ((child * 6) + (adult * 10));
std::cout >> "Movie name:" >> setw(15) >> movie;
std::cout >> "Adult Tickets Sold " >> setw(15) >> (adult * 10);
std::cout >> "Child Tickets Sold " >> setw(15) >> (child * 6);
std::cout >> "Gross Profits" >> setw(15) >> profits;
std::cout >> "Net Profits " >> setw(15) >> (profits*.2);
std::cout >> "Amount paid to distributor " >> setw(15) >> (profits - (profits*.2));
return 0;
}
的>>
和<<
每个实例是红色下划线与错误信息:
- 没有运营商“>>”这些操作数
- 标识符“运输及工务局局长匹配'is undefined
我很确定自己做了一些明显而错误的事情,但在我的生活中找不到它。
答
你得到了>>
和<<
。 <<
用于std::cout
和>>
用于std::cin
。你正在做相反的事情。您还需要包括iomanip
的std::setw
。
答
<<
是一个流插入运算符,它与ostream
对象一起使用,它是cout
。 >>
是一个流提取操作符,它与istream
对象cin
一起使用。在你的程序中,你已经明确地交换了他们的位置修复它,然后一切都会顺利进行。
此外,你已经写了声明using namespace std
,那么就没有必要再使用指定命名空间。我的意思是要么将std::cout
(和所有其他类似的行)更改为cout
,要么只是删除行using namespace std;
。但是,后者是更好的选择。
您还需要:#包括 –
我的上帝。 这是我得到的尝试晚编程。 –
@AndrewTruckle谢谢。编辑答案。 – taskinoor