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?

1 comments

@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

24 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
  • 721507 View
  • 489 Score
  • 24 Answer
  • Tags:   c++ c file directory

14 Answered Questions

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

3 Answered Questions

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

  • 2009-10-31 10:06:34
  • Asad Khan
  • 337034 View
  • 382 Score
  • 3 Answer
  • Tags:   c++ c-preprocessor

11 Answered Questions

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

  • 2008-12-17 20:29:24
  • Gabriel Isenberg
  • 448305 View
  • 1602 Score
  • 11 Answer
  • Tags:   c++ unix profiling

10 Answered Questions

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

  • 2009-05-17 17:59:36
  • dau_man
  • 597342 View
  • 412 Score
  • 10 Answer
  • Tags:   c++ stl vector erase

15 Answered Questions

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

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

17 Answered Questions

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

  • 2009-02-20 21:58:41
  • Joan Venge
  • 800343 View
  • 543 Score
  • 17 Answer
  • Tags:   c++ vector std

9 Answered Questions

[SOLVED] Why have header files and .cpp files?

  • 2008-12-02 13:17:55
  • Clueless
  • 263528 View
  • 440 Score
  • 9 Answer
  • Tags:   c++ header-files

1 Answered Questions

[SOLVED] Resizing 2d vector - error

  • 2013-07-05 17:57:17
  • user1429101
  • 253 View
  • 0 Score
  • 1 Answer
  • Tags:   c++ vector

Sponsored Content