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
“ ,我该怎么办?谢谢!
答案 0 :(得分:3)
编译器不知道Poller<IPC_TYPE>::Event
是Poller<IPC_TYPE>
的成员变量还是嵌套类型。
因此,我们必须输入typename
才能消除这种歧义,如下所示:
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(); }
我注意到了一些令人担忧的问题:
std::shared_ptr<IPC> ipc;
我相信应该是std::shared_ptr<IPC_TYPE> ipc;
user:Hiroki
回答-在typename
之前需要使用Poller<IPC_TYPE>::Event
来声明类型名,以便编译器知道如何识别您的预期用途。请参阅他的答案,以获取更详细的描述和为什么需要typename
的完整解释。MSVS 2017 CE
给出了有关具有默认值的编译器错误。 (请参见下文)。template argument
是某种functor
还是function pointer
您正在调用。 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 ==========
有两种方法可以修复上述编译器错误:
这是上面您的课程的一个有效示例:
#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