【发布时间】:2020-06-13 08:20:54
【问题描述】:
返回此运行时错误
$/usr/bin/ld: QueueusingSLL: _ZSt4cout: invalid version 2 (max 0)
$/usr/bin/ld: QueueusingSLL: error adding symbols: bad value
collect2: error: ld returned 1 exit status
另外,如何从类成员函数返回迭代器?
请参阅代码中的auto iterator() 函数。
源代码
#include<iostream> // std::cout
#include<forward_list> // std::singly linked list
#include<iterator> //// std::iterator,next(is a random iterator)
//#include <algorithm> // std::for_each loop
using namespace std;
class Queue
{
private:
forward_list<int> list;
public:
Queue()
{}
Queue(int firstElem) {
enqueue(firstElem);
}
//Return the size of Queue
int size()
{
forward_list<int>::iterator first = list.begin();
forward_list<int>::iterator last = list.end();
return std::distance(first, last);
}
//Return whether or not the queue us empty
void isEmpty()
{
std::cout << "Queue " << (list.empty() ? "is empty" : "is not empty") << std::endl;
}
// Peek the top of the stack without removing an element
// Returns 0 if queue is empty.
int peek()
{
if (list.empty() == 1)
return 0;
return list.front();
}
//Add an element at back(end or tail) of queue:enqueue
void enqueue(int Elem)
{
auto pos = list.begin();
while (std::next(pos) != list.end()) ++pos;
list.insert_after(pos, Elem);
}
//Remove an element from front of queue:dequeue
int dequeue()
{
if (list.empty() == 1) return 0;
list.pop_front();
return 1; //1 means success
}
void display()
{
cout << endl << "Elements in Queue x";
for (forward_list<int>::iterator i = list.begin(); i != list.end(); i++)
{
cout << endl << "\t" << *i;
}
cout << endl << endl;
}
/*auto iterator()
{
return list.iterator();
}*/
};
int main()
{
Queue Q1, Q2(5);
Q1.isEmpty();
Q2.isEmpty();
cout << endl << "sizeofQ1 " << Q1.size() << endl << "sizeofQ2 " << Q2.size() << endl << endl;
Q1.enqueue(10);
Q1.enqueue(20);
Q1.enqueue(30);
Q1.enqueue(40);
Q1.enqueue(50);
Q1.enqueue(60);
Q1.display();
Q2.display();
cout << endl << "sizeofQ1 " << Q1.size() << endl << endl;
Q1.dequeue();
Q1.dequeue();
Q1.display();
cout << endl << "sizeofQ1 " << Q1.size() << endl << endl;
return 0;
}
【问题讨论】:
标签: c++ c++11 data-structures iterator queue