2017-12-14 4 views
-3

내가"형식을 지정하지 않았습니다". 그러나 그것은

BaseAbstrac 클래스 => Baseabstrac.h 파일을 가지고

Derived1 클래스 => Derived1.h 및 Derived1.cpp 파일

Derived2 클래스 => Derived2.h 및 Derived2.cpp

NestedClass 클래스 => NestedClass.h 및 NestedClass.cpp이

도 MAIN.CPP 파일 파일.

NestedClass.cpp를 제외하고 모든 cpp 파일을 컴파일 할 수 있습니다.

내 오류입니다 :

BaseAbstract.h :

NestedClass.cpp:13:1: error: ‘NestedClass’ does not name a type 
NestedClass& Derived1<T>::NestedClass::operator++() 

하지만 NestedClass.cpp

에 NestedClass.h을 포함하기 때문에 이름의 유형이 내 헤더 및 구현 파일입니다

#ifndef BASEABSTRAC_H 
#define BASEABSTRAC_H 
#include <iostream> 

using namespace std; 

    template <class T> 
    class BaseAbstract{ 
    public: 
     class NestedClass; 
     virtual int count (const T& val)=0; 
    }; 


    #endif 

Derived1.h

#ifndef DERIVED_H 
#define DERIVED_H 

#include "BaseAbstrac.h" 
#include <memory> 

using namespace std; 

template <class T> 
class Derived1:public BaseAbstract<T>{ 

protected: 
    shared_ptr<T>dataS; 
    int sizeS; 
    int capacity; 

public: 

    Derived1(); 

    class NestedClass; 

    int count (const T& val); 

}; 

#endif 

Derived1.cpp

#include "Derived1.h" 
using namespace std; 

template<class T> 
Derived1<T>::Derived1() 
{ 
    sizeS = 0; 
    capacity = 0; 
} 

NestedClass.h

#ifndef NESTEDCLASS_H 
#define NESTEDCLASS_H 

#include <memory> 
#include <string> 
#include "Derived1.h" 
using namespace std; 

template <class T> 

class Derived1<T>::NestedClass 
{ 
protected: 
     T* data; 
public: 
    NestedClass(); 

    T* getData(); 
    NestedClass& operator++(); 
}; 

#endif 

NestedClass.cpp

#include "NestedClass.h" 


using namespace std; 
template<class T> 
Derived1<T>::NestedClass::NestedClass() { data = new T; } 

template<class T> 
T* Derived1<T>::NestedClass::getData() { return data; } 

template<class T> 
NestedClass& Derived1<T>::NestedClass::operator++() 
{ 
    data++; 
    return data; 
} 

Derived2.h

#ifndef DERIVED2_H 
#define DERIVED2_H 
#include "Derived1.h" 

using namespace std; 
template <class K,class V> 
class Derived2:public Derived1<pair<K, V> >{ 
public: 
    Derived2(); 


}; 

#endif 
,451,515,

Derived2.cpp

template<class T> 
NestedClass& Derived1<T>::NestedClass::operator++() 
{ 
    data++; 
    return data; 
} 

문맥에서

#include "Derived2.h" 
    using namespace std; 

template <class K,class V> 
    Derived2<K,V>::Derived2():Derived1<pair<K, V> >() 
    { 

     this->capacity=10000; 
    } 

MAIN.CPP

#include <iostream> 
#include <memory> 
    using namespace std; 

    int main(void){ 

     Derived1<int> a; 

    } 
+4

을 (HTTP ://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) –

+0

':: NestedClass'은 유형이 아니며 ':: Derived :: NestedClass'는 형식이 아닙니다. 타입 (그러나 그것의 컴파일러를 납득시키기 위해'typename'이 필요할 수도 있습니다). 다음 문제는 템플릿 함수 (또는 클래스 템플릿의 멤버 함수)를 .cpp 파일에 넣을 수 없다는 것입니다 (명시 적 instatiation을 수행하지 않는 한) –

+0

@MartinBonner이 문제를 해결하려면 어떻게해야합니까? – badparam

답변

2

정의 된 글로벌 NestedClass 기호가 없다. 당신은 NestedClass따라 이름, as explained heretypename 때문에 필요

template<class T> 
typename Derived1<T>::NestedClass& Derived1<T>::NestedClass::operator++() 
{ 
    data++; 
    return data; 
} 

: 당신은 전체 범위를 사용합니다.


M.코멘트에서 M이 또한 뒤 반환 형식 사용하여 해결 될 수있다 : [? 템플릿에만 헤더 파일에서 구현 될 수있는 이유] 당신의 다음 피할 수없는 질문을하기 전에 읽어 보시기 바랍니다

template<class T> 
auto Derived1<T>::NestedClass::operator++() -> NestedClass& 
{ 
    data++; 
    return data; 
} 
+0

그러나 지금, 나는이 오류 메시지가 걸릴 : 필요 '유형 이름을' ' :: NestedClass Derived1'전에 'Derived1 이'종속 범위 Derived1 :: NestedClass 및 Derived1 :: NestedClass :: 연산자 ++() – badparam

+0

때문에 @ badparam 아아 네,'NestedClass'는 * 의존적 이름 *이라는 것을 잊었습니다. 업데이트 된 답변. –

+1

후행 반환 형식으로이 문제가 방지됩니다. –

관련 문제