与操作员不匹配>>问题

No match for operator>> issue

本文关键字:gt 问题 操作员 不匹配      更新时间:2023-10-16

我在unix终端中运行这个程序,但是当我试图编译它时,它给出了一个巨大的问题列表,但是我认为问题是部分说不匹配操作符>>。我意识到这个程序缺失了很多,它还没有接近完整,我希望能够在我走得更远之前编译它。我不知道是什么原因导致这个错误,希望能得到帮助。

#include <iostream>
#include <vector>  
#include <string>
using namespace std;
int main()
{
    int ui = 0 ;
    vector<string> in;
    string temp = "0";
    int vsize = 0;
    while(ui != 5)
    {
            cout << "1.     Read" << endl;
            cout << "2.     Print" << endl;
            cout << "3.     Sort" << endl;
            cout << "4.     Search" << endl;
            cout << "5.     Quit" << endl;
            std::cin >> ui >> std::endl;
            if(ui = 1)
            {
                    while(temp  != "q")
                    {
                            std::cout << "Enter the next element (Enter 'q' to stop):" << std::endl;
                            std::cin >> temp >>  std::endl;
                            in.pushback(temp);
                            vsize++;
                    }
            }
            if(ui = 2)
            {
                    std::cout << "Sequence: ";
                    for (int i = 0; i < vsize; i++)
                    {
                            cout << in[i];
                    }
                    std::cout << std::endl;
            }
            if(ui = 3)
            {
            }
    }
    return 0;

}

你知道你在if语句中做赋值吗?等式在c++中写为==。还有,为什么是vsize?向量有它自己的方法来获取大小,in.size()会给你。

我希望能够在我走得更远之前编译它…好了!

但是你应该读取的错误和警告消息,它们通常有助于理解问题和解决问题的方法(以下使用CLang输出):

ess.cpp:21:31: error: reference to overloaded function could not be resolved;
  did you mean to call it?
        std::cin >> ui >> std::endl;

你试图提取的东西到std::endl是没有意义的。只写std::cin >> ui;

ess.cpp:23:19: warning: using the result of an assignment as a condition without
  parentheses [-Wparentheses]
        if(ui = 1)

ui = 1是赋值。相等性检验应为if (ui == 1)

ess.cpp:29:32: error: no member named 'pushback' in
  'std::__1::vector<std::__1::basic_string<char,
  std::__1::char_traits<char>, std::__1::allocator<char> >,
  std::__1::allocator<std::__1::basic_string<char,
  std::__1::char_traits<char>, std::__1::allocator<char> > > >'; did you
  mean 'push_back'?
                        in.pushback(temp);

…我也认为你指的是in.push_back(temp);

我只拿了一个例子,每个错误,你应该能够修复重复:-)