0

I'm writing a code to display an array from a file, this is all done in a project with a .h file .cpp file and a main file. however my header file keeps giving me the error redefiniton of 'class token' and precious definition of 'class token' but I'm not very sure how to redefine it. thanks for help

main.cpp

using namespace std;
int main()
{
string filename;
int b;
int d;
int i;
int j;
token matrix[30][30];

cout << "Type the name of text file: e.g. 'File.txt'" << endl;
getline( cin, filename );//This could also be 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; i < b; i++)
{
    for(j = 0; j < d; j++)
    {

        inputFile.get(matrix[i][j]); 

        if(matrix[i][j] == ',' || matrix[i][j] == '\n') // for some reason if i dont include the                          \n it doesnt work, but it should because the row ends at 30
        {   //if loop to get rid of commas by deducting the counter by 1, hence replacing the comma with the next value

            j-- ;       



        }
    }

}
    cout << endl;

for(i = 0; i < b; i++){         // display
    for(j = 0; j < d; j++){

        cout << " " << matrix[i][j] ;


    }

    cout << endl ;
}

}

sorry but i'm new on this forum and don't really know how to use it, I've also included the header files in my main file and .cpp file

  • Are your header guards in place? Refer: http://stackoverflow.com/questions/8020113/c-include-guards – rockoder Nov 07 '14 at 02:00

1 Answers1

0

on the line

 matrix[30][30].setassign( filename );

you are trying to reference an item in the 2D array that is out of bounds. The elements you can use are in the range matrix[0][0] up to matrix[29][29]. matrix[30][30] is out of bounds.

remember in C++ when you allocate an array of 10 for example the first element is at index 0 the last (or tenth element) is at index 9.

Rob
  • 2,618
  • 2
  • 22
  • 29