如何定义返回嵌套类对象的模板类的成员函数 [英] how to define a member function of template class which return a nested class object
本文介绍了如何定义返回嵌套类对象的模板类的成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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语法错误:标识符'Event'IPC poller.cpp 8
,如何我该怎么办?谢谢!
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!
推荐答案
查看您当前的代码:
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;
我相信应该是std :: shared_ptr< IPC_TYPE> ipc;
- 2)已经由
user:Hiroki
-回答在
来声明类型名,以便编译器知道如何识别您的预期用途。请参阅他的答案,以获取更详细的描述和为什么需要Poller< IPC_TYPE> :: Event
之前,需要使用typenametypename
的完整解释。 - 3)由于您声明了在超类主体之外的函数中,
MSVS 2017 CE
给出了有关具有默认值的编译器错误。 (请参见下文)。 - 4)不确定是否要创建临时文件...然后通过其构造函数创建并返回实例,或者
template参数
是您正在调用的functor
或函数指针
。 - 5)您在
Event $ c $中拥有一个
std :: shared_ptr< IPC_TYPE>
成员c>,但没有看到为IPC_TYPE
类型创建的任何动态内存。因此,我添加了一个用户定义的默认构造函数,该构造函数对此进行了设置,以便查看对象的构造函数,析构函数,运算符,成员函数等是否被正确调用,创建和销毁。
- 1)
std::shared_ptr<IPC> ipc;
I believe should bestd::shared_ptr<IPC_TYPE> ipc;
- 2) Already been answered by
user:Hiroki
--typename
needs to be used beforePoller<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 needtypename
. - 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 offunctor
orfunction pointer
that you are invoking. - 5) You have a
std::shared_ptr<IPC_TYPE>
member withinEvent
but did not see any dynamic memory being created for typeIPC_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)-编译器错误:
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)删除超类之外定义中的默认值。
- B)在内部类中编写函数的主体。如果决定选择这种编写函数体的方法;
下面是您上一堂课的工作示例:
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
这篇关于如何定义返回嵌套类对象的模板类的成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文