这是我的H档。
#include <vector>
#include "basequeue.h"
namespace advanced {
template<class T>
class AdvancedQueue {
public:
virtual ~AdvancedQueue();
void GetInfo(vector<T>* events);
void Push(const T& event);
bool Pop(T* event);
bool Wait(T* event);
private:
BaseQueue<T> queue_;
};
}
我在相应的cpp文件中实现了这些。当我使用它时,我有像这样的链接问题....
function advanced::Service::Handler(BaseHandler*): error: undefined reference to 'advanced::AdvancedQueue<advanced::Dashboard>::Pop(advanced::Dashboard*)'
有没有办法指定我将使用“Dashboard”类?我必须单独做吗?
答案 0 :(得分:0)
有没有办法指定我将使用&#34; Dashboard&#34;类? 我必须单独做吗?
使用模板,您只需隐式指定使用Dashboard,只需在源代码中使用它即可。
要满足该链接错误,请替换
void EnemySpeedy::playerTracking(float posX, float posY)
{
//Direction choosing, pos is a member of EnemySpeedy
float dirX = posX - pos.x;
float dirY = posY - pos.y;
//Angle choosing; angle, angularSpeed and angularSpeedMax are members of EnemySpeedy
float goalAngle = atan2(dirY, dirX);
float difAngle = normAngle(angle - goalAngle);
angularSpeed = limitValue(-difAngle,-angularSpeedMax,angularSpeedMax);
float dt = sfw::getDeltaTime();
angle = normAngle(angle + dt * angularSpeed);
// Update speed; acc, vel, etc. are members of EnemySpeedy class
// acc = speed; // it seems odd to me...
// vel = limitValue(vel + (acc - dragVel) * dt, 0.0, maxVel);
// what about:
acc = (difAngle > 1.5 || difAngle < -1.5) ? -maxAcc/2.0 : maxAcc*(maxVel - vel)/maxVel;
// brake if direction is wrong, go to limit velocity otherwise
acc = limitValue(acc, -maxAcc, maxAcc);
vel = limitValue(vel + acc * dt, 0.0, maxVel);
// Update position
pos.x += vel * cos(angle) * dt;
pos.y += vel * sin(angle) * dt;
}
与
bool Pop(T* event);
虽然可以为特定的模板化案例提供资源,但我不鼓励这样做。它似乎首先打败了拥有模板的价值,并且在使用时,但不是必需的,它表明开发人员对模板的使用没有清楚的了解。