I have a header file in which there is a 2d array extern declaration, and a cpp file in which there is the actual definition for the array for it to link 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 other .cpp files I use this vector. When It was an array it all worked fine, I assume it has something to do with my syntax for declaring a 2 dimensional vector across multiple files but I have no idea!
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));
Make sure to spell the names of your files correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With