c++ - 为什么包含.h文件报错,包含.cpp文件却可以?!
伊谢尔伦
伊谢尔伦 2017-04-17 13:15:27
0
2
658

头文件:XXX.h

#ifndef QUEUE_BUFFER_H_
#define QUEUE_BUFFER_H_


template<typename T>
struct Buffer
{
    T _data;
    int _BufNo;
    Buffer *next;
    Buffer(int No, T data): _data(data), _BufNo(No), next(NULL) {}
};

template<typename T>
class Buffer_Queue
{
private:
    int _length; // 队列元素个数
    Buffer<T> *front; // 队列的头指针
    Buffer<T> *rear;  // 队列的尾指针
    bool isEmpty() const; // 判断队列是否为空
public:
    Buffer_Queue(T data, int length = 10); // 构造函数
    ~Buffer_Queue();
    Buffer<T> *getbuf(); // 获得空的buffer(出队)
    void putbuf(Buffer<T> *buf); // 挂载到队尾(入队)
    

    void display(); // 测试输出

};

#endif

定义:XXX.cpp

#include <cstdlib>
#include <iostream>
#include "queue_buffer.h"

using std::cout;
using std::endl;
using std::cerr;


template<typename T>
Buffer_Queue<T>::Buffer_Queue(T data, int length): _length(length) // 构造函数
{
    front = rear = NULL;        
    int i = 0;

    if (i < _length)
    {
        // 分配第一个结点
        Buffer<T> *p = new Buffer<T>(i++, data);
        if (!p) 
        {
            cerr << "Memory alloc failed" << endl;
            exit(1);
        }
        front = rear = p;
        front->next = NULL; // 多余
    }


    while (i < _length)
    {
        Buffer<T> *p = new Buffer<T>(i++, data);
        if (!p)
        {
            cerr << "Memory alloc failed" << endl;
            exit(1);
        }
        rear->next = p;
        rear = p;
    }
        
}

template<typename T>
bool Buffer_Queue<T>::isEmpty() const
{
    return _length == 0;    
}

template<typename T>
Buffer<T> * Buffer_Queue<T>::getbuf() // 出队
{
    if (isEmpty())    // 如果队列为空,则返回NULL
        return NULL;

    Buffer<T> *p = front;
    front = front->next;
    _length--;
    p->next = NULL;
    if (rear == p) // 只有一个结点
        rear = front;

    return p;
}

template<typename T>
void Buffer_Queue<T>::putbuf(Buffer<T> *buf) // 入队
{
    if (isEmpty())    // 如果队列为空
    {
        front = rear = buf;
        front->next = NULL;
        ++_length;
    }

    // 队列不为空
    rear->next = buf;
    rear = buf;
    rear->next = NULL;
    ++_length;
}

template<typename T>
void Buffer_Queue<T>::display()
{
    Buffer<T> *curr = front; 
    while (curr)
    {
        cout << curr->_BufNo << ": " << curr->_data << endl;    
        curr = curr->next;
    }
}

template<typename T>
Buffer_Queue<T>::~Buffer_Queue()
{
    Buffer<T> *curr = NULL;
    while (front)
    {
        curr = front;
        front = front->next;
        delete curr;
    }
}

测试:test.cpp

#include <iostream>
#include "queue_buffer.h"

int main(void)
{
    Buffer_Queue<int> test(-1); // 10个

    test.display();

    Buffer<int> *p = new Buffer<int>(10, -1);
    test.putbuf(p);

    Buffer_Queue<int> temp(-1, 0); // 0个
    temp.putbuf(p);

    test.display();
    temp.display();

    return 0;
}

编译: g++ xxx.cpp test.cpp -o test
test.cpp文件中包含xxx.h就报错,undefined reference to XXXXXXXX(是调用的那些函数)
test.cpp文件中包含xxx.cpp就不会报错,一切正常

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
小葫芦
  1. include does not care whether it is h, cpp, or ini, it just opens that file and expands it into the current file
    You can compile it with g++ -E and see the final compiled file

  2. Is the link wrong? Because your header file contains template classes. However, all current C++ compilers do not support template separation compilation. In other words, the implementation of the function has to be Write it in the header file

Ty80

Based on the rich C++ experience I accumulated through my classmates when I was a student, I think you use iostream in your header file. XXX.cpp did not hang because you included iostream for it.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!