Question

I am running into an error that I do not know how to fix in a program about reading a text file and putting the tokens into a multidimensional vector.

My error is : char *token Error: argument of type "char *" is incompatible with parameter of type "char".

The codes are :

    std::vector< vector <std::string> > my_matrix(10, vector <std::string>(10));

    ifstream myReadFile;
    myReadFile.open("class_data.txt", ios_base::in);
    char output[100];
    if (myReadFile.is_open()) {
        while (myReadFile >> output) {
            myReadFile >> output;

            char* token = NULL;
            char* context = NULL;
            char delims[] = " ,\t\n";

            token = strtok_s(output, delims, &context);

            while (token != NULL)
            {
                for (int i = 0; i < 10; i++) {
                    for (int j = 0; j < 10; j++) {
                        my_matrix[i][j].push_back(token);
                        token = strtok_s(NULL, delims, &context);
                    }
                }
            }
        }
    }

The task would be to get the tokens out of the text file and put them nicely into a 2-Dimensional vector of strings (10x10 matrix). When I want to push_back the tokens into the 2D matrix, char* token is incompatible with the expression. Could anyone help me fix the char* token error ?

P.S Sorry if I do not have the best professional codes, but I am a beginner in C++ and I did not take any University level programming courses.

Was it helpful?

Solution

This will be the cause of your error.

my_matrix[i][j].push_back(token);

Change it to

my_matrix[i][j].push_back(*token);

you should be fine. Your my_matrix holds vector of vector of strings whereas the token is a char pointer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top