【问题标题】:c++ no matching function for call to: passing iterators?c++没有匹配的调用函数:传递迭代器?
【发布时间】:2012-10-26 14:16:44
【问题描述】:

我在我的 C++ 代码中遇到了一个奇怪的问题。 我已经定义了一个名为

的 TemplateClass
StateTemplate<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&lt;StateTemplate&lt;T&gt; &gt;::const_iterator(在 testFunction 参数中)和 std::vector&lt;double&gt;::const_iterator(从 main 传递给 testFunction)之间的类型不匹配。
  • 谢谢。我是 stackoverflow 的新手,我不知道如何正确发布。我已将代码编辑到我的第一篇文章中。但我非常感谢你的帮助:)

标签: c++ templates iterator


【解决方案1】:

如果您正确地提交了partialEntangledScalarProduct 的声明,则模板参数S 不会在函数参数列表中的任何位置使用。因此,编译器永远不会推断它,您必须明确提供它。

partialEntangledScalarProduct<SomeClass>(s1,s2,sSub1,sSub2,vui,vui);

SomeClass 只是一个示例,除非您提供更多详细信息,否则确切名称不清楚。

【讨论】:

  • 哇!那是错误!似乎是 typename 的问题。
【解决方案2】:

(我这台机器上没有g++,所以这是猜想。)

我认为问题在于编译器无法推断出 S 并且您收到了无用的错误消息。将您的用法更改为以下内容:

double test = partialEntangledScalarProduct<double>(s1,s2,sSub1,sSub2,vui,vui);

附带说明,请确保在发布示例之前为您编译。我假设Doub 应该是double

【讨论】:

  • 是的。也谢谢你! :) 我不知道该感谢谁。所以,谢谢大家。
猜你喜欢
  • 2017-07-17
  • 2013-12-16
  • 2011-05-16
  • 2023-04-02
  • 2021-09-21
  • 2016-11-23
  • 2012-05-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多