what is wrong with my array?

hi all

okay so i have a string array that i declared in a header file and I'm trying to write to it but I keep getting this error, "no match for 'operator[]' in 'items[i]'".

here's my code for the array
string items[] = {};

and here's my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void cache()
{
     system("cls"); // clear screen
     string line;
     
     ifstream items;
     items.open("items.txt"); // open file
     
     if(!items.is_open()) // if file failed to open, send user back to start
     {
                       cout << "Product list failed to initialize, teleporting you to the menu." << endl;
                       system("pause");
                       menu();
     }
     
     for(int i=0; !items.eof(); i++) // for loop to add each item to the 'items' array (defined in globalVars.h)
     {
             getline(items,line);
             items[i] = line; // add entry to array
     }
     
     items.close(); //close file
}


thanks for any help :)
Last edited on
This: string items[] = {}; creates an unresizable empty array. All arrays in C++ are unresizable.

This:
 
/*line 6*/ifstream items;
hides the global 'items'. All future uses of that identifier within this function will refer to the std::ifstream.
Last edited on
What is this all about???
items[i] = line; // add entry to array ???????????

at that point in the code the variable items is the name of the file.
What is the name of your array - is that a global variable called items ??
If so you - try ::items[i] = line; // add entry to array

You need to read up scope, visibility, namespaces
thanks, helios; it worked. The only problem now is that when it finishes, the program crashes and shows that stupid "send information to microsoft" message.

Any tips?

oh, and I added
1
2
     system("pause");
     menu();

to the end of it, but it still did that
okay i went into debugging mode and it tells me it's a segmentation fault.... whatever that is. I'm googleing now; has this happened to anyone before?
I solved it. thanks for your help everyone :) I had used the variable 'line' earlier in the script, so i changed the name it it works now.

ta x
Topic archived. No new replies allowed.