C++ “错误:一个声明中有多种类型”和“错误:预期的初始化程序在“KeyType”之前
当我运行以下代码时...
#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
我收到一条错误消息“错误:'KeyType'之前的预期初始化程序”
我首先认为这可能与声明字符串类型有关,所以我将其更改为以下内容以查看是否可以会起作用...
#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
但我收到错误“错误:一个声明中有多种类型”我已经查找了这两个错误的错误,但没有发现任何有帮助的信息。我已经仔细检查了课程,以确保在需要的地方有分号,而且我似乎已经全部都有了。
我没有实现文件只是因为我不需要它,但这可能是问题所在吗?
这只是二叉搜索树的一个类。我正在使用 GNU GCC 编译器在 CodeBlocks 中工作。
树节点.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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用 g++ 而不是 gcc 进行编译
you need to compile with g++ not gcc
解决了...原来我缺少一个包含在我的 main.c 文件中的标头。
Solved... Turns out I was missing a header include in my main.