View Full Version : c++ help array's and files

11-30-2011, 05:19 AM
I am trying to write a program in c++. I have a text file and I am reading the information in...then I am trying to store the information into and object array. Is it possible to do this without using a pointer and a vector?

I found the following code while searching

object myobj[2]; //Define an array of objects from a class

//string array[5]; //Array to save the line on
int loop=0; //Index of array
int loop2;
string line; //The line taken from the *.txt source
ifstream myfile ("example.txt"); //To read from the *.txt File
if (myfile.is_open()) //Checking if the file can be opened
while (! myfile.eof() ) //Runs while the file is NOT at the end
getline (myfile,line); //Gets a single line from example.txt
myobj[loop]=line; //Saves that line in the array
loop++; //Does an increment to the variable 'loop'
myfile.close(); //Closes the file
else cout << "Unable to open file"<<endl; //Gives that sentence if the file can't be opened
for(loop2=0;loop2<=loop;loop2++) //For loop make to cout the lines stored
cout<<myobj[loop2]<<endl; //inside of the variable 'array'
return 0;

The problem I have is I get the following error message

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)