expected primary-expression before 'char' error message
我想不出这个来,就把头发拔出来。我看过你在同一个"预期主表达式"问题上的不同主题(这里和其他地方),我相信我已经接近答案了,特别是在处理模板的问题上,但我无法将其拼凑在一起。我认为我有一个语法问题,并且总是发现新模板化实例化声明的语法有点复杂。
我花了很长时间编写了一个二进制搜索树,它可以遍历、插入等。我的测试程序在不同键(整数)的下标处插入不同的字符。我需要使用TraverseInOrder方法简单地显示那些元素(按顺序)来测试程序。在我的测试程序中,我首先动态地创建了一个binarysearchtree,还创建了一个访问者,我使用它访问树来显示这些元素。然而,这正是某些东西(可能是我对访问者的语法)出错的地方。
我的程序出现以下三个错误:
1)main.cpp 33错误:在'char'之前需要主表达式
2)rootnode.cpp 27错误:为"bool rootnode::insert(key,data)[with key=int;data=char]指定的返回类型冲突3)node.h 22错误:重写"void node::insert(key,data)[with key=int;data=char]"
4)binarynode.cpp 16错误:"int binarynode::m_tkey"是私有的5)rootnode.cpp_35错误:在此上下文中|
我不确定这些错误是否真的相关,但第一个错误是我最担心的。
我的完整代码如下:
"RooNo.CPP"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #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); } }; |
"二进制搜索树.cpp"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | #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); } }; |
"二进制节点.cpp"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | #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); } }; |
"节点H"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #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"
1 2 3 4 5 6 7 8 9 10 11 12 | #include <iostream> using namespace std; class FredTreeFullException { public: FredTreeFullException() { cout << endl <<"The Tree is Full!" << endl; } }; |
"访客H"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #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 |
"主CPP"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | // 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 | main.cpp|33|error: expected primary-expression before 'char' |
第二:
1 | RootNode.cpp|27|error: conflicting return type specified |
您声明的虚拟函数没有返回类型:
1 | virtual void insert(KEY key, DATA data)=0; |
然后尝试用返回
1 | bool insert(KEY key,DATA data) |
决定返回类型应该是什么,并更改所有声明以匹配。
第三:
1 | BinaryNode.cpp|16|error: 'int BinaryNode::m_tKey' is private |
您只能访问该类中