2

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:

  • no matching function for call to 'TermFrequency::TermFrequency()'|
  • note: TermFrequency::TermFrequency(Key, std::string, int, double)|

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;
        }
    }
3
  • Compiler do not provide default constructor when you have overloaded it. Commented Jun 28, 2013 at 13:12
  • In response to your edit, yes, you can do that. Or you can use a std::vector instead to make things easier. Commented Jun 28, 2013 at 13:29
  • Yes one more Thing hmmm I tried tfreq[i][j]->getTermFrequency() instead of tfreq[i][j].getTermFrequency() and it gave me an error can u explain it why? Commented Jun 28, 2013 at 13:34

5 Answers 5

1

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() { ... }
    // ...
};
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

I added a default constructor @Nikola to initialize the array. what am i doing is right?
@HaniGoc yes, you can test it
1

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.

2 Comments

Ahh. Well i have this contructor TermFrequency(Key,string,int,double); Do i need a default constructor? Sorry if this question is silly
Yes, you do need a default constructor: one that accepts an empty list of arguments. Normally the compiler defines one for you but if you define another constructor (as you do) then the compiler will not generate the default constructor and you have to do it manually.
1

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);

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.