When I run the following code…
#ifndef KEYEDITEM_H_INCLUDED
#define KEYEDITEM_H_INCLUDED
#include <string>
typedef std::string KeyType;
class KeyedItem {
public:
KeyedItem() {}
KeyedItem(const KeyType& keyValue) : searchKey(keyValue) {}
KeyType getKey() const
{ return searchKey;
}
private:
KeyType searchKey; };
#endif // KEYEDITEM_H_INCLUDED
I get an error message “error: expected initializer before ‘KeyType'”
I thought at first that this could be related to the declaring the string type so I changed it to the following to see if it would work…
#ifndef KEYEDITEM_H_INCLUDED
#define KEYEDITEM_H_INCLUDED
#include <string>
//typedef std::string KeyType;
class KeyedItem
{
public:
KeyedItem() {}
KeyedItem(const std::string& keyValue) : searchKey(keyValue) {}
std::string getKey() const
{ return searchKey;
}
private:
std::string searchKey;
};
#endif // KEYEDITEM_H_INCLUDED
but I got the error “error: multiple types in one declaration” I have looked for errors for both of these errors and have found nothing that helps. I have gone over the class to make sure I have semi-colons where needed and I seem to have all of them.
I don’t have a implementation file simply because I didn’t need one, but could that be the problem?
This is just a class for a binary search tree. I am working in CodeBlocks using the GNU GCC compiler.
TreeNode.h
#ifndef TREENODE_H_INCLUDED
#define TREENODE_H_INCLUDED
#include "KeyedItem.h"
typedef KeyedItem TreeItemType;
class TreeNode
{
private:
TreeNode() {}
TreeNode(const TreeItemType& nodeItem,
TreeNode *left = NULL,
TreeNode *right = NULL) : item(nodeItem), leftChildPtr(left), rightChildPtr(right) {}
TreeItemType item;
TreeNode *leftChildPtr, *rightChildPtr;
friend class BinarySearchTree;
};
#endif // TREENODE_H_INCLUDED
Solved… Turns out I was missing a header include in my main.