A sample question on c++ primer: Add member named get_file that returns a shared_ptr to the file in the QueryResult object.
class QueryResult
{
friend std::ostream& print(std::ostream&, const QueryResult&);
public:
using line_no = std::vector<std::string>::size_type;
QueryResult(std::string s, std::shared_ptr<std::set<line_no>> p, std::shared_ptr<std::vector<std::string>> f) :sought(s), lines(p), file(f) {}
std::set<line_no>::iterator begin(std::string) const { return lines->begin(); };
std::set<line_no>::iterator end(std::string) const { return lines->end(); };
std::shared_ptr<std::vector<std::string>> get_file() const { return std::make_shared<std::vector<std::string>>(file); };
private:
std::string sought;
std::shared_ptr < std::set<line_no>> lines;
std::shared_ptr<std::vector<std::string>> file;
};
compile error:
error C2665: std::vectorstd::string,std::allocator<std::string>::vector: no overloaded function could convert all the argument types.
As you can see in the
std::make_shareddocumentation, in order to create astd::shared_ptr<T>, you need to pass the arguments forT's constructor.However in this line:
You pass
filewhich is not a proper argument for constructing astd::vector<std::string>(yourT).But since
fileis already astd::shared_ptr<std::vector<std::string>>, you can simply return it instead (no need formake_shared):