我正在用 C++ 创建一个队列类,但在使用 makefile 编译时遇到了问题。我的 queue.cpp 课程在这里
#include "queue.h"
#include <stdlib.h>
queue::queue()
{
front_p = NULL;
back_p = NULL;
current_size = 0;
}
void queue::enqueue(int item)
{
node newnode = node(item, NULL);
if (front_p == NULL) //queue is empty
{
front_p = &newnode;
back_p = &newnode;
}
else
{
back_p->next = &newnode;
back_p = &newnode;
}
current_size ++;
}
我的头文件(queue.h)在这里
class queue
{
public:
queue(); // constructor - constructs a new empty queue.
void enqueue( int item ); // enqueues item.
int dequeue(); // dequeues the front item.
int front(); // returns the front item without dequeuing it.
bool empty(); // true iff the queue contains no items.
int size(); // the current number of items in the queue.
int remove(int item); // removes all occurrances of item
// from the queue, returning the number removed.
private:
class node // node type for the linked list
{
public:
node(int new_data, node * next_node ){
data = new_data ;
next = next_node ;
}
int data ;
node * next ;
};
node* front_p ;
node* back_p ;
int current_size ; // current number of elements in the queue.
};
测试程序(tester.cpp)
#include <iostream>
#include "queue.h"
#include <stdlib.h>
using namespace std;
int main(int argc, char * const argv[])
{
cout << "Lalalalala" << endl;
queue q1;
q1.enqueue(5);
}
生成文件
all: tester
tester: queue.o
g++ -o tester tester.cpp
queue.o: queue.cpp queue.h
g++ -c queue.cpp
clean:
rm -f tester *.o
当我输入“make”或“make all”时,我得到这个错误:
g++ -o tester tester.cpp
/tmp/ccTOKLWU.o: In function `main':
tester.cpp:(.text+0x33): undefined reference to `queue::queue()'
tester.cpp:(.text+0x44): undefined reference to `queue::enqueue(int)'
collect2: ld returned 1 exit status
make: *** [tester] Error 1
它的不同寻常之处在于,在 Windows 机器上的 Visual Studio 中编译时,没有错误。我一点也不知道为什么它不应该像我这样在 linux 机器上编译。有人能解释一下吗?