实例化模板类时出现奇怪的错误消息

发布于 2024-09-30 05:21:50 字数 725 浏览 0 评论 0原文

我收到一个我不明白的错误。我发现甚至在 SO 上提出了类似的问题,但给出的修复已经在我的代码中。

我在这一行中收到错误:

ForestNode<NODETYPE> foo = new ForestNode<NODETYPE> ForestNode(bar);

内容为:

\project 4\forest.h|85|error: Expected ',' or ';'在'ForestNode'之前

我的类forestnode是这样定义的:

template<typename NODETYPE> class Forest;

template<typename NODETYPE> class ForestNode
{
    friend class Forest<NODETYPE>;

    public:
        ForestNode( const NODETYPE &);
        ~ForestNode();
        NODETYPE getTag() const;
    private:
        NODETYPE tag;
        ForestNode<NODETYPE> *leftChild;
        ForestNode<NODETYPE> *sibling;
};

有什么想法吗?

I am getting an error I do not understand. There was even a similar question asked on SO that I found, but the fix given is already in my code.

I am getting an error in this line:

ForestNode<NODETYPE> foo = new ForestNode<NODETYPE> ForestNode(bar);

that reads :

\project 4\forest.h|85|error: expected ',' or ';' before 'ForestNode'

My class forestnode is defindes as such:

template<typename NODETYPE> class Forest;

template<typename NODETYPE> class ForestNode
{
    friend class Forest<NODETYPE>;

    public:
        ForestNode( const NODETYPE &);
        ~ForestNode();
        NODETYPE getTag() const;
    private:
        NODETYPE tag;
        ForestNode<NODETYPE> *leftChild;
        ForestNode<NODETYPE> *sibling;
};

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风挽心 2024-10-07 05:21:50

您在构造函数调用中有两次类型名称,请尝试:

ForestNode<NODETYPE> foo = new ForestNode<NODETYPE>(bar);

You have the type name twice in the constructor call, try:

ForestNode<NODETYPE> foo = new ForestNode<NODETYPE>(bar);
爱给你人给你 2024-10-07 05:21:50

除了一行上有 2 个构造函数之外,您还不能将指针分配给变量。你要么必须这样做:

ForestNode *foo = new ForestNode;

或执行此操作:

ForestNode<NODETYPE> foo;

或执行此操作:

ForestNode<NODETYPE> bar;
ForestNode<NODETYPE> foo( bar );

Besides having 2 constructors on one line, you can not allocate the pointer to the variable. You either have to do this :

ForestNode *foo = new ForestNode;

or do this :

ForestNode<NODETYPE> foo;

or this :

ForestNode<NODETYPE> bar;
ForestNode<NODETYPE> foo( bar );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文