二进制表达式的操作数无效

Invalid operands to binary expression

本文关键字:无效 操作数 表达式 二进制      更新时间:2023-10-16

我收到以下错误:

二进制表达式的无效操作数("basic_ostream<char,std::_1::char_traits<char>>"answers"value_type"(又名"qElem"))发生在:

cout << "Your first task is to: " << tasks.front() << endl;

代码建议我在&tasks.front()处放置&,但我不想接收0xfdlkajd的值,我希望第一个值存储在我的向量中。如有任何帮助,我将不胜感激。

我代码:

#ifndef Queue_queue_h
#define Queue_queue_h
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct qElem { //qElem Struct
    string s;
    string p;
    qElem(string task, string priority) : s(task), p(priority) {}
};

//Establishing my Template and PriQueue Class
template <class T> //Template
class PriQueue
{
public:
    vector<qElem> tasks;
    //PriQueue();
    void enqueue(T str, int pri); //Adds to queue
    void dequeue(); //Deletes from queue
    void peek(); //Prints the first value in queue
    void size(); //Prints how many in queue
    void sort(vector<qElem*> &tasks); //Sort according to priority
private:
    int count = 0;
};
template <class T1>
void PriQueue<T1>::enqueue(T1 str, int pri) //Adding an element to the queue
{
    tasks.push_back(qElem(str, pri));
    sort(tasks); //NEW ERROR IS HERE
    count++;
}
template <class T1>
void PriQueue<T1>::dequeue() //Removing an element from the front of the queue
{
    //tasks.erase(tasks.begin());
    tasks.erase(tasks.begin());
    if (tasks.empty()) {
        cout << "You have no tasks!" << endl;
}
    else {

    }
    count--;
}
template <class T1>
void PriQueue<T1>::peek() //Returning a value at front of the queue (NOT removing it)
{
    if (tasks.empty()) {
        cout << "You have no tasks!" << endl;
}
else {
        cout << "Your first task is to: " << tasks.front().s << endl;
}
//Testing Purposes only
/*
 cout << "Your tasks are:";
 for (typename vector<T1>::iterator i = tasks.begin() ; i != tasks.end(); ++i)
 cout << " " << *i << ",";
 cout << endl;
 */
}
template <class T1>
void PriQueue<T1>::size() //Returning the number of items in the queue.
{
    cout << "You have " << count << " tasks in queue." << endl;
}
template <class T>
void PriQueue<T>::sort(vector<qElem*> &tasks) {
bool sortUp = true;
for(int i = 0; i < tasks.size();i++)
    for(int j = i+1; j < tasks.size(); j++)
    {
        if(sortUp)
        {
            if(tasks[i] > tasks[j])
                swap(tasks[i],tasks[j]);
        }
        else if(tasks[i] < tasks[j]) //else sortDown
            swap(tasks[i],tasks[j]);
    }
}

#endif

编译器不知道如何输出qElem。如果只想打印任务,请使用cout << "..." << tasks.front().s << endl;。编译器知道如何输出std::string。(您也可以为qElem实现自己的operator <<重载,但在这种情况下,这可能是过度的。)

注意你的代码有很多问题。当您的qElem只存储std::string s时,为什么要使用模板PriQueue ?为什么使用类成员(sspp)在构造函数中存储临时值?您的优先级是int(构造函数)还是std::string (qElem)或T ?

qElem必须实现运算符<<

struct qElem { //qElem Struct
    string s;
    string p;
    qElem(string task, string priority) : s(task), p(priority) {}
    std::ostream& operator<<(std::ostream& os, const qElem & obj)
    {
      os << s << "/" << p;
      return os;
    }
};