2013-07-28 2 views
0

Я получаю «Неразрешенные внешние символы» для следующего кода:Нерешенные внешние символы для оператора соиЬ перегружать

template <int n> class Fibo 
{ 
private: 
    int pre, cur, next, tmp; 
public: 
    Fibo() : pre(1), cur(1), next(2), tmp(0) {} 
    int get() 
    { 
     if(n == 0 || n == 1) return 1; 
     for(int i = 2; i < n; ++i) 
     { 
      next = pre + cur; 
      tmp = cur; 
      cur = next; 
      pre = tmp; 
     } return pre + cur; 
    } 

    friend ostream& operator<< (ostream& out, Fibo<n>& f); 
}; 

template<int n> 
ostream& operator<< (ostream& out, Fibo<n>& f) 
{ 
    out << f.get(); 
    return out; 
} 

int main() 
{ 
    Fibo<5> a; 
    cout << a << endl; 
    cin.get(); 
} 

Я получаю эту ошибку компиляции, когда я пытаюсь напечатать a:

cout << a << endl; , Когда я печатаю «normaly», то есть cout << a.get() << endl, ошибок не было.

Я знаю, что ошибка Unresolved external symbols связана с объявленной функцией, которая не была реализована. это случай в моем коде? Я не могу найти его.

ответ

1

Есть несколько способов справиться с этим:

template <int n> class Fibo ; // Forward declaration 

// Definition, you could also forward declare here and define after the class 
template<int n> 
ostream& operator<< (ostream& out, Fibo<n>& f) 
{ 
    out << f.get(); 
    return out; 
} 
template <int n> class Fibo 
{ 
    // Don't forget <> to tell its a template 
    friend ostream& operator<< <> (ostream& out, Fibo<n>& f); 
}; 

выше способ многословен, но вы можете также определить friend в своем классе:

template <int n> class Fibo 
{ 
    // Note this is not a template 
    friend ostream& operator<< (ostream& out, Fibo& f) 
    { 
     out << f.get(); 
     return out; 
    } 

}; 

Определение в классе будет определяют функцию friend для каждого экземпляра, то есть Fibo<1>, Fibo<2> и т. д.

-1

сообщения GCC компилятора:

test.C:22:57: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, 
Fibo<n>&)’ declares a non-template function [-Wnon-template-friend] 
test.C:22:57: note: (if this is not what you intended, make sure the function 
template has already been declared and add <> after the function name here) 

довольно понятно, я думаю.

Смежные вопросы