2014-05-23 52 views
2

我收到以下错误:无效操作数的二进制表示

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 
+0

qElem必须实现运算符<< – agrum

+0

@agrum我有另一个有点相对的错误问题,我只是认为这将是一个浪费,为它创造一个全新的问题。我已经更新了我的代码并添加了一个排序函数。现在我试图在排队函数中使用该排序函数,但是我得到以下错误:类型为'vector '的非常量左值引用无法绑定到无关类型'value_type'的值(又名'qElem')什么是我做错了? – user260739

回答

4

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

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

+0

“你为什么在你的qElem只存储std :: strings时使用模板PriQueue?”那么最初我是在我的结构中传递一个int和一个字符串,如下所示:string s; int p; (字符串任务,int优先级):s(任务),p(优先级){}但这给我一个错误:tasks.push_back(qElem(ss,pp));声明“没有匹配的构造函数来初始化'qElem'”P.对不起后期重播 – user260739

+0

没关系我想通了,我可能会很累。谢谢你的帮助。 – user260739

+0

我有另一个有点相对的错误问题,我只是认为这将是一个浪费,为它创造一个全新的问题。我已经更新了我的代码并添加了一个排序函数。现在我试图在排队函数中使用该排序函数,但是我得到以下错误:类型为'vector '的非常量左值引用无法绑定到无关类型'value_type'的值(又名'qElem')什么是我做错了? – user260739

1

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; 
    } 
}; 
相关问题