Error with classes using 2-D Arrays in C++ -
Error with classes using 2-D Arrays in C++ -
i'm writing code display array file, done in project .h file .cpp file , main file. header file keeps giving me error redefiniton of 'class token' , precious definition of 'class token' i'm not sure how redefine it. help
main.cpp using namespace std; int main() { string filename; int b; int d; int i; int j; token matrix[30][30]; cout << "type name of text file: e.g. 'file.txt'" << endl; getline( cin, filename );//this cin >> name// matrix[30][30].setassign( filename ); cout << endl ; system("pause") ; } token.h // header file token.h using namespace std; class token { private: string assignmat; //char displaymat; //char displaytoken; public: // constructors token(); token( string ); //token( string, char, char ); // public functions void setassign( string ); //void setdisplay( char ); //void settoken( char ); };` token.cpp using namespace std; // constructor 1 token::token() { assignmat = " "; //displaymat = ' '; //displaytoken = ' '; } // constructor 2 parameterised constructor token::token( string assignmat1) //, int displaymat1, char displaytoken1 ) { assignmat = assignmat1; //displaymat = displaymat1; //displaytoken = displaytoken1; } // public function setname void student::setassign( string filename ) { char matrix[30][30]; ifstream inputfile; int i,j; inputfile.open(filename.c_str()); for(i = 0; < b; i++) { for(j = 0; j < d; j++) { inputfile.get(matrix[i][j]); if(matrix[i][j] == ',' || matrix[i][j] == '\n') // reason if dont include \n doesnt work, should because row ends @ 30 { //if loop rid of commas deducting counter 1, hence replacing comma next value j-- ; } } } cout << endl; for(i = 0; < b; i++){ // display for(j = 0; j < d; j++){ cout << " " << matrix[i][j] ; } cout << endl ; } }
sorry i'm new on forum , don't know how utilize it, i've included header files in main file , .cpp file
on line
matrix[30][30].setassign( filename );
you trying reference item in 2d array out of bounds. elements can utilize in range matrix[0][0] matrix[29][29]. matrix[30][30] out of bounds.
remember in c++ when allocate array of 10 illustration first element @ index 0 lastly (or 10th element) @ index 9.
c++
Comments
Post a Comment