没有运算符">>"与这些操作数匹配

No operator ">>" matches these operands

本文关键字:gt 操作数 运算符      更新时间:2023-10-16

我对这个问题做了一些挖掘,发现其他人与我有相似但不相同的错误。我的两个主要理论是,我错过了一些明显的东西,或者我破坏了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;
}

>><<的每个实例都带有红色下划线,并带有错误消息:

  • 没有运算符">>"与这些操作数匹配
  • 标识符"setw"未定义

我很确定我做了一些明显和错误的事情,但我一辈子都找不到它。

你得到了>><<颠倒了。 <<是为了std::cout>>是为了std::cin。你正在做相反的事情。您还需要包含iomanip 用于std::setw .

<<是一个

流插入运算符,它与coutostream对象一起使用。 >> 是一个流提取运算符,它与对象cin一起使用并istream。在您的程序中,您已经清楚地交换了它们的位置。修复它,然后一切都会顺利进行。

此外,您已经编写了语句,using namespace std,则无需再次使用指定命名空间。我的意思是要么将std::cout(和所有其他类似的行(更改为cout,要么只是删除行using namespace std;.但是,后者是更好的选择。