Results 1 to 3 of 3

Thread: by ref struct arrays?

  1. #1

    Thread Starter
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    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

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  3. #3

    Thread Starter
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099
    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
  •  



Click Here to Expand Forum to Full Width