2014-01-19 3 views
-1

В настоящее время я пытаюсь написать класс шаблона, который включает в себя виртуальный метод и друга. Я хочу назвать их в своей тестовой функции. Мой код:шаблон класс с виртуальным методом

#include <iostream> 
#include <string> 
#include <cstdlib> 
#include <ctime> 
#include <vector> 
#include <iterator> 
#include <array> 
using namespace std; 
template <class T> class ProcessorBase; 
template <class T> ostream& operator<<(ostream &, const ProcessorBase<T> &); 
template<class T> 
class ProcessorBase 
{ 
protected: 
vector<T> v; 
public: 
ProcessorBase<T>& newElement(const T & t) 
{ 
    v.push_back(t); 
    return *this; 
} 
virtual T process()=0; 
friend ostream& operator<< <>(ostream & output, const ProcessorBase<T> & o); 
}; 

template<class T> 
ostream& operator<<(ostream & output, const ProcessorBase<T> & o) 
{ 
for (std::vector<T>::iterator it = o.v.end() ; it != o.v.begin() && it >10+o.v.begin(); --it) 
output<<*it<<endl; 
return output; 
} 

template<class T> 
class ProcessorSummer: public ProcessorBase<T> 
{ 
public: 
T process() 
{ 
    T sum=0; 
    for (std::vector<T>::iterator it = ProcessorBase<T>::v.begin() ; it != ProcessorBase<T>::v.end(); ++it) 
     sum=sum+ *it; 
    return sum; 
}   
}; 

template <class T> 
void test(T n = 200) 
{ 
ProcessorSummer<T> ps; 
for(T k=0;k<n ;++k) 
{ 
    T t= (k/static_cast<T>(2)); 
    ps.newElement(t); 
} 
cout<<ps.process()<<endl; 
cout<<ps.v<<endl; 
} 

int main() 
{ 
    test<int>(); 
    test<double>(); 
    test<float>(3); 
    system("PAUSE"); 
    return 0; 
} 

Компиляция завершается с:

error C2248: 'ProcessorBase<T>::v' : cannot access protected member declared in class 'ProcessorBase<T>' 

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::vector<_Ty>' (or there is no acceptable conversion) 

Можете ли вы помочь мне определить, где именно проблема?

+0

В вашем коде есть много проблем, из-за которых компиляция терпит неудачу. –

+0

Помимо двух отсутствующих имен файлов, которые он компилирует (и проблема с защищенным членом тоже исчезла) –

ответ

1

arr содержит указатели, поэтому вы не можете использовать ., вам необходимо ->.

+0

У меня есть еще одна ошибка: ошибка C2248: 'ProcessorBase :: v': не может получить доступ к защищенному члену, объявленному в классе 'ProcessorBase ' – user3140486

+0

ошибка C2679: двоичный '<<': оператор не найден, который принимает правый операнд типа 'std :: vector <_Ty>' (или нет приемлемого преобразования) – user3140486

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