Jump to content
  • Advertisement
hkBattousai

C++ Compiler errors for vector of pointers of compile-time defined types

Recommended Posts

Posted (edited)

I'm trying to implement my own tree container. I have written a "TreeContainer.h" template class and I am posting a piece of its content below.

I have defined a type named "ContainerType", because I am planning to test different other core STL container types later. I want the container type to be define by a "using" or "typedef" macro.

I am getting some compiler error messages which I am having a hard time to understand. Every error message is give in the code as comments.

Can you please help me understand what I am doing wrong in my code.

IDE: Visual Studio 15.7.3 (Language Standard: C++17. Other than that the default compiler options are used.)

#include <memory>
#include <vector>

template <class T>
class TreeContainer
{
    public:
        using pT = T *;
        using ContainerType = std::vector<pT>;
       
        // ...
       
        bool DeleteChild(T * const pChild);
        ContainerType & GetChildren1();
        ContainerType & GetChildren2();
       
        // ...
       
    private:
        ContainerType Children;
};

// ...

template <class T>
bool TreeContainer<T>::DeleteChild(T * const pChild)
{
    for (std::vector<T*>::iterator it=Children.begin(); it!=Children.end(); ++it)    // Error    C2760    syntax error: unexpected token 'identifier', expected ';'
    {
        if (pChild == (*it)[i])
        {
            delete *it;
            Children.erase(cit);
            return true;
        }
    }
    return false;
}

template <class T>
TreeContainer<T>::ContainerType & TreeContainer<T>::GetChildren1()    // ERROR: Error    C2061    syntax error: identifier 'ContainerType'
{    // ERROR1: Error    C2447    '{': missing function header (old-style formal list?),    ERROR2: Error    C2143    syntax error: missing ';' before '{'
    return Children;
}

// THIS WORKS
template <class T>
std::vector<T*> & TreeContainer<T>::GetChildren2()
{
    return Children;
}

// ...

TreeContainer.h

Edited by hkBattousai

Share this post


Link to post
Share on other sites
Advertisement

This type of error is almost always due to a missing typename.  In any context where the compiler isn't sure if something is a type or a variable, you need to write typename before the name of the type.

Try this:

for (typename std::vector<T*>::iterator it=Children.begin(); it!=Children.end(); ++it)
template <class T>
typename TreeContainer<T>::ContainerType & TreeContainer<T>::GetChildren1()

 

Share this post


Link to post
Share on other sites

You were right. Adding "typename" solved everything. Actually I knew this rule, but I couldn't guess that it was the cause of the errors at this time.

Many thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!