현재 가상 메서드와 친구가 포함 된 템플릿 클래스를 작성하려고합니다. 테스트 기능에서 호출하려고합니다. 내 코드는 다음과 같습니다가상 메서드가있는 템플릿 클래스
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)
당신은 문제가 정확히를 어디 있는지 감지 도와주세요 수 :
#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;
}
컴파일이 실패?
코드에 컴파일하는 데 많은 문제가 있습니다. –
두 개의 누락 된 typename 외에도 –