【发布时间】:2015-11-10 19:48:43
【问题描述】:
我已经通过 Google 和 Stack Overflow 进行了梳理,寻找这个问题的答案。我知道这是超级基本的,我真的很生气,我不知道出了什么问题。所以我的研究生教授希望我们实现一个整数队列。为什么?因为这是“编程原理”,我们基本上是用超级简洁的定义来复习一个小细节。老实说,这是我不得不参加的三个小时课程的最大浪费,但至少老师很好。无论如何,我已经在 .h 文件中找到了类定义,其中包含
#ifndef QUEUE_H
#define QUEUE_H
class Queue
{
private:
std::vector<int> myQueue;
public:
int push(int);
int pop_front();
int size();
bool empty();
};
#endif
所以,我觉得 Queue::size() 和 Queue::empty 被声明对了吗?嗯,在我的 queue.cpp 文件中,我已经得到了这些函数的详细信息……
#include <vector>
#include "queue.h"
int Queue::push(int intToPush)
{
myQueue.push_back(intToPush);
return 0;
};
int Queue::pop_front()
{
int front = myQueue.front();
myQueue.erase(myQueue.begin());
return front;
};
/*
int Queue::size(); <- It was these semicolons
{
return myQueue.size();
};
bool Queue::empty(); <- This one too.
{
return myQueue.empty();
};*/
当我像这样编译它时,它确实很好,但是当我包含我已经注释掉的那两个时,它给出了我在这篇文章标题中输入的错误。
我真的很抱歉这是如此简单和愚蠢,但我找不到任何可以帮助我的东西,而且这些函数导致编译器说它们是在类之外声明的,这让我非常困惑。我尝试使用泛型返回,0 和 false,并得到相同的编译错误。
【问题讨论】:
-
作为一般规则,在有关编译错误的问题中包含编译错误会有所帮助。
-
我会的,我已经将它包含在帖子的标题中,所以我不想变得多余。编辑:如果其他人试图用谷歌搜索编译错误,这样它更有可能出现在谷歌上,所以也许他们也能得到帮助?