【问题标题】:no operator matches these operands c++没有运算符匹配这些操作数 c++
【发布时间】:2018-05-02 06:18:52
【问题描述】:
my code:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    char str1[1000000];
    char newString[1000][1000]; 
    int i,j,ctr;
       cout <<" \n\n Split string by space into words :"<< endl;
       cout << "---------------------------------------\n";    

    cout << " Input  a string : ";
    cin >> str1 >> sizeof(str1) >> stdin;   

    j=0; ctr=0;
    for(i=0;i<=(strlen(str1));i++)
    {
        // if space or NULL found, assign NULL into newString[ctr]
        if(str1[i]==' '||str1[i]=='\0')
        {
            newString[ctr][j]='\0';
            ctr++;  //for next word
            j=0;    //for next word, init index to 0
        }
        else
        {
            newString[ctr][j]=str1[i];
            j++;
        }
    }
    cout << "\n Strings or words after split by space are :\n";
    for(i=0;i < ctr;i++)
        cout << newString[i];

    return 0;
}

错误说明:

错误 1 ​​错误 C2679: 二进制 '>>' : 找不到运算符 'unsigned int' 类型的右手操作数(或者没有可接受的 转换)c:\users\ayah atiyeh\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\source.cpp 14 3 IntelliSense:没有运算符“>>”匹配这些操作数 操作数类型为:std::basic_istream> >> unsigned int c:\Users\Ayah Atiyeh\Documents\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\Source.cpp 14

【问题讨论】:

  • 你为什么不直接使用std::string?顺便说一句 - 你认为cin &gt;&gt; str1 &gt;&gt; sizeof(str1) &gt;&gt; stdin; 是什么意思?
  • 请问我应该在哪里使用std::string?
  • &gt;&gt; sizeof(str1) 什么?
  • 对字符串使用 std::string。对数组使用 std::vector。

标签: c++ cin


【解决方案1】:

你需要这样做:

cout << " Input  a string : ";
cin >> str1;

而不是:

cout << " Input  a string : ";
cin >> str1 >> sizeof(str1) >> stdin;   

问题是,>> 运算符用于将输入指向它右侧的变量。 而且您没有在第二个>>右侧给出变量, sizeof(str1) 是一个函数,它返回一个数字。当编译器看到一个数字代替一个变量时,它会给你这个错误。

【讨论】:

    【解决方案2】:

    改变

    char str1[1000000];
    

    std::string str1;
    

    还有

    cin >> str1 >> sizeof(str1) >> stdin; 
    

    std::cin >> str1;
    

    顺便说一句 - 删除行 using namespace std;

    然后一行

     for(i=0;i<=(strlen(str1));i++)
    

    应该是

      for(i=0;i<str1.length();i++)
    

    【讨论】:

    • 或自 C++11 for(char c : str1) :)
    • 错误 1 ​​错误 C2679: 二进制 '>>' : 未找到采用 'unsigned int' 类型右手操作数的运算符(或没有可接受的转换) c:\users\ayah atiyeh \documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\source.cpp 13
    • 3 IntelliSense: no operator ">>" 匹配这些操作数操作数类型是: std::basic_istream> >> unsigned int c:\Users\Ayah Atiyeh \Documents\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\Source.cpp 13
    • std::cin >> str1 >> sizeof(str1) >> stdin;在这一行
    • @A.Atiyah 这是您被告知要替换的行之一 - 为什么您仍然在代码中保留它?
    【解决方案3】:
    cin >> str1 >> sizeof(str1) >> stdin;
    

    在此输入语句的第二部分,您尝试读入一个 rvaluesizeof(str1) 是编译时间常数并且是一个右值)。你不应该那样做。此外,向stdin 读取内容是一种危险操作,因为它的类型是FILE*,这可能会对进一步的输入操作产生负面影响。

    【讨论】:

    • 怎么改?
    猜你喜欢
    • 1970-01-01
    • 2022-01-10
    • 2013-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多