I can return two arrays by 'struct' with below codes; but can't translate the code to "class". the "class" code and error areattached also.
please shed lights on it. I have to use "class" and mutiple arrays in my project.
1) with "struct"
struct strA{
int *p;
int *p1;
};
strA setValue(int n)
{
strA strB;
strB.p=new int[n];
strB.p1=new int[n];
for (int i=0; i<n;i++)
{
strB.p[i]=i;
strB.p1[i]=i*2;
}
return strB;
}
int main(){
const int N=3;
strA strC;
strC=setValue (5);
for (int i=0; i<N;i++)
{
cout<< strC.p[i]<<endl;
cout<< strC.p1[i]<<endl;
}
return 0;
}
with "class". it turned out "error C3867: 'strA::setValue': function call missing argument list; use '&strA::setValue' to create a pointer to member"
class strA{ public: int *p; int *p1; public: strA(); ~strA(){delete p, delete p1;} strA setValue(int n); }; strA strA::setValue(int n) { strA strB; strB.p=new int[n]; strB.p1=new int[n]; for(int i=0; i<n;i++) { strB.p[i]=i; strB.p1[i]=i*2; } return strB; } int main(){ const int N=3; strA strC; strC.setValue (N); for (int i=0; i<N;i++) { cout<< strC.setValue<<endl; cout<< strC.p1[i]<<endl; } return 0; }
structandclassare just two different keywords used to declare a class. C++ has no structs and classes, but just classes. The only difference betweenstructandclassis their default acces (private forclassand public forstruct)setValueis a functionsetValuelooks odd (in both versions of the code) you create a temporary set its values and then return it, insteadsetValueshould set the values ofthisobject