在C++ primer plus 第六版中,程序清单12.10 queue.h,在Queue类的私有域中重载了赋值运算符,在编译时报错,我将这个赋值运算符函数注释后,编译通过。附上我手敲的代码
12.10_queue.h
#ifndef QUEUE_H_
#define QUEUE_H_
class Customer
{
private:
long arrive;
int processtime;
public:
Customer(){arrive = processtime = 0;}
void set(long when);
long when() const {return arrive;}
int ptime() const {return processtime;}
};
typedef Customer Item;
class Queue
{
private:
struct Node { Item item; struct Node *next;};
enum {Q_SIZE = 10};
Node *front;
Node *rear;
int items;
const int qsize;
Queue(const Queue &q) : qsize(0)
{
}
Queue& opetator=(const Queue &q)
{
return *this;
}
public:
Queue(int qs = Q_SIZE);
~Queue();
bool isempty() const;
bool isfull() const;
int queuecount() const;
bool enqueue(const Item &item);
bool dequeue(Item &item);
};
#endif
12.11_queue.cpp
#include "12.10_queue.h"
#include <iostream>
Queue::Queue(int qs) : qsize(qs)
{
front = rear = 0;
items = 0;
}
Queue::~Queue()
{
Node *temp;
while(front != NULL)
{
temp = front;
front = front->next;
delete temp;
}
}
bool Queue::isempty() const
{
return items == 0;
}
bool Queue::isfull() const
{
return items == qsize;
}
int Queue::queuecount() const
{
return items;
}
bool Queue::enqueue(const Item &item)
{
if(isfull())
{
return false;
}
Node *add = new Node;
add->item = item;
add->next = NULL;
items++;
if(front == NULL)
{
front = add;
}
else
{
rear->next = add;
}
rear = add;
return true;
}
bool Queue::dequeue(Item &item)
{
if(front == NULL)
{
return false;
}
item = front->item;
items--;
Node *temp = front;
front = front->next;
delete temp;
if(items == 0)
{
rear = NULL;
}
return true;
}
void Customer::set(long when)
{
processtime = std::rand() % 3 + 1;
arrive = when;
}
12.12_bank.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "12.10_queue.h"
const int MIN_PER_HR = 60;
bool newcustomer(double x);
int main()
{
using std::cin;
using std::cout;
using std::endl;
using std::ios_base;
std::srand(std::time(0));
cout << "hello world" << endl;
return 0;
}
编译命令 g++ 12.10_queue.h 12.11_queue.cpp 12.12_bank.cpp -o queue -std=c++11
编译出错提示如下:
希望有先生能帮忙解答,不胜感激
我想知道的是重载赋值运算符函数为什么这样子写会出错,谢谢啦
在vscode中他提示我这些信息
先把operator拼对再找找别的问题?