Jump to content
  • Advertisement
Sign in to follow this  
gameplayprogammer

std map problem

This topic is 3772 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;







Share this post


Link to post
Share on other sites
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;







Share this post


Link to post
Share on other sites
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;







Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!