简体   繁体   中英

C++ Template - In-class Struct

For the following code, I got a compilation error at implementation line as: "B does not define a type". I am aware of solution of put the function definition inside of class declaration. Is it possible, though, to have the function definition out of the template class declaration? Thanks

template<typename T>
class A {
    public:
        // ctor, dtor and interface funcs etc

   private:
     struct B {
          T value;
          B *next;
     }

     B *locate(const T& val) const;

     // blah blah
};

template<typename T>
B *A<T>::locate(const T& val) const
{
    //logic
}

Since B is defined inside A you should qualify it with A<T>:: :

template<typename T>
typename A<T>::B *A<T>::locate(const T& val) const
{
    //logic
}

Also note typename which is required because B is a dependent name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM