我可以将类数组中的类数组传递给C ++中的函数吗?

时间:2015-07-21 00:32:27

标签: c++ arrays class

我可以将类数组(chainref)内的类的数组(contactsLonN ..)传递给C ++中的函数吗?

// ChainNetwork.cpp
void build_contact_map(Chain *chain, int num_chains,Contact *map) {
    //accept 1 of contactsLonN, contactsLonS, contactsLatW, contactsLatE;    
}

// ChainNetwork.h
class Vector {
public:
    double x;
    double y;
    double z;
Vector (); // Constructor declared.
};
inline Vector::Vector() {
    x = 0.0;
    y = 0.0;
    z = 0.0;
}

class Contact {
public:
    int cresid;
    double distance;
    Contact (); // Constructor declared.
};
inline Contact::Contact() {
    cresid = -1;
    distance = 0.0;
}


class ChainNetwork {
public:
    struct Contact contactsLonN[1000][20];
    struct Contact contactsLonS[1000][20];
    struct Contact contactsLatW[1000][20];
    struct Contact contactsLatE[1000][20];
}


// declarations in ChainNetwork.h
void build_contact_map(ChainNetwork *chain, int num_chains,Contact *map);
double distance ( Vector v1, Vector v2 );



// main.cpp main()
ChainNetwork *chainref;
 try {
     chainref = new ChainNetwork [num_chains];
 } catch (std::bad_alloc xa) {
     std::cout << "Allocation Failure\n";
     return 1;
 }

// 1 generic function I would like to call .. but seems to grow uncontrollably if I try to use switch(s)
build_contact_map(chainref,chains_to_use,chainref[i].contactsLonN);
build_contact_map(chainref,chains_to_use,chainref[i].contactsLonS);
build_contact_map(chainref,chains_to_use,chainref[i].contactsLatW);
build_contact_map(chainref,chains_to_use,chainref[i].contactsLatE);

注意:相关结果通常使用更简单的结构,如整数,浮点数或结构,但不是类中的类的数组或双索引数组。

注2:我已广泛使用接收&#34; Vector&#34;正确地,通过引用或地址;联系方式怎么样..

1 个答案:

答案 0 :(得分:0)

Contact[1000][20]无法转换为Contact*;他们是不同的类型。您可以将build_contact_contact_map()更改为接受Contact (*map)[20],或者更好的是,使用std::vector<std::vector<Contact>>代替原始数组。