Reputation: 49
Recently, I've begun studying C++, and this is my first C++ program - but it does not work.
Error message:
No matching constructor for initialization of "Document".
The IDE I use is Xcode.
class Document {
private:
int doc_id;
int lan_id;
std::vector<int>::size_type n_total;
std::vector<int> words;
std::vector<int> counts;
inline int doc_type(){return (counts.empty() && !words.empty())? 1:0;};
public:
Document(int docId)
{
doc_id = docId;
n_total = 0;
}
Document(int docId, int lanId)
{
lan_id = lanId;
doc_id = docId;
n_total = 0;
}
inline void add(int word, int count)
{
words.push_back(word);
counts.push_back(count);
}
inline void add(int word) { words.push_back(word);}
inline int word(int i) { return words[i]; }
inline int count() { return counts[1];};
inline std::vector<int>::size_type size() { return n_total;};
inline void setSize(std::vector<int>::size_type total){ n_total = total;};
inline std::vector<int>::size_type types() { return words.size();}
~ Document()
{
words.clear();
counts.clear();
}
};
Upvotes: 0
Views: 2612
Reputation: 23624
My guess is that you are trying to instantiate your Document
class by using default constructor, however, you do not have a default constructor defined in you code.
You only defined two overloaded versions of the constructor in your public section:
Document(int docId, int lanId)
and
Document(int docId)
If you instantiate your object as follows
Document d;
it will give you compile error since compiler cannot find a default constructor to finish the creation of the object. Meanwhile, you have defined your own constructors, so compiler will not generate a default constructor for you.
Besides, you have several places that put redundant semicolons there, which is error-prone and should not be there.
For example:
inline int doc_type(){return (counts.empty() && !words.empty())? 1:0;};
//^^^^
inline int count() { return counts[1];};
inline std::vector<int>::size_type size() { return n_total;};
inline void setSize(std::vector<int>::size_type total){ n_total = total;};
Upvotes: 3
Reputation: 254431
The problem is in the code you haven't shown us, where you try to create an object of this type. You need to use one of the defined constructors:
Document d1(docId);
Document d2(docId, lanId);
My wild guess is that you're trying to default-construct one:
Document d;
The class doesn't (and probably shouldn't) have a default constructor Document()
, so this won't work.
Upvotes: 2