Mustafa
Mustafa

Reputation: 263

c++ declaring stl vector using type of befriending class as template argument

I have the following piece of code:

template <typename T>
class A {
    friend class B;
    struct model_struct {
       [...]
    }
}

template <typename T>
class B {
    func {
        typename vector<A<T>::model_struct > myVec;
    }
}

the vector declaration is giving me the following compile error:

error: type/value mismatch at argument 1 in template parameter list for ‘template class std::vector’

error: expected a type, got ‘ObjectExtraction::model_struct’

any ideas?

Upvotes: 0

Views: 221

Answers (2)

jogojapan
jogojapan

Reputation: 70027

Three things appear to be missing:

  • Semicolons at the end of class definitions
  • The template argument in the friend declaration
  • The typename specifier in the vector declaration

The latter is the problem that gave rise to the error.

I am unsure what the func {...} in the definition of B does; you may want to consider removing it.

Finally, you'll need a forward-declaration of the B template, so you can actually use it for the friend declaration in A.

Here is an attempt at correcting the code:

template <typename T>
class B; // Forward-declaration

template <typename T>
class A {
    friend class B<T>;  // template argument added
    struct model_struct {
       /*...*/
    };  // added semicolon
}; // semicolon added

template <typename T>
class B {
   /* Removed 'func'. */
   typename vector<typename A<T>::model_struct > myVec; // 'typename' added
};

Upvotes: 1

bbg
bbg

Reputation: 321

You should modify like this:

template <typename T>
class A {
    friend class B;
    struct model_struct {
       [...]
    }
}

template <typename T>
class B {
    func {
        vector<typename A<T>::model_struct > myVec;
    }
}

Upvotes: 1

Related Questions