0

I tried in my own code to create definition of something, but the compiler couldn't recognize it, so just simply the code, I made a more short-abstract code that will represent what I'm trying to do:

template <class T>
class A
{
public:
    A() {}
    ~A() {}

    class B
    {
        T x;
    public:
        B() : x(0) {}
        ~B(){}

        void printAB() const;
        B& increase();
    };
};

Now, I want to make a definiton of printAB and increase outside the class, so printAB doesn't make any problem because it returns void, but when I'm trying to make definiton of PrintAB the compiler doesn't recognize it and make red underline under it:

This one works good:

template <class T>
void A<T>::B::printAB() const
{
    cout << "Hello World" << endl;
}

And this one it doesn't recognize:

template<class T>
A::B & A<T>::B::increase()
{
    x++;
    return *this;
}

I also tried to make this declaration:

template<class T>
A<T>::B & A<T>::B::increase() {
...
}

I must say that it doesn't work with A as you guys suggested, so please any other solution that may help?

Mor Tsimo
  • 3
  • 2
  • 1
    `template A::B & A::B::increase()` – SergeyA Sep 11 '18 at 18:55
  • @SergeyA, I tried this one, and it doesn't seem to show underline error, but when I'm trying to compile, it says: "synax error" on that line. – Mor Tsimo Sep 11 '18 at 18:57
  • Voting to close as a typo. Just like you need `A` in `A::B::increase()` you also need it in `A::B`. `A::B` should be `A::B` – NathanOliver Sep 11 '18 at 18:57
  • @NathanOliver as I already said, it doesn't work with `A::B` also – Mor Tsimo Sep 11 '18 at 18:58
  • Ah, yep, forgot you need `typename` first: https://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords. `A::B & A::B::increase()` -> `typename A::B & A::B::increase()` – NathanOliver Sep 11 '18 at 19:02

1 Answers1

1

We should to tell compiler what we meant by A<T>::B

template<class T> 
typename A<T>::B & A<T>::B::increase() 
{
    x++;
    return *this;
}
Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42