-4

I have

BaseAbstrac class => Baseabstrac.h file

Derived1 class => Derived1.h and Derived1.cpp files

Derived2 class => Derived2.h and Derived2.cpp file

NestedClass class => NestedClass.h and NestedClass.cpp file

and also main.cpp.

Except NestedClass.cpp , all cpp files can compile.

This is my error:

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

But It is name type because I include NestedClass.h into NestedClass.cpp

This is my header and implementation files:

BaseAbstract.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

Derived2.cpp

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

    }
badparam
  • 1
  • 3
  • 4
    Before you ask your next inevitable question, please read [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Some programmer dude Dec 14 '17 at 08:31
  • `::NestedClass` is not a type, `::Derived::NestedClass` is a type (but you may need `typename` to convince the compiler of that). Your next problem is that you can't put template functions (or member functions of a class template) in a .cpp file (unless you are doing explicit instatiation) – Martin Bonner supports Monica Dec 14 '17 at 08:31
  • @MartinBonner What should I do for this problem?Can you separate? – badparam Dec 14 '17 at 08:41

1 Answers1

2

In the context of

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

there is no global NestedClass symbol defined. You have to use the full scope:

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

You need the typename because NestedClass is a dependent name, as explained here.


As mentioned by M.M in a comment, this could also be solved using trailing return types:

template<class T>
auto Derived1<T>::NestedClass::operator++() -> NestedClass& 
{
    data++;
    return data;
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621