I'm having problems with line 61, but let me tell you what is supposed to be happening:
I have a structure called 'member', and have created an array of this datatype called 'network' (line 36).
I have then allocated two elements to the first item in the array (memb in line 45 and category in line 58).
I am then passing the array (network) and the required record number (i) to a function (displaysinglerecord) defined as part of the structure (line 18+) for it to be displayed.
Now, line 61.
I originally had this line without the 'member::' bit, but got the error message that this function "was not declared in this scope".
This is, in itself confusing. The function was declared as part of the structure 'member', which main() does can see.
But I thought a simple solution to this error was to tell the program where displaysinglerecord was declared - as part of the structure 'member' - so I added the "member::" bit to it. I now get the error message "cannot call member function 'void member::displaysinglerecord(member*,int) without object"
I have tried adding the size of the array to the parameters (although I am telling the function which record in the array is required), but I get the same results.
In truth, I don't really know what sort of "object" it is expecting, but (based on the fact that, when the code in the function is just put in main() instead of the function call, it produces the result I expect) I suspect it is something to do with the parameters and is probably a case of me missing something blindingly obvious.
Any pointers greatly appreciated.
Thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
using std::cout;
using std::endl;
struct member{
int memb;
int event;
int ud;
int uc;
char category;
int priority;
// put functions here
void displaysinglerecord(member network[], int r)
{
cout<<"member# : "<<network[r].memb<<endl;
//cout<<"event# : "<<network[r].event<<endl;
//cout<<"u/l distributor : "<<network[r].ud<<endl;
//cout<<"u/l customer : "<<network[r].uc<<endl;
cout<<"category : "<<network[r].category<<endl;
//cout<<"priority : "<<network[r].priority<<endl;
}
}; // end of struc member definition
int main()
{
int randVal(int);
int i, r;
int f=0,b=0,e=0,d=0,n=0,t=0;
char c;
member network[2];
srand(time(NULL));
// this is where get-struct-record begins
// for testing just use one value initially
for(i=0; i<1; i++)
{
// allocate the membership number
network[i].memb = i+1;
// get random value and allocate the category
r=randVal(16);
switch(r) {
case 0 : c='N'; break;
case 15 : c='D'; break;
case 14 : c='E'; break;
case 13 : c='E'; break;
case 12 : c='E'; break;
case 11 : c='E'; break;
default : c='B'; break;
}
network[i].category = c;
// now use display single record function to display it ...
member::displaysinglerecord(network, i);
} // end of for-loop creating struct record
// this is where get-struct-record ends
} // end main()
int randVal(int n)
{
int m=rand() %n;
return m;
}
| |