0

I would like to initalize a vector of pairs with some hard-coded values, I tried using different solutions but I keep getting compilation error. My code looks like this:

std::vector<std::pair<cv::HOGDescriptor, std::ifstream> > hogs_files = { std::make_pair(hog, file), std::make_pair(hog2, file2), std::make_pair(hog3, file3), std::make_pair(hog4, file4), std::make_pair(hog5, file5), std::make_pair(hog6, file6), std::make_pair(hog7, file7), std::make_pair(hog8, file8) }; 

and the error I've got is:

Error C2440 '<function-style-cast>': cannot convert from 'initializer list' to '_Mypair' 

Thank you for answers.

6
  • 3
    Please remove the '='. Commented Nov 7, 2016 at 9:36
  • It doesn't help Commented Nov 7, 2016 at 9:39
  • visual studio 2015 Commented Nov 7, 2016 at 9:40
  • 1
    If you post an minimal reproducible example I'll check that here. Commented Nov 7, 2016 at 9:45
  • 3
    Use {{ and }}, but std::ifstream is not copy-constructable and therefore can't be used in std::pair. Commented Nov 7, 2016 at 9:47

2 Answers 2

1

The error is because fstreams are not copy-constructible.

I would suggest you move your ifstreams to the vector of pairs; more clarity and control.

std::vector<std::pair<cv::HOGDescriptor, std::ifstream> > hogs_files = { std::make_pair(hog, std::move(file)), std::make_pair(hog2, std::move(file2)), std::make_pair(hog3, std::move(file3)), std::make_pair(hog4, std::move(file4)), std::make_pair(hog5, std::move(file5)), std::make_pair(hog6, std::move(file6)), std::make_pair(hog7, std::move(file7)), std::make_pair(hog8, std::move(file8)) }; 
Sign up to request clarification or add additional context in comments.

Comments

1

The general approach to initialize the vector of pairs is OK but the problem is that std::ifstream is not copy-constructible. Hence, you won't be able to use

std::vector<std::pair<cv::HOGDescriptor, std::ifstream> > hogs_files = { std::make_pair(hog, file), ... }; 

However, you should be able to use std::ifstream* in the pair:

std::vector<std::pair<cv::HOGDescriptor, std::ifstream*> > hogs_files = { std::make_pair(hog, &file), ... }; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.