【问题标题】:Invalid operands to binary expression二进制表达式的无效操作数
【发布时间】:2014-05-23 23:38:34
【问题描述】:

我收到以下错误:

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

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

代码建议我在&amp;tasks.front() 处放置一个&amp;,但我不想接收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 我有另一个相对的错误问题,我只是认为为它创建一个全新的问题是一种浪费。我已经更新了上面的代码并添加了排序功能。现在我正在尝试在我的入队函数中使用该排序函数,但出现以下错误:对类型“vector”的非 const 左值引用不能绑定到不相关类型“value_type”的值(又名“qElem” ) 我做错了什么?

标签: c++ data-structures vector


【解决方案1】:

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

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

【讨论】:

  • "当您的 qElem 只存储 std::strings 时,为什么要使用模板 PriQueue?"好吧,最初我在我的结构中传递了一个 int 和一个字符串,如下所示: string s;诠释 p; qElem(string task, int priority) : s(task), p(priority) {} 但这给了我一个错误:tasks.push_back(qElem(ss, pp));声明“没有用于初始化 'qElem' 的匹配构造函数”抱歉重播晚了
  • 没关系,我想通了,我可能只是累了。感谢您的所有帮助。
  • 我有另一个错误问题,这有点相对,我只是认为为它创建一个全新的问题是浪费。我已经更新了上面的代码并添加了一个排序功能。现在我试图在我的入队函数中使用该排序函数,但出现以下错误:对类型“vector”的非 const 左值引用不能绑定到不相关类型“value_type”的值(又名“qElem” ) 我做错了什么?
  • @user260739 您尝试将tasksvector&lt;qElem&gt;)传递给定义为引用vector&lt;qElem*&gt; 的函数。如果您想编写的不仅仅是非常基本的 C++ 程序,那么您应该能够在没有帮助的情况下检测和纠正这种非常基本的错误。顺便说一句,这绝对不是您应该实现优先级队列的方式。每次插入后进行 O(N^2) 排序?
【解决方案2】:

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;
    }
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多