|
-
Dec 23rd, 2002, 07:37 AM
#1
Thread Starter
<?="Moderator"?>
by ref struct arrays?
Code:
struct AddressBook{
string Name;
string Email;
string Address;
string Town;
string Postcode;
string Phone;
};
void ReadBook(ifstream &fin, AddressBook &book,int total){
for(int x = 0;x<total;x++){
string strVal ="";
strVal = "<" + x;
strVal += ">";
string strTemp = "";
while(getline(fin,strTemp, '*')){
if(strTemp == strVal)
{
fin>>strTemp>>book[x].Name;
fin>>strTemp>>book[x].Email;
fin>>strTemp>>book[x].Address;
fin>>strTemp>>book[x].Town;
fin>>strTemp>>book[x].Postcode;
fin>>strTemp>>book[x].Phone;
}
};
}
}
int main(){
ifstream fin(ADDRESS_LOCATION);
int Record_ID = GetRecords(fin);
AddressBook *book;
book = new AddressBook[Record_ID];
ReadBook(fin,*book[Record_ID],Record_ID);
delete book;
return 0;
}
c:\C++\Addressbook\source1.cpp(43): error C2676: binary '[' : 'AddressBook' does not define this operator or a conversion to a type acceptable to the predefined operator
c:\C++\Addressbook\source1.cpp(43): error C2228: left of '.Name' must have class/struct/union type
... and the same for all the items in the struct
any ideas what im doing wrong.
Thanks
-
Dec 23rd, 2002, 08:18 AM
#2
ReadBook takes a reference to a single AddressBook variable. You pass a single variable, to be specific you pass the (n+1)th member of an array with size n (you pass the element after the last element).
Yet the code in ReadBook acts as if the passed reference was an array.
Changes are bold.
Code:
void ReadBook(ifstream &fin, AddressBook *book,int total){
for(int x = 0;x<total;x++){
string strVal;
strVal = "<" + x;
strVal += ">";
string strTemp;
while(getline(fin,strTemp, '*')){
if(strTemp == strVal)
{
fin>>strTemp>>book[x].Name;
fin>>strTemp>>book[x].Email;
fin>>strTemp>>book[x].Address;
fin>>strTemp>>book[x].Town;
fin>>strTemp>>book[x].Postcode;
fin>>strTemp>>book[x].Phone;
}
};
}
}
int main(){
ifstream fin(ADDRESS_LOCATION);
int Record_ID = GetRecords(fin);
AddressBook *book;
book = new AddressBook[Record_ID];
ReadBook(fin, book,Record_ID);
delete[] book;
return 0;
}
You don't need to assign strings an initial value of "". Doing so results in an unnecessary function call and wasted CPU time.
If you want a string that has an initial value that is not "" you don't write
string str = "Hello";
but rather
string str("Hello");
as this again saves you a function call.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 23rd, 2002, 08:25 AM
#3
Thread Starter
<?="Moderator"?>
cheers
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|