简体   繁体   English

初始化2D对象数组

[英]Initialize 2D array of objects

Hi I am having a problem with the initialization of a 2D array of objects. 嗨,我在初始化2D对象数组时遇到问题。 The class is TermFrequency(Key,string,int,double); 该类是TermFrequency(Key,string,int,double); That's how I initialize the dynamic 2D array of objects: 这就是我初始化动态2D对象数组的方式:

// 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()'| 没有匹配的函数可用于调用'TermFrequency :: TermFrequency()'|
  • note: TermFrequency::TermFrequency(Key, std::string, int, double)| 注意: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(); 好的,我添加了DEFAULT构造函数TermFrequency,它可以正常工作: 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. 尝试默认构造TermFrequency对象数组。 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: 问题是您的TermFrequency类已经有一个用户定义的构造函数( TermFrequency::TermFrequency(Key, std::string, int, double) ,它会覆盖编译器生成的默认构造函数。您需要将其包括在拥有:

class TermFrequency
{
    public:
         TermFrequency() { ... }
    // ...
};

no matching function for call to 'TermFrequency::TermFrequency() 没有匹配的函数来调用'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); 由于您已经超载了构造器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构造函数:

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 ( ); 那么您可以for循环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 如果您希望所有元素都由第二个构造函数初始化,则需要for循环每个元素,就像我说的:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM