2015-12-28 2 views
-2

компилировать проект с помощью команды сделать, но это дает мне эти ошибки:Error «объявляет функцию нешаблонного» в C++

g++ -std=gnu++11 -c Array.cc  
In file included from Array.cc:5:0: 
Array.h:9:65: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, Array<Items>&)’ declares a non-template function [-Wnon-template-friend] 
friend ostream& operator << (ostream& fout, Array<Items>& ary); 
                  ^
Array.h:9:65: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) 
In file included from Array.cc:9:0: 
List.h:28:64: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, List<Items>&)’ declares a non-template function [-Wnon-template-friend] 
friend ostream& operator << (ostream& fout, List<Items>& lst); 
                  ^
Array.cc:112:5: error: specializing member ‘Array<int>::compare’ requires ‘template<>’ syntax 
int Array<int>::compare(Array<int>* ar2) 
^ 
Makefile:30: recipe for target 'Array.o' failed 
make: *** [Array.o] Error 1 

Код в Array.cc İŞ

#include "Array.h" 

код в Array.h: 9: 65 is:

class Array{ 
    friend ostream& operator << (ostream& fout, Array<Items>& ary); 
    private: 
    int theSz; 
    int totSz; 
    Items *theAry; 

Можете ли вы объяснить мне эти ошибки? Я использую Ubuntu 15.10. Может быть, это от устаревшей функции в C++, поскольку этот алгоритм был разработан в 2005 году

+3

Пожалуйста ** [править] ** Ваш вопрос с [mcve] или [SSCCE (Short, Self Contained, Correct Example)] (http://sscce.org) – NathanOliver

+1

Что такое строки Array.cc ~ 112? – AndyG

+0

содержит следующее: int Array :: сравнение (Array * ar2) –

ответ

2

Если Array класс не предназначен, чтобы быть шаблоном, ваша функция должна иметь подпись

friend ostream& operator << (ostream& fout, Array& ary); 

Тогда вы может зацикливаться на вашей ary.theAry внутри этой функции. Что-то вроде

ostream& operator << (ostream& fout, Array& ary) 
{ 
    for (int i = 0; i < ary.theSz; ++i) 
    { 
     fout << ary.theAry[i] << " "; 
    } 
} 

Как написано Array<Items>& объявляет ссылку на Items специализации Array класса, но ваш класс не шаблон, так что вы не можете передать аргумент шаблона.

Если Array класс должен быть шаблоном, вы должны объявить его как такой

template <class Items> 
class Array 
{ 
    friend ostream& operator << (ostream& fout, Array& ary); 
    private: 
    int theSz; 
    int totSz; 
    Items *theAry; 
}; 
+0

У меня сложилось впечатление, что класс действительно template, но шаблонная часть просто не была включена в OP (отсюда необходимость MCVE). – AndyG

+0

Я думаю, что 'Array' является шаблоном в конце концов, но OP не предоставил требуемую информацию. – Walter

+0

Может (и, следовательно, должен), вы не опускаете '' внутри определения 'Array ', так как там 'Array' автоматически ссылается на' Array '? – Walter

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