簡體   English   中英

無效的二進制操作數

[英]Invalid operands to binary expression

我收到以下錯誤:

二進制表達式(“ basic_ostream<char,std::_1::char_traits<char>> '和' 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時,為什么使用模板PriQueue 為什么要使用類成員( sspp )在構造函數中存儲臨時值? 您的優先級是int (構造函數)還是std::stringqElem )或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;
    }
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM