二进制表达式的操作数无效 [英] Invalid operands to binary expression
问题描述
我收到以下错误:
二进制表达式的操作数无效(
basic_ostream< char,std :: _ 1 :: char_traits< char>>
'和'value_type
'(aka'qElem
'))
发生在:
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
的值,我想要第一个值存储在我的向量中,任何帮助将不胜感激。
The code suggest I place a &
at &tasks.front()
but I do not want to receive a value of 0xfdlkajd
, I want the first value stored within my vector. Any help will be greatly appreciated.
我的代码:
#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
实现自己的运算符<
重载,但在这种情况下可能是过度的。 )
The compiler has no idea how to print out a qElem
. If you want to print out the task only, use cout << "..." << tasks.front().s << endl;
. The compiler knows how to print a std::string
. (You can also implement your own operator <<
overload for qElem
, but that's probably overkill in this case.)
请注意,您的代码有很多问题。当您的 qElem
仅存储 std :: string >时,为什么要使用模板
PriQueue
code> s?为什么要使用类成员( ss
和 pp
)将临时值存储在构造函数中?你的优先级是 int
(constructor)或 std :: string
( qElem
)或
T
?
Note that your code has lots of problems. Why are you using a template PriQueue
when your qElem
only stores std::string
s? Why are you using class members (ss
and pp
) to store temporary values in your constructor? Is your priority an int
(constructor) or a std::string
(qElem
) or a T
?
这篇关于二进制表达式的操作数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!