我收到以下錯誤:無效操作數的二進制表示
Invalid operands to binary expression ("
basic_ostream<char,std::_1::char_traits<char>>
' and 'value_type
' (aka 'qElem
')) which occurs at: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必須實現運算符<< – agrum
@agrum我有另一個有點相對的錯誤問題,我只是認爲這將是一個浪費,爲它創造一個全新的問題。我已經更新了我的代碼並添加了一個排序函數。現在我試圖在排隊函數中使用該排序函數,但是我得到以下錯誤:類型爲'vector'的非常量左值引用無法綁定到無關類型'value_type'的值(又名'qElem')什麼是我做錯了? –
user260739