C ++编译器无法选择重载运算符*

时间:2013-04-16 19:23:34

标签: c++ operator-overloading

我的问题在于运营商*。我有两个操作员*与类Wektor连接。首先是所有类型名称,并且在类Wektor中。

friend Wektor operator* (Wektor & a, const int & b)

和第二个仅用于字符串类型,并且在Wektor类之外。

template <typename T,int Roz, typename Policy >
Wektor<std::basic_string<char>,Roz,Fast<std::basic_string<char>,Roz> > operator * (Wektor<std::basic_string<char>,Roz,Fast<string,Roz> > & a,int & b)

我想要两个运算符*哪一个专门用于字符串并且具有不同的行为。 和整个代码如下:

//Policy for Wektor
template<typename T,int Roz>
class Safe
{
public:
void static zeroo(T t[]){
            for(int i=0;i<Roz;++i)
            {
                t[i]=0;
            }

}
static bool isOut(int i){
    return Roz<i;
}
};

template<typename T,int Roz>
class Fast
{
public:
void static zeroo(T t[]){
}
static bool isout(int i){
    return false;
}
};


template<typename T,int Roz, typename Policy = Safe<T,Roz> >
class Wektor;

template <typename T,int Roz, typename Policy = Safe<T,Roz> >
Wektor<T,Roz,Policy> operator + (const Wektor<T,Roz,Policy> & a, const Wektor<T,Roz,Policy> & b);

template <typename T,int Roz, typename Policy >
Wektor<std::basic_string<char>,Roz,Fast<std::basic_string<char>,Roz> > operator * (Wektor<std::basic_string<char>,Roz,Fast<string,Roz> > & a, int & b);


template<typename T,int Roz, typename Policy >
class Wektor{
public:

typedef typename typy<T>::result args;
Wektor()
{
    Policy::zeroo(tab);
}
T tab[Roz];
args get(int i)
{
    if (Policy::isout(i)) return 0;
    return tab[i];
}
void set(args val,int i)
{
    if (Policy::isout(i))return;
    tab[i]=val;
}
//This operator works fine
friend Wektor operator* (Wektor & a, const int & b){
      Wektor<T,Roz,Policy> w;

      for(int i=0;i<Roz;++i)
      {
          w.set(a.get(i)*b,i);
      }
      return w;
 }

 friend Wektor operator + <> (const Wektor & a, const Wektor & b);
 };

 template<typename T, int Roz>
 Wektor<T,Roz> operator + (Wektor<T,Roz> & a,Wektor<T,Roz> & b)
 {
 Wektor<T,Roz> wynik;
 for(int i=0;i<Roz;++i)
 {
    wynik.set(a.get(i)+b.get(i),i);
 }
 return wynik;
 }
 //This operator dosent work
 template <typename T,int Roz, typename Policy >
 Wektor<std::basic_string<char>,Roz,Fast<std::basic_string<char>,Roz> > operator *  (Wektor<std::basic_string<char>,Roz,Fast<string,Roz> > & a,int & b)
{
Wektor<string,Roz,Fast<string,Roz> > wynik;
string tmp;
for(int i=0;i<Roz;++i)
{
    for(int j;j<b;++j)tmp.append("asa");
    wynik.set(tmp,i);
    tmp.clear();
}
}

对于main()中的语句:

 Wektor<string,2,Fast<string,2> > str;
 str*3

我在这一行收到错误:

w.set(a.get(i)*b,i);

在朋友经营者*在Wektor课程中完整。

编译器说:模板参数扣除/替换失败。 其余编译器注释:

错误:'Wektor :: get(int)中的'operator *'与T = std :: basic_string不匹配; int Roz = 2; Policy = Fast,2&gt ;; Wektor :: args = std :: basic_string * b'

还有更多:

注意:候选人是: 注意:模板Wektor,Roz,Fast,Roz&gt; &GT;运营商*(Wektor,Roz,Fast,Roz&gt;&gt;&amp;,int&amp;)

注意:'Wektor,2,Fast,2&gt; &gt; :: args {aka std :: basic_string}'不是来自'Wektor,Roz,Fast,Roz&gt; &GT;'|

在这种情况下的含义是不是来自?我尝试使用类Wektor将专门的operator *作为firend,但这会产生同样的错误。

1 个答案:

答案 0 :(得分:2)

正如Daniel Frey在他的评论中似乎已经注意到,operator *的第一个参数是临时

w.set(a.get(i)*b,i);
//    ^^^^^^^^ returns a temporary

C ++ will not bind a non-const reference to a temporary

template <typename T,int Roz, typename Policy >
Wektor<std::basic_string<char>,Roz,Fast<std::basic_string<char>,Roz> > operator * 
  (Wektor<std::basic_string<char>,Roz,Fast<string,Roz> > & a, int & b);

将其更改为const

template <typename T,int Roz, typename Policy >
Wektor<std::basic_string<char>,Roz,Fast<std::basic_string<char>,Roz> > operator * 
  (const Wektor<std::basic_string<char>,Roz,Fast<string,Roz> > & a, int & b);
// ^^^^^