gpt4 book ai didi

c++ - 使用 C++ 编译函数模板时出错

转载 作者:太空狗 更新时间:2023-10-29 23:46:04 25 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Why can templates only be implemented in the header file?

我在 gcc 和 clang 中遇到了一些我不熟悉的错误。首先,这是有问题的代码。

WaypointTree.h:

class WaypointTree {

private:
Waypoint *head = nullptr;
Waypoint *current = nullptr;
template<class Function>
void each_recur(Waypoint *current, Function f);

public:
template<class Function>
void foreach(Function f);
void add(Waypoint *wp, Waypoint *parent = nullptr);
bool isLast(Waypoint *wp);
Waypoint *next();
Waypoint *getHead() { return head; }
void reverse() {}
};

路点树.cpp:

. . .

template<class Function>
void WaypointTree::foreach(Function f) {
each_recur(head, f);
}

. . .

羊群.cpp:

. . .

_waypoints->foreach([] (Waypoint *wp) {
glPushMatrix(); {
glTranslatef(wp->getX(), wp->getY(), wp->getZ());
glutSolidSphere(0.02, 5, 5);
} glPopMatrix();
});

. . .

使用 clang,我收到以下警告:

lib/WaypointTree.h:17:7: warning: function 'WaypointTree::foreach<<lambda at lib/Flock.cpp:128:22> >' has internal linkage but is not defined
void foreach(Function f);
^
lib/Flock.cpp:128:14: note: used here
_waypoints->foreach([] (Waypoint *wp) {
^

随后出现以下链接器错误:

lib/Flock.cpp:128: error: undefined reference to 'void WaypointTree::foreach<Flock::render(std::vector<Boid*, std::allocator<Boid*> >)::$_3>(Flock::render(std::vector<Boid*, std::all
ocator<Boid*> >)::$_3)'

使用 gcc 我只得到以下错误:

lib/WaypointTree.h:17:7: error: ‘void WaypointTree::foreach(Function) [with Function = Flock::render(std::vector<Boid*>)::<lambda(Waypoint*)>]’, declared using local type ‘Flock::ren
der(std::vector<Boid*>)::<lambda(Waypoint*)>’, is used but never defined [-fpermissive]

我不太确定从哪里开始调试它,但它似乎与模板和 lambda 相关。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com