如何定义返回嵌套类对象的模板类的成员函数

时间:2018-11-18 01:32:03

标签: c++ c++11 templates visual-c++ inner-classes

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();
    }

我定义了一个类模板Poller和一个嵌套类Event,我正在编写Poller的成员函数,该函数返回一个Event对象,但是编译器会报告” 错误C2061语法错误:标识符'事件'IPC poller.cpp 8 “ ,我该怎么办?谢谢!

2 个答案:

答案 0 :(得分:3)

编译器不知道Poller<IPC_TYPE>::EventPoller<IPC_TYPE>的成员变量还是嵌套类型。

因此,我们必须输入typename才能消除这种歧义,如下所示:

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();
}

答案 1 :(得分:2)

查看您当前的代码:

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();
}

我注意到了一些令人担忧的问题:

  • 1)std::shared_ptr<IPC> ipc;我相信应该是std::shared_ptr<IPC_TYPE> ipc;
  • 2)已经被user:Hiroki回答-在typename之前需要使用Poller<IPC_TYPE>::Event来声明类型名,以便编译器知道如何识别您的预期用途。请参阅他的答案,以获取更详细的描述和为什么需要typename的完整解释。
  • 3)由于您在超类的主体之外声明函数,因此MSVS 2017 CE给出了有关具有默认值的编译器错误。 (请参见下文)。
  • 4)不知道是否要创建临时实例...然后通过其构造函数创建并返回实例,或者template argument是某种functor还是function pointer您正在调用。
  • 5)您在std::shared_ptr<IPC_TYPE>中有一个Event成员,但是没有看到为类型IPC_TYPE创建的任何动态内存。因此,我添加了一个用户定义的默认构造函数,该构造函数对此进行了设置,以便查看对象的构造函数,析构函数,运算符,成员函数等是否被正确调用,创建和销毁。

(3)-编译器错误:

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 ==========

有两种方法可以修复上述编译器错误:

  • A)删除定义在父类之外的默认值。
  • B)在内部类中编写函数的主体。如果您决定选择这种编写函数体的方法;实际上,它将首先消除您的问题的全部需求和目的,因为您将在内部类中对其进行定义。

这是上面您的课程的一个有效示例:

#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;
}

  

-输出-

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