错误消息 C++
我在尝试编译代码时收到此错误消息:
btree.h:23: error: Expected unqualified-id before 'template'
它来自这一行:
template
除了一堆注释和一些 #include
,这一行上面绝对没有任何内容> 库文件,它们是:
#include <iostream>
#include <cstddef>
#include <utility>
#include "btree_iterator.h"
我的 btree_iterator.h 包含:
template <typename T>
class btree_iterator {
}
如果有人能告诉我出了什么问题,我将不胜感激!
I got this error message when trying to compile my code:
btree.h:23: error: expected unqualified-id before 'template'
it comes from this line:
template <typename T> std::ostream& operator<<(std::ostream& os, const btree<T>& tree);
there is absolutely nothing above this line except a bunch of comments and a couple of #include
library files, which are:
#include <iostream>
#include <cstddef>
#include <utility>
#include "btree_iterator.h"
my btree_iterator.h holds:
template <typename T>
class btree_iterator {
}
if someone could tell me whats wrong it'd be much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你忘记了一个分号:
You forgot a semicolon:
注释掉该行并放置一个非常简单的行,可能是 typedef char mychar; 如果您在该行上收到错误,那么您可以发现实际的错误位于 btree_iterator 的末尾.h。
因为在 C 和 C++ 中,包含文件只是粘贴包含文件的内容。如果包含文件中存在拼写错误(可能是缺少分号),则会将其带入原始文件中。
Comment that line out and put a very simple line, maybe a
typedef char mychar;
If you get an error on that line then you can figure out that the actual mistake is at the end ofbtree_iterator.h
.Because in C and C++ include files just paste the contents of the include file. If there is a typo in the include file, perhaps a missing semicolon, that carries through into the original file.
如果您在类中声明它,则应该模板化类本身并直接在其中定义它。
同样,对于模板类,您不能将类严格分离为头文件(.h)和实现文件(.cpp),因为这样编译器将无法在运行时创建正确的对象,就好像它只能看到头文件一样。因此,在同一个“.h”文件中,写入声明和定义。
If you are declaring it within the class, you should templatize the class itself and directly define this within it.
Also with template classes, you cannot separate the class into a strictly header(.h) and implementation(.cpp) file because then the compiler will not be able to create the correct objects at runtime as if it can only see the header. So in the same '.h' file, write both the declaration and definition.