How do you declare a std :: vector 2d global array for multiple files? C ++

I have a header file that has an extern 2d declaration of the array and a cpp file that has the actual definition for the array that it refers to. I would like to replace this array with a 2d vector, but my compiler keeps telling me:

            'A': redefinition; multiple initialization

Here is my code

header.h

            #ifndef HEADERS_H_DECLARED
            #define HEADERS_H_DECLARED

            #include <vector>
            ...
            extern std::vector<std::vector<int>> A(10, std::vector<int>(10));
            ...
            #endif

a.cpp

            #include "headers.h"
            ...
            std::vector<std::vector<int>> A(10, std::vector<int>(10));
            ...

Then in all my .cpp files I use this vector. When it was an array, everything worked fine, I suppose this has something to do with my syntax for declaring a two-dimensional vector for multiple files, but I have no idea!

+4
source share
1 answer

Like this:

header.h:

#ifndef HEADERS_H_DECLARED
#define HEADERS_H_DECLARED

#include <vector>

extern std::vector<std::vector<int>> A;

#endif

a.cpp:

#include "header.h"

std::vector<std::vector<int>> A(10, std::vector<int>(10));

, .

+4

Source: https://habr.com/ru/post/1676607/


All Articles