没有运算符与这些操作数 C++ 匹配

no operator matches these operands c++

本文关键字:操作数 C++ 匹配 运算符      更新时间:2023-10-16
my code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
    char str1[1000000];
    char newString[1000][1000]; 
    int i,j,ctr;
       cout <<" nn 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]=='')
        {
            newString[ctr][j]='';
            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:二进制">>":未找到需要 类型为"无符号 int"的右操作数(或者没有可接受的操作数 转换( c:\users\ayah atiyeh\documents\Visual Studio 2012\项目\控制台应用程序1\控制台应用程序1\源.cpp 14 3 智能感知:没有运算符">>"与这些操作数匹配 操作数类型为:std::basic_istream>>> unsigned int c:\Users\Ayah Atiyeh\Documents\Visual Studio 2012\项目\控制台应用程序1\控制台应用程序1\源.cpp 14

你需要这样做:

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

而不是:

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

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

更改

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++)
cin >> str1 >> sizeof(str1) >> stdin;

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