在'char'错误消息之前预期的primary-expression

时间:2013-12-17 15:21:07

标签: c++

我无法想出这一个,我正在拔头发。我看过你在同一个“预期的初级表达式”问题上的各种线程(这里和其他地方),并且相信我接近答案,特别是那些处理模板的问题,但我只是不能把它分开一起。我认为我有一个语法问题,并且总是发现新模板化实例化声明的语法有点棘手。

我花了很长时间编写可以遍历,插入等的二进制搜索树...我的测试程序在不同键(整数)的不同位置插入不同的字符(字符)。我需要使用traverseInorder方法简单地显示这些元素(按顺序)来测试程序。在我的测试程序中,我首先动态创建一个BinarySearchTree,并创建一个访问者,我用它来访问树以显示这些元素。但是,这是某些东西(可能是我的访问者的语法)出错了。

我的程序出现以下三个错误:

1)main.cpp | 33 |错误:在'char'之前预期的primary-expression

2)RootNode.cpp | 27 | error:为'bool RootNode :: insert(KEY,DATA)指定的冲突返回类型[使用KEY = int; DATA = char]' 3)Node.h | 22 |错误:覆盖'void Node :: insert(KEY,DATA)[使用KEY = int; DATA = char]'

4)BinaryNode.cpp | 16 |错误:'int BinaryNode :: m_tKey'是私有的 5)RootNode.cpp | 35 |错误:在此上下文中|

我不确定这些错误是否真的有关,但第一个是我最担心的错误。

我的完整代码如下:


“RootNode.cpp”

#include "Node.h"
#include "Visitor.h"
#include "BinaryNode.cpp"

template <class KEY, class DATA>
class RootNode: public Node<KEY,DATA>
{
    private:
    KEY m_tKey;
    DATA m_tData;

    BinaryNode<KEY,DATA> *m_pcFirstLink;

    public:
    RootNode():m_pcFirstLink(NULL){;}

    ~RootNode(){if (m_pcFirstLink) {delete m_pcFirstLink; m_pcFirstLink=NULL;}}

     bool insert(KEY key,DATA data)
     {
        bool m_bAdded;

        if (m_pcFirstLink) m_bAdded=m_pcFirstLink->insert(key,data);
        else
        {
            BinaryNode<KEY,DATA>* p_cBinaryNode = new BinaryNode<KEY,DATA>(key,data);
            p_cBinaryNode->m_tKey=key;
            p_cBinaryNode->m_tData=data;
            m_pcFirstLink=p_cBinaryNode;
            m_bAdded=true;
        }

        return m_bAdded;
     }

     void traverseInorder(Visitor<DATA> visitor)
     {
        if (m_pcFirstLink) m_pcFirstLink->traverseInorder(visitor);
     }

};

“BinarySearchTree.cpp”

#include <iostream>
#include <exception>
#include <stdexcept>
#include "RootNode.cpp"
#include "Visitor.h"
#include "FredTreeFullException.cpp"

using namespace std;

template <class KEY,class DATA>
class BinarySearchTree
{
    private:
    KEY m_tKey;
    DATA m_tData;
    int count;

    RootNode<KEY,DATA>* m_pcRoot;

    public:
    BinarySearchTree():count(0)
    {
    m_pcRoot = new RootNode<KEY,DATA>;
    }

    int getSize(){return count;}

    bool insert(KEY key, DATA data)
    {
    bool m_bAdded;

    if (!isFull())
        {
            m_bAdded=m_pcRoot->insert(key,data);
            if (m_bAdded) {count++;}
        }
    // if trying to insert into the binary tree while already full, throw an exception!
    else {throw FredTreeFullException();}
    }

    bool isEmpty(){(count==0)?true:false;}

    bool isFull(){return false;}

    void traverseInorder(Visitor<DATA> visitor)
    {
        if (!isFull()) m_pcRoot.traverseInorder(visitor);
    }

};

“BinaryNode.cpp”

#include "Node.h"
#include "Visitor.h"

template <class KEY, class DATA>
class BinaryNode: public Node<KEY,DATA>
{
private:
    KEY m_tKey;
    DATA m_tData;

    BinaryNode<KEY,DATA> *m_pcLeft,*m_pcRight;

public:
    BinaryNode(KEY key,DATA data)
    {
        m_tKey(key);
        m_tData(data);
        m_pcLeft(0);
        m_pcRight(0);
    }

    ~BinaryNode()
    {
        if (m_pcLeft)
        {
            delete m_pcLeft;
            m_pcLeft=0; // additionally setting to NULL for best practice
        }
        if (m_pcRight)
        {
            delete m_pcRight;
            m_pcRight=0; // additionally setting to NULL for best practice
        }
    }

    bool insert(KEY key,DATA data)
    {
        bool m_bAdded=true;
        if (key==(m_tKey))
        {
            m_tData=data;
            m_bAdded=false;
        }
        else if (key<m_tKey)
        {
            if (m_pcLeft) {m_bAdded = m_pcLeft->insert(key,data);}
            else
            {
                BinaryNode<KEY,DATA>* p_cBinaryNode = new BinaryNode<KEY,DATA>(key,data);
                p_cBinaryNode->m_tKey=key;
                p_cBinaryNode->m_tData=data;
                m_pcLeft=p_cBinaryNode;
            }
        }
        else
        {
            if (m_pcRight) {m_bAdded = m_pcRight->insert(key,data);}
            else
            {
                BinaryNode<KEY,DATA>* p_cBinaryNode = new BinaryNode<KEY,DATA>(key,data);
                p_cBinaryNode->m_tKey=key;
                p_cBinaryNode->m_tData=data;
                m_pcRight=p_cBinaryNode;
            }
        }

        return m_bAdded;
    }

    void traverseInorder(Visitor<DATA> visitor)
    {
        if (m_pcLeft) m_pcLeft->traverseInorder(visitor);
        visitor.visit(m_tData);
        if (m_pcRight) m_pcRight->traverseInorder(visitor);
    }
};

“Node.h”

#ifndef NODE_H
#define NODE_H

#include "Visitor.h"

template <class KEY, class DATA>
class Node
{
    private:
    KEY m_tKey;
    DATA m_tData;

    public:
    virtual void insert(KEY key, DATA data)=0;
    virtual void traverseInorder(Visitor<DATA> visitor)=0;
};

#endif // NODE_H_INCLUDED

“FredTreeFullException.cpp”

#include <iostream>

using namespace std;

class FredTreeFullException
{
public:
   FredTreeFullException()
   {
       cout << endl << "The Tree is Full!" << endl;
   }
};

“Visitor.h”

#ifndef VISITOR_H
#define VISITOR_H

#include <iostream>

template <class DATA>
class Visitor
{
    public:
    void visit(DATA data){std::cout << std::endl << "Data: " << data << std::endl;};

};

#endif // VISITOR_H_INCLUDED

“的main.cpp”

// Driver / test program

#include <iostream>
#include <string>
#include <string.h>
#include "BinarySearchTree.cpp"
#include "Visitor.h"

using namespace std;

int main()
{
    try
    {
    Visitor<char> cVisitor;
    BinarySearchTree<int,char>* cBinarySeachTree = new BinarySearchTree<int,char>;

    cBinarySeachTree->insert(3,'c');
    cBinarySeachTree->insert(5,'e');
    cBinarySeachTree->insert(2,'b');
    cBinarySeachTree->insert(7,'g');
    cBinarySeachTree->insert(4,'d');
    cBinarySeachTree->insert(1,'a');
    cBinarySeachTree->insert(6,'f');

    cout << "The binary tree's contents in order, are as follows:" << endl;
    cBinarySeachTree->traverseInorder(cVisitor<char>.visit);
    }
    catch(FredTreeFullException &TFE)
    {
        FredTreeFullException();
    }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

通过查看错误消息中的代码行,您应该能够非常轻松地找到错误。我不得不猜测,因为你发布的一堆代码中的行号不匹配,但是:

首先:

main.cpp|33|error: expected primary-expression before 'char'

cVisitor<char>.visit是胡言乱语。它应该是cVisitor

第二

RootNode.cpp|27|error: conflicting return type specified 

您声明虚函数没有返回类型:

virtual void insert(KEY key, DATA data)=0;

然后尝试使用返回bool的函数覆盖它:

bool insert(KEY key,DATA data)

确定返回类型应该是什么,并更改所有声明以匹配。

第三

BinaryNode.cpp|16|error: 'int BinaryNode::m_tKey' is private

您只能访问该班级中的BinaryNode的私人成员及其朋友。要从RootNode访问它,要么将其公开,要么提供公共访问者,要么让RootNode成为朋友,具体取决于您对封装的看法。