【问题标题】:C++ compiler can't chose overloading operator*C++ 编译器不能选择重载运算符*
【发布时间】:2013-04-09 08:25:17
【问题描述】:

我的问题在于操作员*。我有两个与 Wektor 类连接的 operator*。首先是所有类型名,属于 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() 中的 For 语句:

 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;诠释罗兹 = 2;策略=快速,2>; Wektor::args = std::basic_string * b'

还有更多:

注意:候选人是: 注意:模板 Wektor, Roz, Fast, Roz> > operator*(Wektor, Roz, Fast, Roz> >&, int&)

注意:'Wektor, 2, Fast, 2> >::args {aka std::basic_string}' 不是从 'Wektor, Roz, Fast, Roz>>'|

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

【问题讨论】:

  • 请不要用波兰语编写代码。此外,std::basic_string&lt;char&gt; 只是 std::string。也看看operator overloading FAQ
  • 并将operator+operator*的第一个参数作为const引用。

标签: c++ operator-overloading


【解决方案1】:

正如 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);
// ^^^^^

【讨论】:

  • 这解决了问题。主要问题是模板参数推导/替换失败:我有两个 operator* 第一个是朋友 Wektor operator* (Wektor & a, const int & b) 第二个是 template Wektor,Roz,Fast<:basic_string>,Roz> > 运算符 * (Wektor<:basic_string>,Roz,Fast > & a,int & b )。 const 添加剂量解决问题。我必须如何编写此运算符以避免扣除/替换错误?
  • 当我想要这个 Wektor<:basic_string>,Roz,Fast> 这个运算符 Wektor<:basic_string>,Roz,Fast<:basic_string>,Roz> > 运算符 * (const Wektor<:basic_string>,Roz,Fast > & a, int & b);
猜你喜欢
  • 1970-01-01
  • 2016-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
相关资源
最近更新 更多