2013-04-16 58 views
0

我的問題是運算符*。我有兩個運營商*與班級Wektor連接。首先是所有類型名稱,並且在類Wektor中。C++編譯器不能選擇超載運算符*

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)與T = std :: basic_string中找不到'operator *' int Roz = 2; Policy = Fast,2>; Wektor :: ARGS =標準:: basic_string的* B」

多:

注:候選人是: 注:模板Wektor,羅茲,快速,羅茲>>運算符*(Wektor,羅茲,快,羅茲>> &,整數&)

注: 'Wektor,2,快,2>> :: ARGS {又名性病:: basic_string的}' 未從「Wektor,羅茲,快速衍生Roz>>'|

在這方面的含義不是從派生的嗎?我嘗試使用專門的Wektor作爲特殊的操作符*,但這會造成同樣的錯誤。

+0

請不要用波蘭語寫代碼。另外,'std :: basic_string '只是'std :: string'。另外請看[運算符重載常見問題解答](http://stackoverflow.com/questions/4421706/operator-overloading)。 –

+1

並使'operator +'和'operator *'的第一個參數成爲** const **引用。 –

回答

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); 
// ^^^^^ 
+0

這劑量解決問題。主要問題是模板參數推理/替換失敗:我有兩個運算符*第一個是朋友Wektor運算符*(Wektor&a,const int&b)第二個是模板 Wektor < std :: basic_string ,Roz,Fast ,Roz>> operator *(Wektor ,Roz,Fast >&a,int&b)。 Const添加劑量解決問題。我如何寫這個運算符來避免扣除/替換錯誤? – Aku

+0

@Aku你想讓它解決哪個功能? –

+0

當我需要這個Wektor實例時,編譯器總是把這個朋友Wektor運算符*(Wektor&a,const int&b),Roz,Fast >這個運算符Wektor ,Roz ,Fast ,Roz>> operator * (const Wektor ,Roz,Fast >&a,int & b); – Aku