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

标签 c++ data-structures vector

我收到以下错误:

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 的值,我希望第一个值存储在我的 vector 中。任何帮助将不胜感激。

我的代码:

#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 . (您也可以为 operator << 实现自己的 qElem 重载,但在这种情况下,这可能有点矫枉过正。)

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

关于c++ - 二进制表达式的无效操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23839780/

相关文章:

vector - Rust 特征向量 : cast each trait

c++ - Matlab 2017a 无法识别编译器(Error using mex No supported compiler or SDK was found)

c++ - #if 预处理器指令可以嵌套在 C++ 中吗?

c++ - 定点处理器中浮点运算的数据结构

c++ - 使用 ifstream 在 while 循环中将数据传递给 vector

c++ - 附加时是否保留 vector 元素的顺序?

c++ - 如何从堆栈中找到崩溃的代码行?

c++ - 函数的签名

c++ - DAWG 与基数树?

algorithm - 预测数字相加的数字