如何将模板类作为函数参数传递而不出现 C7568 错误
这里是 C++ 新手。我很确定这个问题有一个简单而明显的解决方案,但即使在阅读了这里的数十个类似的问答之后,我还没有更接近它。但这是我的问题:
我有一个模板类:
template<class T>
struct KalmanSmoother
{
Eigen::MatrixX<T> P;
...
KalmanSmoother(int dynamParams, int measureParams, int controlParams = 0);
...
}
我可以毫无问题地使用它,就像这样:
KalmanSmoother<float> smoother(4, 2);
smoother.P = Eigen::Matrix4f {
{0.1f, 0.0f, 0.1f, 0.0f},
{0.0f, 0.1f, 0.0f, 0.1f},
{0.1f, 0.0f, 0.1f, 0.0f},
{0.0f, 0.1f, 0.0f, 0.1f}
};
...
像魅力一样工作。但是,当我想重构代码并将初始化部分提取到其他函数中时,编译器(MSVC 19.31.31104.0)开始哭泣。函数提取看起来像这样:
// Declaration in the header:
void setupKalmanSmoother(KalmanSmoother<float> & smoother);
// Definition in the .cpp
inline void Vehicle::setupKalmanSmoother(KalmanSmoother<float> & smoother)
{
smoother.P = Eigen::Matrix4f {
{0.1f, 0.0f, 0.1f, 0.0f},
{0.0f, 0.1f, 0.0f, 0.1f},
{0.1f, 0.0f, 0.1f, 0.0f},
{0.0f, 0.1f, 0.0f, 0.1f}
};
...
}
我只想这样称呼它:
KalmanSmoother<float> smoother(4, 2);
setupKalmanSmoother(smoother);
没什么神奇的。它应该可以工作(我想......),但我收到此编译器错误:
error C7568: argument list missing after assumed function template 'KalmanSmoother'
错误消息指向标头中的声明。值得一提的是,模板类的所有函数定义都在头文件中,因为当我出于习惯将定义放入 .cpp 文件时,我已经遇到了完全相同的错误。
那么我错过了什么?
提前致谢!!!
C++ newbie here. I'm pretty sure there's an easy and obvious solution to this problem, but even after reading through dozens of similar Q&As here, I haven't got closer to it. But here's my problem:
I have a template class:
template<class T>
struct KalmanSmoother
{
Eigen::MatrixX<T> P;
...
KalmanSmoother(int dynamParams, int measureParams, int controlParams = 0);
...
}
And I can use it without any problem, like this:
KalmanSmoother<float> smoother(4, 2);
smoother.P = Eigen::Matrix4f {
{0.1f, 0.0f, 0.1f, 0.0f},
{0.0f, 0.1f, 0.0f, 0.1f},
{0.1f, 0.0f, 0.1f, 0.0f},
{0.0f, 0.1f, 0.0f, 0.1f}
};
...
Works like charm. But when I want to refactor my code and I extract the initialization part into an other function, the compiler (MSVC 19.31.31104.0) starts crying. The function extraction looks like this:
// Declaration in the header:
void setupKalmanSmoother(KalmanSmoother<float> & smoother);
// Definition in the .cpp
inline void Vehicle::setupKalmanSmoother(KalmanSmoother<float> & smoother)
{
smoother.P = Eigen::Matrix4f {
{0.1f, 0.0f, 0.1f, 0.0f},
{0.0f, 0.1f, 0.0f, 0.1f},
{0.1f, 0.0f, 0.1f, 0.0f},
{0.0f, 0.1f, 0.0f, 0.1f}
};
...
}
And I'd just like to call it like this:
KalmanSmoother<float> smoother(4, 2);
setupKalmanSmoother(smoother);
Nothing magical. It should be working (I suppose...), but I get this compiler error:
error C7568: argument list missing after assumed function template 'KalmanSmoother'
The error message points to the declaration in the header. It's worth to mention that all function definitions of the template class are in the header file, since I have already run into - I think - exactly the same error when out of habit I put the definitions into the .cpp file.
So what am I missing?
Thanks in advance!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了一个非常类似的错误。就我而言,这是由于循环#includes造成的。
I got a very similar error. In my case it was due to circular #includes.
创建模板类的数据成员时,我遇到了同样的错误。
我的实例中的修复是我需要将范围限制在“List<>”的命名空间中它
没有说“List”未定义,而是给出了错误 7568。这是误导性的。我建议检查类似问题。
I ran into the same error when creating a datamember of a templated class.
The fix in my instance was I needed to scope into the namespace that the 'List<>' was in.
Instead of saying 'List' is undefined, it gave me error 7568. Which is misleading. I recommend checking for similar issues.
此错误代码也有类似的问题,编译器无法编译以创建堆栈。
解决方案是将包含内容移至“.H”文件中,而不是直接移至 CPP 文件中。 some_class 定义和堆栈变量的创建没有动。我花了一天时间才弄清楚这一点,所以我希望它对其他人有帮助!
Had a similar problem with this error code where the compiler couldn't compile to create a stack<some_class>.
The solution was to move the inclusion of the include into a ".H" file instead of directly of in the CPP file. The some_class definition and the creation of the stack variable didn't move. Took me a day to figure this out, so I hope it helps somebody else!