标准模板库,向量和语法错误

时间:2015-09-21 20:04:06

标签: c++ vector stl

这是我第一次尝试将STL合并到我的代码中,我只是有一些简单的语法问题,我似乎无法弄清楚。

所以我创建了一个指针向量:

   std::vector<event_tracking*> track[10]; 

并且在主程序的循环中我想调用

 track[nfd] = new event_tracking(pfd[nfd].fd, initial_requests);

但是这一行给出了错误

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*>]

以后我想调用我班级的函数:

char* p = track[j]->receive_datagram();

但是我收到了错误

error: base operand of '->' has non-pointer type 'std::vector<event_tracking*, std::allocator<event_tracking*> >'

然后我尝试将删除称为:

delete track[j];

并收到错误:

error: type 'class std::vector<event_tracking*, std::allocator<event_tracking*> >' argument given to 'delete', expected pointer

我真的很困惑因为我认为在涉及指针时调用类中的函数的方法是使用 - &gt;我不明白为什么在这里不起作用。我也不明白为什么没有正确使用[]来引用向量中的特定元素。如果有人可以请解释我的语法错误在哪里,以及为什么它们会产生,这将是伟大的!谢谢

1 个答案:

答案 0 :(得分:6)

std::vector<event_tracking*> track[10];

这不会创建10个指针的向量。它创建了一个由10个空指针向量组成的数组。要创建10个指针的单个向量,请使用括号而不是方括号。

std::vector<event_tracking*> track(10);