how to define a member function of template class which return a nested class object

一曲冷凌霜 提交于 2019-12-24 04:59:47

问题


template<typename IPC_TYPE>
class Poller
{
private:

public:
    struct Event
    {
        std::shared_ptr<IPC> ipc;
        enum Status
        {
            NONE = 0, POLLIN = 1, POLLHUP = 2, MessageArrival = 3
        }status;
    };

    //block wait
    Event wait(size_t max_wait_time = 50);
};

 template<typename IPC_TYPE>
    Poller<IPC_TYPE>::Event Poller<IPC_TYPE>::wait(size_t max_wait_time = 50)
    {
        Event e;
        return Event();
    }

I define a class template Poller and also a nested class Event, I am writing a member function of Poller which return a Event object,but the compiler reports " Error C2061 syntax error: identifier 'Event' IPC poller.cpp 8 ", how should I do? thank you!


回答1:


Looking at your current code:

template<typename IPC_TYPE>
class Poller {    
public:
    struct Event {
        std::shared_ptr<IPC> ipc;
        enum Status
        {
            NONE = 0, POLLIN = 1, POLLHUP = 2, MessageArrival = 3
        } status;
    };

    //block wait
    Event wait(size_t max_wait_time = 50);
};

template<typename IPC_TYPE>
Poller<IPC_TYPE>::Event Poller<IPC_TYPE>::wait(size_t max_wait_time = 50) {
    Event e;
    return Event();
}

I noticed a few issues of concern:

  • 1) std::shared_ptr<IPC> ipc; I believe should be std::shared_ptr<IPC_TYPE> ipc;
  • 2) Already been answered by user:Hiroki -- typename needs to be used before Poller<IPC_TYPE>::Event to declare a typename so that the compiler knows how to recognize your intended use. Refer to his answer for a more detailed description and a fuller explanation of why you need typename.
  • 3) Since you are declaring the function outside of the super class's body, MSVS 2017 CE gives a compiler error about having a default value. (See Below).
  • 4) Not sure if you are creating a temporary... Then creating and return an instance by its constructor or if the template argument will be some kind of functor or function pointer that you are invoking.
  • 5) You have a std::shared_ptr<IPC_TYPE> member within Event but did not see any dynamic memory being created for type IPC_TYPE. So I added a user defined default constructor that set's this in order to see the object's constructors, destructors, operators, member functions, etc. being called, created and destroyed properly.

(3) - Compiler Error:

1>------ Build started: Project: StackQA, Configuration: Debug Win32 ------
1>main.cpp
1>c:\users\...\main.cpp(41): error C5037: 'Poller<IPC_TYPE>::wait': an out-of-line definition of a member of a class template cannot have default arguments
1>Done building project "StackQA.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

There are 2 ways of fixing the above compiler error:

  • A) Removing the default value that is in definition that is outside of the super class.
  • B) Writing the body of the function within the inner class. If you decide to choose this method of writing your function body; it will actually eliminate the whole need and purpose of your question in the first place since you would be defining it within the inner class.

Here is a working example of your class above:

#include <iostream>
#include <exception>
#include <memory>

// Classes A & B are just basic classes with ctor & dtor displaying a message
class A {
public:
    A() { std::cout << "A CTOR called\n"; }
    ~A() { std::cout << "A DTOR called\n"; }
};

class B {
public:
    B() { std::cout << "B CTOR called\n"; }
    ~B() { std::cout << "B DTOR called\n"; }
};

// Classes C & D are functors where their operator invokes a message to be displayed
class C {
public:
    void operator()() { std::cout << "Functor C called\n"; }
};

class D {
public:
    void operator()() { std::cout << "Functor D called\n"; }
};

template <typename IPC_TYPE>
class Poller {
public:
    struct Event {
        std::shared_ptr<IPC_TYPE> ipc; // Made correction here from IPC to IPC_TYPE
        enum Status {
            NONE = 0,
            POLLIN = 1,
            POLLHUP = 2,
            MESSAGE_ARRIVAL = 3, // Changed to All Caps... (personal preference)
        } status;

            // Added this constructor to actually make a shared_ptr of IPC_TYPE
        Event() {
            ipc = std::make_shared<IPC_TYPE>();
        }    
    };

    // Defined the function body within the inner class which also prevents your compiler error.
    Event wait( size_t max_wait_time = 50 ) {
        // Not sure of your intentions here, but for demonstration purposes
        // I've just commented out the temporary and just returned the ctor
        // Event e;
        return Event();
    }
};

// To define it outside of class remove the body from the inner class above,
// uncomment this section, and don't forget to use `typename`.
// Also make sure that your parameter does not have a default value here.
/*template<typename IPC_TYPE>
typename Poller<IPC_TYPE>::Event Poller<IPC_TYPE>::wait( size_t wait_time ) {
    // Not sure of your intentions here, but for demonstration purposes
    // I've just commented out the temporary and just returned the ctor
    //Event e;
    return Event();
}
*/    

int main() {
    try {
        Poller<A> p1;
        p1.wait( 10 );

        Poller<B> p2;
        p2.wait( 12 );

        Poller<C> p3;       
        Poller<C>::Event e1 = p3.wait( 7 );
        e1.ipc->operator()();

        Poller<D> p4;
        Poller<D>::Event e2 = p4.wait( 9 );
        e2.ipc->operator()();

    } catch( std::runtime_error& e ) {
        std::cerr << e.what() << std::endl;
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

-Output-

A CTOR called
A DTOR called
B CTOR called
B DTOR called
Functor C called
Functor D called



回答2:


The compiler doesn't know whether Poller<IPC_TYPE>::Event is a member variable of Poller<IPC_TYPE> or a nested type.

Thus we must type typename to remove this ambiguity as follows:

DEMO is here.

template<typename IPC_TYPE>
typename Poller<IPC_TYPE>::Event Poller<IPC_TYPE>::wait(size_t max_wait_time)
{
    Event e;
    return Event();
}


来源:https://stackoverflow.com/questions/53357136/how-to-define-a-member-function-of-template-class-which-return-a-nested-class-ob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!