std map problem

Started by
9 comments, last by Evil Steve 15 years, 10 months ago
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)
Advertisement
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
Million-to-one chances occur nine times out of ten!
Required Reading.
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.

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?
If you have "array" in the A namespace, A.cpp should be:
A.cpp----------------------include "A.h"std::map<int,int> A::array;
Or else the compiler doesn't know what "array" you're referring to
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;







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;







Got it now, declare std::map as extern within namespace in the .h
then again in the .cpp without the extern.
Thanks all.

A.h
---------------------

include <map>

namespace A {

extern std::map<int,int> array;

};


A.cpp
----------------------
include "A.h"

std::map<int,int> A::array;







sorry, only meant to reply once. pressed post a too many times :-)

This topic is closed to new replies.

Advertisement