Hi I am having a problem with the initialization of a 2D array of objects. The class is TermFrequency(Key,string,int,double);
That's how I initialize the dynamic 2D array of objects:
// TermFrequency tfreq [v_word.size()][totalNumberOfDocuments];
TermFrequency** tfreq = new TermFrequency*[v_word.size()];
for(size_t i = 0; i < v_word.size(); ++i)
tfreq[i] = new TermFrequency[totalNumberOfDocuments];
I understood why i am getting the error:
I just want to know how I can fix it?
Thank you.
Ok I added the DEFAULT Constructor TermFrequency and it worked: TermFrequency();
Now for example I can add new objects like, right?
Is that implementation considered right?
For(int i = 0; i < Length1; i++){
for(int j = 0; j < length2;j++){
tfreq[i][j] = TermFrequency(v_word[i],documents[j],j,wordCount);
}
}
And that's for the output:
for( size_t i = 0 ; i < v_word.size() ; i++ )
{
for(int j = 0; j < totalNumberOfDocuments;j++)
{
cout << tfreq[i][j].getTermFrequency() << endl;
}
}
Replace this
tfreq[i] = new TermFrequency[totalNumberOfDocuments];
with this
tfreq[i] = new TermFrequency(yourKey,totalNumberOfDocuments);
or simply
tfreq[i] = //create new TeamFrequencyObject by using class constructor
This line
TermFrequency tfreq[v_word.size()][totalNumberOfDocuments];
attempts to default-construct an array of TermFrequency
objects. In other words, it will call the default-constructor of all the elements in the array.
The problem is that your TermFrequency
class already has a user-defined constructor ( TermFrequency::TermFrequency(Key, std::string, int, double)
that overrides the compiler-generated default-constructor. You'll need to include it on your own:
class TermFrequency
{
public:
TermFrequency() { ... }
// ...
};
no matching function for call to 'TermFrequency::TermFrequency()
It looks like your class doesn't have a default constructor and then the line
tfreq[i] = new TermFrequency[totalNumberOfDocuments];
fails to compile.
Since you have overloaded your conctructor TermFrequency(Key,string,int,double);
you do not have a default one anymore. And when you call this line
tfreq[i] = new TermFrequency[totalNumberOfDocuments];
your program looks for a default constructor.
In order to avoid the error you have to rewrite your overlaoded TermFrequency
constructor as follows:
TermFrequency(Key = Key(),string = " ",int = 0,double = 0);
or Dynamic then you can do the following :
myclass **mc = new myclass*[5];
for (int i = 0; i < 5; ++i)
mc[i] = new myclass[6];
then you can for loop the 2d mc[i][x] = new myclass ( ); // if you have several constructors , if you have the default one then the previous first loop is enough without calling the constructor in each element , it will be called by default
what i meant is that if you have two constructors
myclass (){
this->variable = 0 //... etc
}
and another constructor
myclass (int var){
this->variable = var;
}
if you want all elements to be initialized by the second constructor , then you need to for loop each element like i said :D
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.