2014-01-19 2 views
-1

현재 가상 메서드와 친구가 포함 된 템플릿 클래스를 작성하려고합니다. 테스트 기능에서 호출하려고합니다. 내 코드는 다음과 같습니다가상 메서드가있는 템플릿 클래스

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; 
} 

컴파일이 실패?

+0

코드에 컴파일하는 데 많은 문제가 있습니다. –

+0

두 개의 누락 된 typename 외에도 –

답변

1

arr에는 포인터가 포함되어 있으므로 .을 사용할 수 없으므로 ->이 필요합니다.

+0

오류가 발생했습니다. 오류 'C2248 :'ProcessorBase :: v ':'ProcessorBase '에 선언 된 보호 된 멤버에 액세스 할 수 없습니다. – user3140486

+0

오류 C2679 : 이진 '<<': 'std :: vector <_Ty>'유형의 오른쪽 피연산자를 사용하는 연산자가 없습니다 (또는 허용되는 변환이 없음) – user3140486

관련 문제