错误消息 C++

发布于 2024-12-10 13:00:44 字数 614 浏览 0 评论 0原文

我在尝试编译代码时收到此错误消息:

btree.h:23: error: Expected unqualified-id before 'template'

它来自这一行:

template; std::ostream& operator<<(std::ostream& os, const btree& tree);

除了一堆注释和一些 #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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丑丑阿 2024-12-17 13:00:44

你忘记了一个分号:

template <typename T>
class btree_iterator  {


};
 ^

You forgot a semicolon:

template <typename T>
class btree_iterator  {


};
 ^
雨后彩虹 2024-12-17 13:00:44

注释掉该行并放置一个非常简单的行,可能是 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 of btree_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.

冷默言语 2024-12-17 13:00:44

如果您在类中声明它,则应该模板化类本身并直接在其中定义它。

template<typename T>
class btree {

   //your overloaded function here without the beginning template declaration
};

同样,对于模板类,您不能将类严格分离为头文件(.h)和实现文件(.cpp),因为这样编译器将无法在运行时创建正确的对象,就好像它只能看到头文件一样。因此,在同一个“.h”文件中,写入声明和定义。

If you are declaring it within the class, you should templatize the class itself and directly define this within it.

template<typename T>
class btree {

   //your overloaded function here without the beginning template declaration
};

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文