这是我第一次尝试将STL合并到我的代码中,我只是有一些简单的语法问题,我似乎无法弄清楚.
所以我创建了一个指针向量:
std::vector<event_tracking*> track[10];
Run Code Online (Sandbox Code Playgroud)
并且在我要调用的主程序的循环中
track[nfd] = new event_tracking(pfd[nfd].fd, initial_requests);
Run Code Online (Sandbox Code Playgroud)
但这一行给出了错误
error: no match for 'operator=' in '((Pds::MyXtcMonitorServer*)this)->Pds::MyXtcMonitorServer::track[nfd] = (((event_tracking*)operator new(12u)), (<anonymous>->event_tracking::event_tracking(((Pds::MyXtcMonitorServer*)this)->Pds::MyXtcMonitorServer::pfd[nfd].pollfd::fd, ((Pds::MyXtcMonitorServer*)this)->Pds::MyXtcMonitorServer::initial_requests), <anonymous>))'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/vector.tcc:133: note: candidates are: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = event_tracking*, _Alloc = std::allocator<event_tracking*>]
Run Code Online (Sandbox Code Playgroud)
后来我想调用我班级的功能:
char* p = track[j]->receive_datagram();
Run Code Online (Sandbox Code Playgroud)
但是我得到了错误
error: base operand of '->' has non-pointer type 'std::vector<event_tracking*, std::allocator<event_tracking*> >'
Run Code Online (Sandbox Code Playgroud)
然后我尝试调用删除像:
delete track[j];
Run Code Online (Sandbox Code Playgroud)
并得到错误:
error: type 'class std::vector<event_tracking*, std::allocator<event_tracking*> >' argument given to 'delete', expected pointer
Run Code Online (Sandbox Code Playgroud)
我真的很困惑,因为我认为在涉及指针时在类中调用函数的方法是使用 - >而我不明白为什么在这里不起作用.我也不明白为什么没有正确使用[]来引用向量中的特定元素.如果有人可以请解释我的语法错误在哪里,以及为什么它们会产生,这将是伟大的!谢谢
std::vector<event_tracking*> track[10];
Run Code Online (Sandbox Code Playgroud)
这不会创建10个指针的向量.它创建了一个由10个空指针向量组成的数组.要创建10个指针的单个向量,请使用括号而不是方括号.
std::vector<event_tracking*> track(10);
Run Code Online (Sandbox Code Playgroud)