-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue with Exercise 13.21 #304
Comments
Sorry I did't get you. |
In exercise 13.21 the copy constructor of QueryResult should not be delete. We need the copy constructor in query function of TextQuery class. |
Exercise 13.21:link!
Reason: Need copy constructor here : Exercise 12.32link!
|
why ❓ |
@opensourceDA |
Paste detailed compiler info please. |
Using g++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4 ! |
OK. I'll look into it now. |
Let me know the cause for error whenever u find out please! Thnx |
Please paste the two files you tried to compile, including both the .h and the .cpp file. |
http://pastebin.com/g6p4uNV5 -- this is the class http://pastebin.com/vNCjGAVf -- driver |
Ok.I think I know what's going on. Basically, we need add one more line into it: QueryResult(QueryResult &&) noexcept = default; For such case, copy constructor works, just as @opensourceDA expected. I tested the code below with Vs 2013, Vs 2015 RC and G++ 4.82. All compiled. #ifndef MY_CLASS
#define MY_CLASS
#include<iostream>
#include<sstream>
#include<fstream>
#include<map>
#include<set>
#include<vector>
#include<string>
#include<memory>
class QueryResult;
std::ostream& print(std::ostream&, const QueryResult&);
class TextQuery {
public:
//typedef
using line_no = std::vector<std::string>::size_type;
//constructor
TextQuery(std::ifstream&);
//Query function
QueryResult query(const std::string&) const;
TextQuery(const TextQuery&) = delete;
TextQuery& operator=(const TextQuery&) = delete;
private:
//data members
std::shared_ptr<std::vector<std::string>> in_file;
std::map<std::string, std::shared_ptr<std::set<line_no>>> word_occr;
};
//TextQuery constructor
TextQuery::TextQuery(std::ifstream& input) : in_file(new std::vector<std::string>) {
for (std::string text; getline(input, text); in_file->push_back(text)) {
int cur_line_no = in_file->size() + 1;
std::string word;
for (std::istringstream line(text); line >> word; ) {
auto &line_nums = word_occr[word];
if (!line_nums)
line_nums.reset(new std::set<TextQuery::line_no>);
line_nums->insert(cur_line_no);
}
}
}
class QueryResult {
friend std::ostream& print(std::ostream&, const QueryResult&);
public:
//constructor
QueryResult(std::string s,
std::shared_ptr<std::vector<std::string>> f,
std::shared_ptr<std::set<TextQuery::line_no>> l)
: sought(s), file(f), lines(l) { }
QueryResult(const QueryResult&) = delete;
QueryResult& operator=(const QueryResult&) = delete;
QueryResult(QueryResult &&) noexcept = default; //!! here!!
//member functions
std::set<TextQuery::line_no>::iterator begin() { return lines->begin(); }
std::set<TextQuery::line_no>::iterator end() { return lines->end(); }
std::shared_ptr<std::vector<std::string>> get_file() { return file; }
private:
//data members
std::string sought; //word sought by this query
std::shared_ptr<std::vector<std::string>> file; //file we are searching in
std::shared_ptr<std::set<TextQuery::line_no>> lines; //lines sought is found on
};
QueryResult TextQuery::query(const std::string& s) const {
static std::shared_ptr<std::set<line_no>> nodata(new std::set<line_no>);
auto found = word_occr.find(s);
if (found == word_occr.end())
return QueryResult(s, in_file, nodata);
else
return QueryResult(s, in_file, found->second);
}
std::ostream& print(std::ostream& out, const QueryResult& qr) {
out << qr.sought << " : " << qr.lines->size() << std::endl;
return out;
}
#endif Please try it. |
Yeah, works. Then to the answer of Exercise 13.21, please add this detail. thanks |
Thx for reporting. |
In the answer to Exercise 13.21 it is suggested that the copy constructor of QueryResult class should be declared as deleted. But in the implementation of TextQuery::query function(Exercise 12.32) we use the copy constructor to return a QueryResult type. This results in error. So, the copy constructor for QueryResult class should be deleted.
The text was updated successfully, but these errors were encountered: