class test {
int age;
string name;
public:
test () {
}
test (int i, string n) : age (i), name (n) {}
int age_caller () {
return age;
}
};
void testing_function (test &t) {
cout << t.age_caller() << endl;
}
int main () [
test t [5], *pt;
//blablabla...
testing_function (t); //error in this calling...
}
i don't know what's wrong. in fact, i think, there's nothing wrong with the syntax... and how can i initialize pt with t's arrays?
testing_function expects (a reference to) an object of type test. t is not an object of type test.
You can set pt to point at the array t with
pt = t; which is a synonym for pt=&t[0] although you still couldn't just pass pt, as pt is a pointer to an object of type test, and testing_function doesn't accept pointers to objects of type test. You would have to dereference pt on the way in;
testing_function (*pt);
There's no reason not to just use the t[i] notation;
but how to pass the "whole ts" (i mean with the whole arrays)? so we can access it within the function. and to assign the pt with the whole t arrays... use new? how?
#include <iostream>
usingnamespace std;
class test {
int age;
string name;
public:
test () {
}
test (int i, string n) : age (i), name (n) {}
int age_caller () {
return age;
}
};
void testing_function (test &t) {
cout << t.age_caller() << endl;
}
int main () {
test t [5], *pt;
pt = new test [5];
return 0;
}
i didn't try to test it further more but i guess it will work, maybe using pt + 2->age; and so forth... (but as i remembered, i've tried this way too. my bad... -_-)