【问题标题】:C++: Compile Error: expected initializer before ‘<’ tokenC++:编译错误:“<”标记之前的预期初始化程序
【发布时间】:2014-12-01 04:22:54
【问题描述】:

这是一个作业相关的问题,但编译器问题不是作业,我已经实现了我需要编写的功能,现在只需要弄清楚这个编译器错误。
我已经尝试过搜索,到目前为止,我得到的接近我的问题的结果不符合导致我的编译器错误的原因。

来自 binaryTree.h

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

using namespace std;
// Definition of the Class
template <class elemType>
class binaryTreeType
{

[rest of definition]

public:
[rest of declarations]
    void createList(orderedLinkedList<elemType>& list);
[rest of declarations]
private:
    void inorderToList(nodeType<elemType> *p, orderedLinkedList<elemType>& tList) const; 
[.... then the definitions]
template <class elemType>
void bSearchTreeType<elemType>::createList(orderedLinkedList<elemType>& tList)
{
    inorderToList(this->root, tList);
}

// copies to list
template <class elemType>

void bSearchTreeType<elemType>::inorderToList(nodeType<elemType> *p, 
                                              orderedLinkedList<elemType>& tList) const
{
    if (p != NULL)
    {
        inorder(p->lLink);
        tList.insert(p->info);
        inorder(p->rLink);
    }   
 }

我收到错误:

binaryTree.h:250:错误:“

binaryTree.h:257:错误:“

createList() 和 inorderToList() 的函数定义分别是第 250 行和第 257 行。所以我对我在这里做错了什么感到有点困惑,并且确定这很简单。

【问题讨论】:

  • using namespace std; 你永远不应该把它放在头文件中,但是如果你从其他地方包含std::list,这样做可能会导致问题。
  • 您已经展示了binaryTreeType&lt;elemType&gt; 的部分声明和bSearchTreeType&lt;elemType&gt; 的函数定义。这是故意的吗?类型是否相关?
  • @etheranger :不是故意的,只是想节省空间并显示相关代码,对显示整个内容和/或orderedLinkedList.h文件没有帮助吗?
  • @shuttle87:只是搜索了代码,没有包含列表(我们是从头开始为课程编写的),这让我知道我可能有名称冲突,并更改了从 listtList 的参数名称,仍然收到相同的错误。
  • @WoodenCode,只是指出这可能是一个问题,因此该指南存在的原因。如果你想减少打字,只需通过像using std::cout 而不是using namespace std; 这样的操作将你正在使用的东西带入命名空间,这里有更多关于此的信息:stackoverflow.com/questions/1452721/…

标签: c++ templates compiler-errors g++


【解决方案1】:

好的,知道我做错了什么。

我最初将模板放在派生类 (bSearchTreeType) 中,但在将其移入父类时忘记更新方法定义。

所以新代码(第 250 和 257 行):

template <class elemType>
// below is 250
void binaryTreeType<elemType>::createList(orderedLinkedList<elemType>& tList)
{
[... same as in original post]
}

template <class elemType>
// below is 257
void binaryTreeType<elemType>::inorderToList(nodeType<elemType> *p, 
                orderedLinkedList<elemType>& tList) const
{
[... same as in original post]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多