std map problem
Had a problem with this block of code.
Got a linker error
In my file A.h i have....
pragma once
include <map>
namespace A
{
std::map< int,int > array;
}
in my file A.cpp i have...
include A.h
the errors i get are
BFile.obj : error LNK2005: class std::map<int,int.....already defined in CFile.obj
Anyone any ideas???
(Note: hash key on my keyboard is not working so no hash in front of include etc)
Apparently you're also including A.h in BFile.cpp. So in both cases the variable 'array' get instantiated, and there should be only one.
Change the definition in A.h to
extern std::map< int,int > array;
and put a
std::map< int,int > array;
in A.cpp
Change the definition in A.h to
extern std::map< int,int > array;
and put a
std::map< int,int > array;
in A.cpp
Tried that and i got a unresolved linker error, this time in A.obj.
Tried taking the extern std::map<int,int> array out of the namespace and that fixed it.
Tried taking the extern std::map<int,int> array out of the namespace and that fixed it.
Sorry should have been clearer.
My files now read.....
A.h
---------------------
include <map>
// this works
extern std::map<int,int> array;
namespace A {
// when i did this i got a linker error
// extern std::map<int,int> array;
};
A.cpp
----------------------
include "A.h"
std::map<int,int> array;
My problem now is std::map<int,int> array part of namespace A?
My files now read.....
A.h
---------------------
include <map>
// this works
extern std::map<int,int> array;
namespace A {
// when i did this i got a linker error
// extern std::map<int,int> array;
};
A.cpp
----------------------
include "A.h"
std::map<int,int> array;
My problem now is std::map<int,int> array part of namespace A?
If you have "array" in the A namespace, A.cpp should be:
Or else the compiler doesn't know what "array" you're referring to
A.cpp----------------------include "A.h"std::map<int,int> A::array;
Yeah but the array is now no longer declared in namespace A.
I originally declared it in namespace A but got linker errors,
so i made it extern and still got a linker error, so i moved it out of the
namespace. See below...
A.h
---------------------
include <map>
// this works
extern std::map<int,int> array;
namespace A {
// when i did this i got a linker error
// extern std::map<int,int> array;
};
so how can doing the code below tell the compiler that
namespace A has an array when it doesn't anymore?
A.cpp
----------------------
include "A.h"
std::map<int,int> A::array;
I originally declared it in namespace A but got linker errors,
so i made it extern and still got a linker error, so i moved it out of the
namespace. See below...
A.h
---------------------
include <map>
// this works
extern std::map<int,int> array;
namespace A {
// when i did this i got a linker error
// extern std::map<int,int> array;
};
so how can doing the code below tell the compiler that
namespace A has an array when it doesn't anymore?
A.cpp
----------------------
include "A.h"
std::map<int,int> A::array;
Yeah but the array is now no longer declared in namespace A.
I originally declared it in namespace A but got linker errors,
so i made it extern and still got a linker error, so i moved it out of the
namespace. See below...
A.h
---------------------
include <map>
// this works
extern std::map<int,int> array;
namespace A {
// when i did this i got a linker error
// extern std::map<int,int> array;
};
so how can doing the code below tell the compiler that
namespace A has an array when it doesn't anymore?
A.cpp
----------------------
include "A.h"
std::map<int,int> A::array;
I originally declared it in namespace A but got linker errors,
so i made it extern and still got a linker error, so i moved it out of the
namespace. See below...
A.h
---------------------
include <map>
// this works
extern std::map<int,int> array;
namespace A {
// when i did this i got a linker error
// extern std::map<int,int> array;
};
so how can doing the code below tell the compiler that
namespace A has an array when it doesn't anymore?
A.cpp
----------------------
include "A.h"
std::map<int,int> A::array;
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement