2

Can I pass an array (contactsLonN ..) of a class that is within/or a subcomponent of an array of classes (chainref) to a function in 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);

Note: Related results usually employed simpler structures like ints, float, or struct, but not an array or double index array of a class within a class.

Note2: I have made extensive use of functions receiving "Vector" correctly, by reference or address; how about contactsLonN ..

dmerz75
  • 23
  • 4
  • the error message I'm trying to resolve: src/main.cpp:354:78: error: cannot convert ‘Contact (*)[700][20]’ to ‘Contact*’ for argument ‘3’ to ‘void build_latlon_contact_map(Chain*, int, Contact*)’ build_contact_map(chainref,chains_to_use,chainref[i].contactsLonN); – dmerz75 Jul 21 '15 at 00:35
  • I still don't understand how you are successfully implicitly converting `ChainNetwork*` to `Chain*` – AndyG Jul 21 '15 at 00:40
  • Sorry, that's the same thing. my failure to edit clearly, probably should have left intact – dmerz75 Jul 21 '15 at 00:44
  • I only saw one error in your codes: passing a `(*contact)[20]` to a `contact*` in function: `void build_contact_map(ChainNetwork *chain, int num_chains,Contact *map);` – Shindou Jul 21 '15 at 01:26
  • I guess you are confused with how to pass a `Type[x][y]` to a function which in your case is passing a `Contact[1000][20]` to `build_contact_map`, there are many ways to do this. [see this link, it may help you](http://stackoverflow.com/questions/31367771/passing-to-a-same-function-matrices-with-different-sizes-of-both-dimensions/31367837?noredirect=1#comment50716206_31367837) – Shindou Jul 21 '15 at 01:34

1 Answers1

0

Contact[1000][20] cannot be converted to Contact*; they are different types. You could change build_contact_contact_map() to accept Contact (*map)[20], or, better yet, use a std::vector<std::vector<Contact>> instead of raw arrays.

Miles Budnek
  • 28,216
  • 2
  • 35
  • 52
  • Thanks, this is working! I have a #define max_var 20, in that spot, but I probably will consider std::vector soon, it all started in C a little while back. – dmerz75 Jul 21 '15 at 01:30
  • If this answers you question, please click the little green check mark next to it to accept it as the correct answer. – Miles Budnek Jul 21 '15 at 01:41