I have an STL map that maps a key to a vector of deliverables. I would like to search the map for a certain key and print out all of the deliverables. I am trying to iterate through the vector and call print on each item.
typedef std::vector<Deliverables> MyDeliverables;
typedef std::map<int, MyDeliverables> MyMap;
MyMap map1;
template < class T >
void printVector( const vector <T> &v)
{
for (auto p = v.begin(); p != v.end(); ++p)
*p->print();
}
int main()
{
Deliverables del("Name", 12, 12, 2018);
map1.insert(MyMap::value_type(1, MyDeliverables()));
auto search = map1.find(1);
if (search != map1.end()) {
std::cout << "Found Student ID: " << search->first << '\n';
printVector(search->second);
}
else {
std::cout << "Not found\n";
}
}
Error C2662 'void Deliverables::print(void)': cannot convert 'this' pointer from 'const Deliverables' to 'Deliverables &'
Line: *p->print();
How can I correctly print the deliverables?
The issue is with the code you are not showing:
Deliverables::print()
It is not const
, so you cannot use it. Declare the print function as const, and then you can use a const Deliverables&
:
Deliverables::print() const
Then change your loop to avoid confusion as to what to dereference and how many times:
for(const auto& p: v)
p.print();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.