I have 3 files Test.h , Test.cpp and main.cpp
Test.h
#ifndef Test_H
#define Test_H
namespace v
{
int g = 9;;
}
class namespce
{
public:
namespce(void);
public:
~namespce(void);
};
#endif
Test.cpp
#include "Test.h"
namespce::namespce(void)
{
}
namespce::~namespce(void)
{
}
Main.cpp
#include <iostream>
using namespace std;
#include "Test.h"
//#include "namespce.h"
int main ()
{
return 0;
}
during building it gives the following error ..
1>namespce.obj : error LNK2005: "int v::g" (?g@v@@3HA) already defined in main.obj
1>C:\Users\E543925\Documents\Visual Studio 2005\Projects\viku\Debug\viku.exe : fatal error LNK1169: one or more multiply defined symbols found
kindly help as soon as possible ..
You have two options:
static:
extern:
You want just one instance of
g
accessed by everyone? In the header, usein Test.cpp, put
This is a definition:
that gets duplicated in
main.obj
andtest.obj
due to the#include "Test.h"
in each of the.cpp
files. The include guard#ifndef Test_H
only prevents multiple inclusions in a single translation unit.Change to:
and add the following to the
Test.cpp
: