By MrBach

2019-02-11 09:29:47 8 Comments

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?


@Jack Aidley 2019-02-11 09:33:01

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.

@john 2019-02-11 09:35:30

Worth saying (in case the OP is tempted) that although it's bad practise in a cpp file, but it's simply wrong in a header file as it may make correct code that includes the header file uncompilable.

Related Questions

Sponsored Content

26 Answered Questions

[SOLVED] How can I get the list of files in a directory using C or C++?

  • 2009-03-04 19:35:58
  • samoz
  • 828033 View
  • 555 Score
  • 26 Answer
  • Tags:   c++ c file directory

17 Answered Questions

[SOLVED] How can I profile C++ code running on Linux?

  • 2008-12-17 20:29:24
  • Gabriel Isenberg
  • 494497 View
  • 1742 Score
  • 17 Answer
  • Tags:   c++ unix profiling

16 Answered Questions

[SOLVED] Why can templates only be implemented in the header file?

19 Answered Questions

[SOLVED] How to find out if an item is present in a std::vector?

  • 2009-02-20 21:58:41
  • Joan Venge
  • 879616 View
  • 588 Score
  • 19 Answer
  • Tags:   c++ vector std

4 Answered Questions

[SOLVED] Why are #ifndef and #define used in C++ header files?

  • 2009-10-31 10:06:34
  • Asad Khan
  • 391847 View
  • 462 Score
  • 4 Answer
  • Tags:   c++ c c-preprocessor

13 Answered Questions

[SOLVED] How do I erase an element from std::vector<> by index?

  • 2009-05-17 17:59:36
  • dau_man
  • 699831 View
  • 464 Score
  • 13 Answer
  • Tags:   c++ stl vector erase

10 Answered Questions

[SOLVED] How to convert vector to array

  • 2010-05-27 17:14:40
  • ganuke
  • 315433 View
  • 324 Score
  • 10 Answer
  • Tags:   c++ arrays vector

15 Answered Questions

[SOLVED] Can I call a constructor from another constructor (do constructor chaining) in C++?

  • 2008-11-21 09:43:08
  • Stormenet
  • 437578 View
  • 881 Score
  • 15 Answer
  • Tags:   c++ constructor

Sponsored Content