Originally Posted by
nick4
My main problem is when i try to write or read from the ptable eg.
others = ptable[j].other;
gives error
subscript requires array or pointer type
left of '.other' must have class/struct/union type
That's because ptable is a member of a class. Outside of a class, such as in your main function, you access members of a class by typing the particular class instance's name followed by the dot (.) operator (this is provided the data member is declared as public and therefore directly accessible). I.e.:
Code:
int main()
{
table_of_pairs mytable(MAX);
float others = mytable.pairs[0].other;
}
Within a class, you of course don't need to do that.
Code:
int GetKey(int index,int& key) const
{
if( index < table_size )
{
key = ptable[index].key;
}
else return (-1);
}
int Getother(int index,float& other) const
{
if( index < table_size )
{
other = ptable[index].other;
}
else return (-1);
}
Those should not compile correctly, you should be getting some sort of "Not all control paths return a value" type of error. The "if" blocks need to return a value indicating success. This is why I suggested a function that returns a bool true/false value, it just seems to make more sense to me personally.
Judging from what it seems you are trying to do, I don't think you would have a need to write to these key/other values beyond what you have during the initial loading of values from the file. If you did however, you could just add another couple member functions to the class:
Code:
class table_of_pairs
{
...
public:
...
void SetKey(const int index,const int key)
{
if( index < table_size )
ptable[index].key = key;
}
void SetOther(const int index,const int other)
{
if( index < table_size )
ptable[index].other = other;
}
};
Code:
int table_of_pairs::add_to_table(ifstream)
{
int i;
float others;
ifstream fp_in("numbers.txt", ios::in); // declare and open file numbers.txt
while(others != 0)
{
for(i=0; i<MAX; i++)
{
cin >> others;
mytable.Getkey(i , i);
mytable.Getother(i , others);
}
}
fp_in.close(); // close the streams
}
The GetKey and Getother functions are meant to be used from outside the class, i.e. from the main function. This allows your main function to indirectly access/read the values stored in the classes private area. Within the add_to_table function itself, the code has no knowledge of a mytable variable, this represents an instance of a class in the main function and nothing viable within the add_to_table function. Since a classes member functions already have access to anything in the class (regardless of it being private/public) you don't have any need to call these functions here. Just directly access the ptable variable as in your original post. [edit]Also saw your read code doesn't read from the file in the above code, modified it in the code below.[/edit]
Code:
int table_of_pairs::add_to_table(ifstream)
{
int i;
float others;
ifstream fp_in("numbers.txt", ios::in); // declare and open file numbers.txt
for(i=0; i<MAX; i++)
{
fp_in >> others;
ptable[i].key = i;
ptable[i].other = others;
}
fp_in.close(); // close the streams
}