c++ - 尝试在模板类中使用模板对象时出现问题

标签 c++ templates most-vexing-parse

考虑以下类定义......

节点

    template <class T> class Node {
        private :
            T* data;
            Node<T>* next;
        public :
            Node(T* data);
            void setData(T* data);
            T* getData();
            void setNext(Node<T>* next);
            Node<T>* getNext();
    };

链表

    template <class T> class LinkedList {
        private :
            Node<T>* start;
        public :
            LinkedList();
            void add(Node<T>* node);
            bool isEmpty();
    };

主要

    #include "Foo.h"
    int main() {
        Foo foo();
        Node<Foo> node(&foo);
        LinkedList<Foo> linkedList();
        linkedList.add(&node);
        return 0;
    }

编译时会抛出以下错误...

    Request for member 'add' in 'linkedList', which is of non-class type 'LinkedList<Foo>()'

我在使用模板方面经验不足,因此非常感谢任何帮助。

最佳答案

Foo foo();LinkedList<Foo> linkedList();不是变量而是函数原型(prototype)。

请使用Foo foo;LinkedList<Foo> linkedList;相反。

您还可以使用 Foo foo = Foo();LinkedList<Foo> linkedList = LinkedList<Foo>();明确调用了构造函数。

关于c++ - 尝试在模板类中使用模板对象时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32643989/

相关文章:

c++ - 内存池和 <Unable to read memory>

c++ - C++ 中上周一的时间戳

c++ - C2653 : 'GUI' : is not a class or namespace

c++ - 使用带有宏的 decltype 显式实例化类成员函数

c++ - 这两个版本的代码有什么区别?

c++ - 如何使用makefile编译arduino核心库?

java - 编辑 "Test for Existing Class"的 Netbeans 模板

c++ - 无法在模板类中使用 typedef 编译示例

c++ - iostream 不打印到第二个源类中的终端 (c++)

c++ - 为什么匿名对象有时需要默认构造函数?