How to resolve these compiler errors

There are these four functions i have called consecutively from main but the compiler shows the below errors, what do they mean ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Error	1	error LNK2019: unresolved external symbol "void __cdecl Fill_vector(class std::vector<class LandDetails,class std::allocator<class LandDetails> > &)" (?Fill_vector@@YAXAAV?$vector@VLandDetails@@V?$allocator@VLandDetails@@@std@@@std@@@Z) referenced in function _main
//here, i had written a vector and initialized with a class object containing strings like this:
LandDetails obj (string1, string2, string3....);       //LandDetails is a class containing declared string variables: string string1, string2, string3...;
myVector.push_back (obj);                          //is there anything wrong here ?

Error	2	error LNK2019: unresolved external symbol "void __cdecl Display_vector(class std::vector<class LandDetails,class std::allocator<class LandDetails> > const &)" (?Display_vector@@YAXABV?$vector@VLandDetails@@V?$allocator@VLandDetails@@@std@@@std@@@Z) referenced in function _main	
//here, i wanted to display the contents of the  vector filled above like this:
 void Display_vector (const vector <LandDetails>& displayer){
cout<<"You have entered :\n\n";
cout<<"string1: "<<displayer[i].string1<<"\n";
cout<<"string1: "<<displayer[i].string2<<"\n";
cout<<"string1: "<<displayer[i].string3<<"\n";
}

Error	3	error LNK2019: unresolved external symbol "void __cdecl write_vector(class std::vector<class LandDetails,class std::allocator<class LandDetails> > &)" (?write_vector@@YAXAAV?$vector@VLandDetails@@V?$allocator@VLandDetails@@@std@@@std@@@Z) referenced in function _main	
//here, i wanted to write the contents of the  vector filled to file like this:
 void write_vector (const vector <LandDetails>& writeFile){
 std::ofstream outFile("File.txt", ios::app); 
outFile<<"string1: "<<writeFile [i].string1<<",";
outFile<<"string1: "<<writeFile [i].string2<<",";
outFile<<"string1: "<<writeFile [i].string3<<",";
outFile<<endl;
}

Error	4	error LNK2019: unresolved external symbol "void __cdecl Display_file(class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > &,class std::vector<class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >,class std::allocator<class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > > > &)" (?Display_file@@YAXAAV?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@AAV?$vector@V?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@V?$allocator@V?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@@2@@2@@Z) referenced in function _main

//here, i wanted to read all the strings from file and display to screen like this:
void Display_file  ( std::vector<std::string> &line_values, std::vector<std::vector<std::string> >&display){
std::string line = "";   //variable to hold lines being read from file
std::ifstream inFile("File.txt");  //opening text file in read mode  by default i.e instead of  inFile("Lands File.txt", ios::in)
 int i;
        while (std::getline(inFile, line))
		{	  
        std::stringstream ss(line);   //a line is read into string stream as a strings   //ss is not a must here
 std::string vector_line;   //variable to hold strings
    while(std::getline(ss, vector_line))  	//  looping through the lines until it reach eof// this is not a must
    {
      line_values.push_back(vector_line);  //reading a line into a vector (temporary in memory)
    unsigned int size =display.size ();   
for ( i= 0; display.size (); i++)    
   display.emplace_back(line_values); // pushing every line at the end of the sequence of the vector of vectors
	cout<<line_values [i];     //display all line values by looping through lines ....displays all the file lines 
 	cout<<endl;       //move to newline
	 }
}
system ("pause");
inFile.close();
}

Last edited on
Hi,
Why suddenly did you get all those errors?
Can you make those errors temporarily go away?
These are link errors. After the code has been compiled, the linker can't find definitions for Fill_vector, etc. Where have you defined those functions? Is the file containing those definitions being compiled and linked into your executable?

MikeyBoy ....i have defined those functions in a separate .cpp file, so i have #included its header (.h) file to main.cpp.
I have also tried to include all the required headers i think required to the main.cpp but same errors persist.
Last edited on
are you missing const in your signatures ?
Ericool.....i have included one const parameter in the function void Display_vector (const vector <LandDetails>& displayer) and have included it as a declaration in the header and in the main.cpp before the call in the main ().
Last edited on
> i have #included its header (.h) file to main.cpp.
> I have also tried to include all the required headers i think required
you've got a linker error, includes don't help*
you need to add all the sources to your project.

you ought to provide the build command that you are using and all the files needed to reproduce your issue. I don't want to guess your typos.


* Unless you deal with template or inline functions.
....i have defined those functions in a separate .cpp file, so i have #included its header (.h) file to main.cpp.
I have also tried to include all the required headers i think required to the main.cpp but same errors persist.

Have you actually added the .cpp files to your project?
Yes Thomas, i have included the .cpp file, now i have included the static keyword to function declarations in header file, the "unresolved externals" have disappeared and what i now get is
1
2
 Error  1    error C2129: static function 'void write_vector(std::vector<_Ty> &)' declared but not defined	


yet i have defined all the functions outside the class in the .cpp file following the syntax:

1
2

<return type>  class_name::function_name (parameters) {//definition} 
Last edited on
> The "unresolved externals" have disappeared and what i now get is (C2129)...

You can't say that. They are compiler errors. Compiler errors always come first before linker errors.
Make sure write_vector() is declared only once in your header files. If there are more than one of the same thing, you must use extern instead of (static).

Also in function definition, make sure you remove all extern and static keywords or you will confuse the compiler or the linker.
> now i have included the static keyword to function declarations
¿and why did you do such a thing?
¿do you even know what that means?
http://stackoverflow.com/questions/558122/what-is-a-static-function
static functions are functions that are only visible to other functions in the same file



> If there are more than one of the same thing, (¿?)
> you must use extern instead of (static).
http://stackoverflow.com/questions/11712707/extern-functions-in-c-vs-c


> Yes Thomas, i have included the .cpp file
show your build command.
Ok ne555, thanks for that explanation on static keyword, i have removed the keyword, seems it limits the scope of functions to one file only.

The build command remains:

1
2
3
4
5
6
7
8

prog.cpp:(.text.startup+0x5e): undefined reference to `Fill_vector(std::vector<LandDetails, std::allocator<LandDetails> >&)'
prog.cpp:(.text.startup+0x66): undefined reference to `Display_vector(std::vector<LandDetails, std::allocator<LandDetails> >&)'
prog.cpp:(.text.startup+0x6e): undefined reference to `write_vector(std::vector<LandDetails, std::allocator<LandDetails> >&)'
prog.cpp:(.text.startup+0x77): undefined reference to `Display_file(std::vector<std::string, std::allocator<std::string> >&, std::vector<std::vector<std::string, std::allocator<std::string> >, std::allocator<std::vector<std::string, std::allocator<std::string> > > >&)'
prog.cpp:(.text.startup+0x7f): undefined reference to `search_record()'
collect2: error: ld returned 1 exit status
 
Last edited on
That's not the command, that's the error message

For example, the command
$ dict command
produces this output
From The Free On-line Dictionary of Computing (18 March 2015) [foldoc]:
  command
     <operating system> A character string which tells a program to
     perform a specific action.  Most commands take {arguments}
     which either modify the action performed or supply it with
     input.
here you may see some build commands http://www.cplusplus.com/forum/general/113904/#msg622055

You say that you have included the source to your project, I don't buy it.
Another issue may be that you don't respect the prototype, which is hard to guess because you didn't ever bother to show it, the more blatant case may be
1
2
3
4
5
//Error	3	error LNK2019: unresolved external symbol
//"void write_vector(class std::vector<class LandDetails> &)"
void write_vector (const vector <LandDetails>& writeFile){
	//...
}
where you added a `const' in the definition.


Again
you ought to provide the build command that you are using and all the files needed to reproduce your issue. I don't want to guess your typos.
ne555.....I have tried to run the code on the online shell and have included the command line as you have advised. Kindly check the output i have gotten (it is up there above your post).
I have done all the functions like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
void write_vector (std::vector <LandDetails>& writeFile);
int main()
{	std::vector <LandDetails>land;    //declaring a vector variable
	
	write_vector (land);

}

void LandDetails:: write_vector (std::vector <LandDetails>& writeFile){

	 std::ofstream outFile("Lands File.txt", ios::app);  //opening a file in write mode

	 if (!outFile) 
	 {
		 cout<<"Lands file failed to open. Data could not be entered to the register !";
		 return;
	 }
	
	unsigned int size =writeFile.size ();

	for (unsigned int i=0; i<size;i++)

	{
		
	
	outFile<<" Surname:"<<writeFile [i].Surname<<","<<
	" OtherNames:"<<writeFile [i].OtherNames<<","<<
	" Land Number:"<<writeFile [i].LandNum<<","<<
	" KRA PIN:"<<writeFile [i].KraPin<<","<<
	" ID card No.:"<<writeFile [i].OwnerIdCard<<","<<
	" address:"<<writeFile [i].address<<","<<
	" County:"<<writeFile [i].LandCounty<<","<<
	" District:"<<writeFile [i].LandDist<<","<<
	" Division:"<<writeFile [i].LandDiv<<","<<
	" Location:"<<writeFile [i].Landloc<<","<<
	" Sublocation:"<<writeFile [i].LandSubloc;


	outFile<<endl;

	std::cout<<"Land details have been successfully written to Land file ";
	
	}
	outFile<<endl;

}
Show us the header file and the #include part in main.cpp too. They may be very useful.
Line 1: You're declaring write_vector in the global namespace.
Line 9: write_vector is a member of either the Land_Details namespace or class. This is not the same as the instance in the global namespace. This will cause a linker error.

Line 3: land is an empty vector.
Line 5: You pass this empty vector to write_vector.
Line 19: You take the size of the empty vector. The result is 0.
Line 21-43: These lines don't execute because the upper limit of the for loop is 0.

Line 41: This should be after your for loop.




It would be rude of me sleeping without saying thank you to all programmers that have advised me accordingly. After considering all these suggestions, i have finally succeeded in running the program with all the five functions correctly linking. I have found it easier to declare, define and run everything from a single file.
Topic archived. No new replies allowed.