0

Hey I have a fairly simple question that some quick google searches couldnt solve so I'm coming here for some help.

I'm having trouble just getting my assignment off the ground because I can't even write the skeleton code!

Basically I have a header file like so:

namespace foo{
    class A {
    public:
        class B {
            B(); 
            int size();
            const int last();
        };
    };
}

And I want to know how to refer to these guys outside of the file in a implementation file.

BONUS:

namespace foo{

    template<typename T>
    typename
    class A {
    public:
        class B {
            B(); 
            int size();
            const int last();
        };
    };
}

how are these functions referred to as?

Is there a formula I can follow when it comes to this or is it more of flexible, different for your needs kinda thing?

Thanks for the help!

I'm using visual studios if that changes anything...

Tyler Kelly
  • 564
  • 5
  • 23
  • 1
    I don't understand your question. It would just be `foo::A::B::size()` for example. – Cory Kramer Sep 24 '15 at 21:08
  • 4
    How are what "guys" referred to in what context? Also see [templates must be implemented in header file](http://stackoverflow.com/q/495021/2069064) – Barry Sep 24 '15 at 21:10
  • are consturctor any different when it comes to templates? – Tyler Kelly Sep 24 '15 at 21:10
  • 1
    @user3470987 Are constructors any different... in what context? Like are you asking "what is the syntax for defining a constructor for a class template outside of the class definition?" – Barry Sep 24 '15 at 21:13
  • @Barry yeah, this is a part of a bigger school assignment, and we were told its `typename A::int A::B::size()` which continues to give me compile errors – Tyler Kelly Sep 24 '15 at 21:16
  • @user3470987: Remove that first `A::`. `A` doesn't have an `int` subtype. – Mooing Duck Sep 24 '15 at 21:17
  • I second Mooing Duck's point. I know it's hard to believe the professor/teacher would provide an invalid statement on that point, but there's no doubt, it's wrong. – JVene Sep 24 '15 at 21:24
  • @MooingDuck interesting, the TA also doesn't have the namespace part included anywhere in the header, emailing him now for clarification – Tyler Kelly Sep 24 '15 at 21:26

2 Answers2

5

Given:

namespace foo{
    class A {
    public:
        class B {
            B(); 
            int size();
            const int last();
        };
    };
}

The complete name for a function definition of size or last would be:

int foo::A::B::size() {...}
const int foo::A::B::last() {...}

Given:

namespace foo{

    template<typename T>
    typename
    class A {
    public:
        class B {
            B(); 
            B & operator ++();
            int size();
            const int last();

            template< typename I, typename R>
            R getsomethingfrom( const I & );
        };
    };
}

The function definitions would be:

template <typename T> int foo::A<T>::B::size() { ... }
template <typename T> const int foo::A<T>::B::last() { ... }

For these, getting the pointer to a member function would be:

auto p = &foo::A<T>::B::size;

Constructor definition would be:

template<typename T> foo::A<T>::B::B() {}

Making one of these things:

foo::A<T>::B nb{}; // note, with nb() it complains

The operator function definition returning a reference to a B, in the template, tricky:

template<typename T>         // standard opening template....
typename foo::A<T>::B &        // the return type...needs a typename 
foo::A<T>::B::operator++()     // the function declaration of operation ++
{ ... return *this; }        // must return *this or equivalent B&

In case you're curious, if a template function is inside B, like getsomethingfrom, then definition of the function is:

template< typename T>                       // class template
template< typename I, typename R>           // function template
R foo::A<T>::B::getsomethingfrom( const I & ) // returns an R, takes I
{ R r{}; return r }
JVene
  • 1,611
  • 10
  • 10
  • thanks this seems to be what I'm looking for, I have a case where I have something like `B& operator++();` in the header and I try implementing it like `typename B& A::B::operator++()` but i get an error saying its an unrecognizeable definition, would you know what the issue is there? – Tyler Kelly Sep 24 '15 at 21:32
0

To use the class in your implementation (.cpp) file you go likes that:

namespace foo{
    A::B::B(){
        // this is your inner class's constructor implementation
    }

    int A::B::size(){
        // implementation for you size()
        int res = last(); // access the method last() of the same object
        return res;
    }

    const int A::B::last(){
        // implementation of last()
        return 42;
    }

}

void main(){
    foo::A a; // construct an object of A
    // can't do anything useful as the methods of A::B are all private
}
YePhIcK
  • 5,816
  • 2
  • 27
  • 52