fatal error LNK1120: 1 unresolved externals

Hi,

I have these two files

global.h
1
2
3
4
5
6
7
8
#include <iostream>
#include <map>
#include <vector>
#include <string>

using namespace std;

extern map<string, vector<double>> mapData;


and

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <map>
#include <vector>
#include <string>
#include "global.h"
//using namespace std;

int main()
{
	vector<double> dv(2);
	dv[0] = 0.0;
	dv[1] = 0.1;

	mapData["Hello"] = dv;

	return 0;
}


When I build this project in Visual Studio, I am getting the following error

SampleCppProject1.exe : fatal error LNK1120: 1 unresolved externals

error LNK2001: unresolved external symbol "class std::map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::vector<double,class std::allocator<double> >,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,class std::vector<double,class std::allocator<double> > > > > mapData" (?mapData@@3V?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$vector@NV?$allocator@N@std@@@2@U?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$vector@NV?$allocator@N@std@@@2@@std@@@2@@std@@A)

Not a very helpful error message. Please guide.

Thank you
extern map<string, vector<double>> mapData
You haven't fully defined mapData yet.
1
2
3
4
5
int main()
{
    map<string, vector<double>> mapData{};
    // ...
}

Hello kinley,

integralfx is correct to a point. Think of extern as a prototpe for a variable. It is saying the map<string, vector<double>> mapData is defined in a another file, but you only have two files. so where is map<string, vector<double>> mapData defined?

The other problem is that I do not believe that extern can be used in the same file as the definition.

If you are going to put an extern in a header file you have to be careful where you use that header file.

For your two files I would delete line 8 from the header file and define the map inside main. This way if you add more files that would use the map definition with the key word extern in oth other files.

Hope that helps,

Andy
Hi intergralfx and Andy,

Thank you for your reply.

Andy, I am sorry but I still do not understand your advise for deleting line 8 from header file. So do you basically want me to delete the header file?

Anyways, here is a simple set of files if you could throw some light.
I am trying to populate mapData in another class defined in cpp and displaying in main.

global.h
1
2
3
4
5
#include <map>
#include <string>
#include <vector>

extern map<string, vector<double>> mapData;


sample.h
1
2
3
4
5
6
7
8
#include<iostream>
using namespace std;

class sample
{
public:
	void populateMapData();
}


sample.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "sample.h"
#include "global.h"
#include <vector>
#include <map>

using namespace std;

void sample::populateMapData()
{
	vector<double> dv(2);
	dv[0] = 0.0;
	dv[1] = 0.1;

	mapData["Hello"] = dv;
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <map>
#include "global.h"
#include "sample.h"

using namespace std;
int main (void) 
{
	sample s;
	s.populateMapData();
	cout << mapData.size() << endl;
}


Its giving me error like this
1
2
3
samplecppproject1\global.h(5): error C2143: syntax error : missing ';' before '<'
samplecppproject1\global.h(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
samplecppproject1\main.cpp(7): error C2143: syntax error : missing ';' before 'using'



Last edited on
Hello kinley,

Sorry I was thinking to far ahead and just thinking about line 8 from your original post. Now I see that if you delete line 8 it makes the rest of the "global.h" mostly pointless.
As integralfx said
extern map<string, vector<double>> mapData

You haven't fully defined mapData yet.

Now that you have shown me more to work with the extern map<string, vector<double>> mapData; makes a little more sense, but I still do not see where you actually define the map.

Sounds like I have not seen every file that you are using. The others would be helpful. Also I am curious as to what IDE and or compiler you are using. I will set up your files and see what I come up with.

Andy

P.S. You are missing a semi-colon after the closing brace of the class in the "sample.h" file.
Last edited on
Hi Andy,

Thanks for your reply.

I am using Visual Studio 2010. I didn't change anything in compiler settings.

Those are the only files that I have.

Sorry but I do not quite understand what integralfx means by defining. Basically what I want is by end of main program, the mapData should hold an entry with "Hello" as the key and a vector of double 0.0, 0.1 as the value.
And I want this data population to be done in another function defined in another class and not in main.

Thanks once again for your help :)
Hello kinley,

When I finally ascertained the right approach I ended up with:
commenting out the "#include "global.h" in main and putting map<string, vector<double>> mapData; as a global variable in main.
The "sample.h" file needed a semi-colon after the closing brace of the class.
The "sample.cpp" file needed the "sample.h" and "gllobal.h" files.

These changes allowed the program to compile.

When I ran the program it gave me an output of 1 and when I looked at mapData it contained the correct information. Do not know what you want to do, but the program works and able to be expanded.

Hope that helps,

Andy
Thank you very much Andy!

It worked flawlessly.
Topic archived. No new replies allowed.