c++ - C++ 模板故障排除简介

标签 c++ class templates

本周我们开始在 CSS 类中使用模板,它们看起来相当简单,但是,当我们开始在实验室中深入研究时,一些我们无法解决的问题开始出现。

我们收到以下错误:

../Lab7PartC/DLNode.cpp:13:5: error: invalid use of template-name ‘lab7::DLNode’ without an argument list
../Lab7PartC/DLNode.cpp:20:5: error: invalid use of template-name ‘lab7::DLNode’ without an argument list
../Lab7PartC/DLNode.cpp:26:5: error: expected unqualified-id before ‘template’
../Lab7PartC/DLNode.cpp:33:5: error: invalid use of template-name ‘lab7::DLNode’ without an argument list

这是相关的类.cpp 文件:

/*
 * DLNode.cpp
 *
 *  Created on: Mar 29, 2013
 *      Author: tony
 */

#include <cstddef>
#include "DLNode.h"

namespace lab7 {

    DLNode::DLNode() {
        data = 0;
        next = NULL;
        prev = NULL;
    }

    template<class T>
    DLNode::DLNode(T d) {
        data = d;
        next = NULL;
        prev = NULL;
    }

    template<class T>
    DLNode::DLNode(T d, DLNode* n, DLNode* p) {
        data = d;
        next = n;
        prev = p;
    }

    DLNode::~DLNode() {
        delete next;
    }



} /* namespace lab7 */

这是相关的 .h 文件:

/*
 * DLNode.h
 *
 *  Created on: Mar 29, 2013
 *      Author: tony
 */

#ifndef DLNODE_H_
#define DLNODE_H_

namespace lab7 {
    template<class T>
    class DLNode {
    public:
        DLNode();
        DLNode(T data_in);
        DLNode(T data_in, DLNode* n, DLNode* p);
        virtual ~DLNode();

        void setNext(DLNode* n) {
            next = n;
        }

        void setPrev(DLNode* p) {
            prev = p;
        }

        int getData() {
            return data;
        }

        DLNode* getNext() {
            return next;
        }

        DLNode* getPrev() {
            return prev;
        }
    private:
        T data;
        DLNode* next;
        DLNode* prev;
    };

} /* namespace lab7 */
#endif /* DLNODE_H_ */

我们花了一个晚上的时间试图解决这个问题,并感谢有关该主题的任何建议。谢谢。

我不想再次打扰你,但我修改了头文件以包含你建议的整个定义,这就是我所拥有的,这次我包含了使用节点类的附加类:

/*
 * DLNode.h
 *
 *  Created on: Mar 29, 2013
 *      Author: tony
 */

#ifndef DLNODE_H_
#define DLNODE_H_

namespace lab7 {

    template<class T>
    class DLNode {
    public:
        DLNode<T>();
        DLNode<T>(T data_in);
        DLNode<T>(T data_in, DLNode* n, DLNode* p);
        ~DLNode<T>();

        void setNext<T>(DLNode* n) {
            next = n;
        }

        void setPrev<T>(DLNode* p) {
            prev = p;
        }

        int getData<T>() {
            return data;
        }

        DLNode* getNext<T>() {
            return next;
        }

        DLNode* getPrev<T>() {
            return prev;
        }
    private:
        T data;
        DLNode* next;
        DLNode* prev;
    };

    template<class T>
    DLNode<T>::DLNode() {
        data = 0;
        next = NULL;
        prev = NULL;
    }

    template<class T>
    DLNode<T>::DLNode(T d) {
        data = d;
        next = NULL;
        prev = NULL;
    }

    template<class T>
    DLNode<T>::DLNode(T d, DLNode* n, DLNode* p) {
        data = d;
        next = n;
        prev = p;
    }

    template<class T>
    DLNode<T>::~DLNode() {
        delete next;
    }
} /* namespace lab7 */
#endif /* DLNODE_H_ */

--以及附加的列表类头文件:

/*
 * DLList.h
 *
 *  Created on: Mar 29, 2013
 *      Author: tony
 */

#ifndef DLLIST_H_
#define DLLIST_H_


namespace lab7 {

    class DLNode<T>;

    template<class T>
    class DLList {
    public:
        DLList();
        virtual ~DLList();
        void insert(T data);
        void print();
        bool search(const T key);
        bool searchandremove(const T key);
        void swap();
        DLNode* findNode(const T key);
    private:
        DLNode* first;
    };

    template<class T>
    DLList<T>::DLList() {
        first = NULL;
    }

    template<class T>
    DLList<T>::~DLList() {
        delete first;
    }

    template<class T>
    void DLList<T>::insert(T data) {
        DLNode<T>* temp = first;
        first = new DLNode(data, first, NULL);
        if (temp == NULL)
            return;
        temp->setPrev(first);
    }

    template<class T>
    void DLList<T>::print() {
        DLNode* temp = first;
        while (temp != NULL) {
            cout << temp->getData() << " ";
            temp = temp->getNext();
        }
        cout << endl;
    }

    template<class T>
    bool DLList<T>::search(const T key) {
        DLNode* temp = new DLNode;
        bool found = false;
        for (temp = first; temp != NULL; temp = temp->getNext()) {
            if (temp->getData() == key)
                found = true;
        }
        return found;
    }

    template<class T>
    bool DLList<T>::searchandremove(const T key) {
        DLNode* prev = new DLNode;
        DLNode* next = new DLNode;
        DLNode* toDelete = new DLNode;
        toDelete = findNode(key);
        if (toDelete != NULL) {
            prev = toDelete->getPrev();
            next = toDelete->getNext();
            toDelete->setNext(NULL);
            toDelete->setPrev(NULL);
            if (toDelete == first) {
                first = next;
                next->setPrev(first);
            } else {

                prev->setNext(next);
                if (next != NULL)
                    next->setPrev(prev);
            }
            delete toDelete;
            return true;
        }
        return false;
    }

    template<class T>
    DLNode* DLList<T>::findNode(const T key) {
        DLNode* temp = new DLNode;
        for (temp = first; temp != NULL; temp = temp->getNext()) {
            if (temp->getData() == key)
                return temp;
        }
        return NULL;
    }

    template<class T>
    void DLList<T>::swap() {
        DLNode* last = new DLNode;
        //navigate to second to last node
        for (last = first; last->getNext() != NULL; last = last->getNext()) {
        }
        last->getPrev()->setNext(first);
        first->setPrev(last->getPrev());
        last->setPrev(NULL);
        last->setNext(first->getNext());
        first->getNext()->setPrev(last);
        first->setNext(NULL);
        first = last;

    }
} /* namespace lab7 */
#endif /* DLLIST_H_ */

这伴随着我不理解的额外过多错误:

In file included from ../Lab7PartC/Lab7B.cpp:13:0:
../Lab7PartC/DLList.h:14:11: error: ‘DLNode’ is not a template
../Lab7PartC/DLList.h:14:18: error: ‘T’ was not declared in this scope
../Lab7PartC/DLList.h: In member function ‘void lab7::DLList<T>::insert(T)’:
../Lab7PartC/DLList.h:43:9: error: ‘lab7::DLNode’ is not a template
../Lab7PartC/DLList.h:47:13: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h: In member function ‘void lab7::DLList<T>::print()’:
../Lab7PartC/DLList.h:54:25: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:55:24: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h: In member function ‘bool lab7::DLList<T>::search(T)’:
../Lab7PartC/DLList.h:62:28: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:64:53: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:65:21: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h: In member function ‘bool lab7::DLList<T>::searchandremove(T)’:
../Lab7PartC/DLList.h:73:28: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:74:28: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:75:32: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:78:28: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:79:28: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:80:21: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:81:21: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:84:21: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:87:21: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:89:25: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h: In member function ‘lab7::DLNode* lab7::DLList<T>::findNode(T)’:
../Lab7PartC/DLList.h:99:28: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:100:53: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:101:21: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h: In member function ‘void lab7::DLList<T>::swap()’:
../Lab7PartC/DLList.h:109:28: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:111:32: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:111:64: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:113:13: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:114:14: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:114:28: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:115:13: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:116:13: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:116:28: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:117:14: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:118:14: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h: In member function ‘void lab7::DLList<T>::insert(T) [with T = int]’:
../Lab7PartC/Lab7B.cpp:23:26:   instantiated from here
../Lab7PartC/DLList.h:44:9: error: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: error: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h: In member function ‘bool lab7::DLList<T>::searchandremove(T) [with T = int]’:
../Lab7PartC/Lab7B.cpp:34:32:   instantiated from here
../Lab7PartC/DLList.h:91:13: warning: possible problem detected in invocation of delete operator:
../Lab7PartC/DLList.h:75:17: warning: ‘toDelete’ has incomplete type
../Lab7PartC/DLList.h:14:11: warning: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:91:13: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined.
../Lab7PartC/DLList.h: In destructor ‘lab7::DLList<T>::~DLList() [with T = int]’:
../Lab7PartC/Lab7B.cpp:46:1:   instantiated from here
../Lab7PartC/DLList.h:38:9: warning: possible problem detected in invocation of delete operator:
../Lab7PartC/DLList.h:38:9: warning: invalid use of incomplete type ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:14:11: warning: forward declaration of ‘struct lab7::DLNode’
../Lab7PartC/DLList.h:38:9: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined.

最佳答案

首先,模板应该只在头文件中实现。参见 Why can templates only be implemented in the header file?

现在,DLNode不是一个类,它是一个类模板,所以它应该总是有一个模板参数,即 DLNode<T>DLNode<int> .现在,您可能想知道为什么不在类主体中执行此操作,例如 void setNext(DLNode* n) ,这是因为该参数隐含在类作用域中。

我写的看起来很困惑,但一切应该是这样的:

template<class T>
DLNode<T>::DLNode() {
    data = 0;
    next = NULL;
    prev = NULL;
}

template<class T>
DLNode<T>::DLNode(T d) {
    data = d;
    next = NULL;
    prev = NULL;
}

template<class T>
DLNode<T>::DLNode(T d, DLNode* n, DLNode* p) {
    data = d;
    next = n;
    prev = p;
}

template<class T>
DLNode<T>::~DLNode() {
    delete next;
}

正如我所说,这段代码需要在头文件而不是cpp文件中。虽然一旦你这样做了,在类主体之外定义它们就变得毫无意义了,所以只需在类主体内定义所有内容:

namespace lab7 {
    template<class T>
    class DLNode {
    public:
        DLNode() {
            data = 0;
            next = NULL;
            prev = NULL;
        }

        // ...
    };

} /* namespace lab7 */

关于c++ - C++ 模板故障排除简介,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15847096/

相关文章:

c++ - 测试 mini Matrix ADT 时的核心转储

python - 在 Django 中创建可重复使用的模板

c++ - 是否有必要 #include <exception>,当我似乎能够使用 std::exception 即使它不包括在内?

c++ - 在哪里告诉 Qt Creator 搜索头文件以进行自动完成

c++ - OpenGL 绘制顶点缓冲区对象

c++ - 如何在编译时确定此返回类型?

c++ - 搜索字符串中的垃圾字符

java - java中的跨类同步

javascript - 在元素上添加/删除最新应用的类

c# - 如何创建一个将属于解决方案资源管理器中某个类的类?