【发布时间】:2012-10-26 14:16:44
【问题描述】:
我在我的 C++ 代码中遇到了一个奇怪的问题。 我已经定义了一个名为
的 TemplateClassStateTemplate<T>
另外我定义了
typedef StateTemplate<double> StateDouble.
然后我定义了一个朋友函数
template<class S>
friend S partialEntangledScalarProduct(
const typename std::vector<StateTemplate<T> >::const_iterator s1,
const typename std::vector<StateTemplate<T> >::const_iterator s2,
const typename std::vector<StateTemplate<T> >::const_iterator sSub1,
const typename std::vector<StateTemplate<T> >::const_iterator sSub2,
vector<unsigned int> const &pos,
vector<unsigned int> const &posNot);
如果我现在做
std::vector<StateDouble>::const_iterator s1;
std::vector<StateDouble>::const_iterator s2;
std::vector<StateDouble>::const_iterator sSub1;
std::vector<StateDouble>::const_iterator sSub2;
vector<unsigned int> vui;
Doub test=partialEntangledScalarProduct(s1,s2,sSub1,sSub2,vui,vui);
我收到以下错误:
no matching function for call to
'partialEntangledScalarProduct(
__gnu_cxx::__normal_iterator<
const StateTemplate<double>*,
std::vector<
StateTemplate<double>,
std::allocator<StateTemplate<double> > > >&,
__gnu_cxx::__normal_iterator<
const StateTemplate<double>*,
std::vector<
StateTemplate<double>,
std::allocator<StateTemplate<double> > > >&,
__gnu_cxx::__normal_iterator<
const StateTemplate<double>*,
std::vector<
StateTemplate<double>,
std::allocator<StateTemplate<double> > > >&,
__gnu_cxx::__normal_iterator<
const StateTemplate<double>*,
std::vector<
StateTemplate<double>,
std::allocator<StateTemplate<double> > > >&,
const std::vector<unsigned int, std::allocator<unsigned int> >&,
std::vector<unsigned int, std::allocator<unsigned int> >&)'
我已经尝试了几个小时来找到问题,但似乎我忽略了一些东西。 也许有人可以帮助我?
最好的问候 多米尼克
ps:如果您需要有关我的代码的更多信息,请告诉我。此刻我只想给你重要的部分。
这是一个有同样错误的小例子。
#include <iostream>
#include <vector>
template<class T>
class StateTemplate{
template<class S>
friend S testFunction(
const typename std::vector<StateTemplate<S> >::const_iterator s1);
public:
StateTemplate();
};
template<class T>
StateTemplate<T>::StateTemplate(){}
template<class T>
T testFunction(
const typename std::vector<StateTemplate<T> >::const_iterator s1)
{
return T(1);
}
int main(){
std::vector<double>::const_iterator s1;
double s=testFunction<double>(s1);
return 0;
}
【问题讨论】:
-
Minimal,独立的,可编译的例子请...如果我们需要水平滚动10个屏幕才能看到代码,那就错了。
-
友元函数定义中的
T是什么?是S吗?您能否提供一个完整(但最少)的示例来说明您不理解的行为? -
对不起。您可以在下面找到一个具有相同错误的简单示例。
-
@Dominik:我已将您的示例上传到 liveworkspace -- here。您的示例中的问题是
std::vector<StateTemplate<T> >::const_iterator(在 testFunction 参数中)和std::vector<double>::const_iterator(从main传递给 testFunction)之间的类型不匹配。 -
谢谢。我是 stackoverflow 的新手,我不知道如何正确发布。我已将代码编辑到我的第一篇文章中。但我非常感谢你的帮助:)