제 문제는 연산자 *입니다. 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();
}
}
() :이 줄에서 오류가 발생
Wektor<string,2,Fast<string,2> > str;
str*3
은 : 친구 연산자에서
w.set(a.get(i)*b,i);
* 느릅 나무의 전체입니다 Wektor 클래스입니다.
컴파일러에서 말하는 템플릿 인수 차감/대체에 실패했습니다. 컴파일러 노트의 나머지 :
오류 : Wektor :: 취득 (INT)와 T = 표준 : : basic_string '에서'운영자 * '아무 짝; int Roz = 2; Policy = Fast, 2>; Wektor :: 인수 = 표준 : : basic_string *의 B '
그리고 더 :
참고 : 후보는 다음과 같습니다 참고 : 템플릿 Wektor, 로즈, 빠른, 로즈>> 운영자 * (Wektor, 로즈, 빨리가, 로즈>> &는) &을 int로
참고 : '빠른 Wektor, 2, 2>> :: 인수 {일명 표준 : : basic_string}'빠른 'Wektor, 로즈에서 파생되지 않으며, Roz> ''|
그리고이 문맥에서 의미하는 것은 은에서 파생되지 않았습니까? 나는 Wektor 클래스로 firend와 같은 특수 연산자를 만들려고 노력하지만 이것은 동일한 오류를 만든다.
폴란드어로 코드를 작성하지 마십시오. 또한,'std :: basic_string'은 단지'std :: string'입니다. 또한 [연산자 과부하 FAQ] (http://stackoverflow.com/questions/4421706/operator-overloading)를 살펴보십시오. –
그리고'operator +'와'operator *'의 첫번째 매개 변수를 ** const ** 참조로 만드십시오. –