Multiple defined variables including a header

I'm having some troubles with including headers in my project. While I'm not exactly new to programming, I am new to spanning my source files to better organize things. I've googled a bit and though I see examples of this, I can't find my error. I have a main.cpp file, a header, system.h, defining some variables and declaring some functions, and a system.cpp defining the functions that were declared in the header. I need to use the variables defined in system.h in system.cpp. I'm also including system.h in the main.cpp. I have guards on my header but I'm still having a clash and getting variable previously defined errors. What is the error here? How can I use the variables in system.h in system.cpp without a clash? do I need to declare them with extern and drop the #include "system.h" in system.cpp?


main.cpp
1
2
3
4
5
6
7
8
#include <iostream>
#include "system.h"

int main()
{
    //for system loop
    return 0;
}


system.h
1
2
3
4
5
6
7
8
9
10
11
12
#ifndef SYSTEM_H
#define SYSTEM_H
#include <vector>

//variables
std::vector<unsigned char> memory(4096);
unsigned char opcode;

//functions
void memClear();

#endif //SYSTEM_H 


system.cpp
1
2
3
4
5
6
7
#include <vector>
#include "system.h"

void memClear(){
    for(unsigned int i = 0; i < memory.size();++i)
        memory[i] = 0x00;
}
For any global variable in your header, you'll have to use the extern keyword, making sure to only declare it once in your header, and define it once in a source file.
Last edited on
So to avoid name collisions with the global variable, which ideally I'd like to avoid all together, should I wrap this up in a class? It seems overkill but it would stop collisions right? I can change the function
 
void clearMem();

to pass a reference of the vector memory like this
 
void clearMem(std::vector<unsigned char>&);

but that still doesn't solve the fact that I have global non-const(s) polluting my code.

What's the standard way to do this? Other source files WILL need access to the memory vector. Would placing them in a namespace be sufficent?
Last edited on
That's the reason why namespaces have been introduced.
> but that still doesn't solve the fact that I have global
so... ¿why can't declare the variable inside main?
So to avoid name collisions with the global variable, which ideally I'd like to avoid all together, should I wrap this up in a class?

That doesn't change the problem. You would still have to declare the global instance of the class as extern in the header.

but that still doesn't solve the fact that I have global non-const(s) polluting my code.

As ne555 said, why can't you declare it in main and pass it where needed? Then you don't need to declare it as extern.

Would placing them in a namespace be sufficent?

No. Namespaces won't change the duplicate defined symbol problem. Namespaces only prevent name collisions. You have the same name defined in multiple compilation units. It doesn't matter if it's qualified by a namespace or not.
Wow, it's amazing how lost I get thinking about this. I'm getting so stuck on wanting to keep my code separated that I'm shooting myself in the foot.
Topic archived. No new replies allowed.