Search code examples
c++vectorstructheader-files

How can I use the vector datatype in a header-file in C++?


I have this struct below for matrixes. It has worked properly when I had declared it in my main.cpp, but since the program became more complex, I decided to swap it out. So my header file looks like this:

    #ifndef MATRIX_STRUCT_H
    #define MATRIX_STRUCT_H

    #include <vector>

    // Matrix datatype
    struct matrix_ {
        // Matrix dimension m x n
        unsigned int dimX; // n
        unsigned int dimY; // m
        bool square;
        // Matrix coefficients
        vector <vector <double>> coef;
    };
    typedef struct matrix_ matrix;

    #endif // MATRIX_STRUCT_H

The problem I have now is, that line 4 #include <vector> makes no difference if it is here or not. I always get the error for line 13

error: 'vector' does not name a type

If you're wondering why I'm using a struct and not a class, I simply arrived only recently from C, so I don't have any experiences with classes yet.

Does anybody can help me, please? Would it be better (would it help) to abandon the structs and concentrate on classes only?


Solution

  • The C++ standard library includes put new declarations in the std namespace. You need to write std::vector instead of just vector.

    Likely the reason that it worked in your .cpp file is that you have the line using namespace std somewhere near the top which brings everything from the namespace std and brings it into the local namespace. This is bad practice and I recommend you stop doing it.