Thread: LNK2019 unresolved external symbol

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    19

    LNK2019 unresolved external symbol

    I'm working on a program that reads in values from a file 3 at a time and stores them separately in a vector. I keep getting this error when I run the program, and I can't figure out why. The class that reads in the input file is a template so it can read in numbers of any type, so I might have some problem with all the template stuff, because I'm new to it and the book I'm learning from doesn't go in to much detail. Anyway, here are the files:

    Code:
    //class header file
    #include <vector>
    
    template <typename T> 
    class TextFile
    {
    public:
    	void readFile();
    	void printValues() const;
    	
    private:
    	std::vector< T > inputValues;
    };
    
    //class implementation file
    #include <iostream>
    #include "TextFile.h"
    #include <fstream>
    #include <vector>
    #include <cstdlib>
    #include <algorithm>
    #include <iterator>
    using namespace std;
    
    template <typename T>
    void TextFile<T>::readFile()
    {
    	ifstream inFile("TopicIinBasic.txt");
    	if (!inFile)
    	{
    		cout << "File open failed.  Program ending" << endl;
    		exit(1);
    	}
    	
    	while (!(inFile.eof()))
    	{
    		T tempVal = 0;
    		string temp;
    		stringstream ss;
    
    		getline(inFile, temp); //Read in up to newline character
    		if (inFile.fail())
    		{
    			cout << "Input from file failed.  Program ending." << endl;
    			inFile.close();
    			exit(1);
    		}
    
    		if (count(temp.begin(), temp.end(), '/') != 2)
    		{
    			cout << "Error in input.  Program ending." << endl;
    			inFile.close();
    			exit(1);
    		}
    
    		replace(temp.begin(), temp.end(), '/', ' ');
    
    		ss << temp;
    		for (int i = 0; i < 3; i++)
    		{
    			ss >> tempVal;
    			inputValues.push_back(tempVal);
    		}
    	}
    	inFile.close();
    }
    
    template <typename T>
    void TextFile<T>::printValues() const
    {
    	ostream_iterator< T > output(cout, "\n");
    	
    	copy(inputValues.begin(), inputValues.end(), output);
    }
    
    //main
    #include <iostream>
    #include "TextFile.h"
    using namespace std;
    
    int main()
    {
    	TextFile<unsigned short int> text;
    	text.readFile();
    	cout << "Display input: " << endl;
    	text.printValues();
    	return 0;
    }
    
    //error message
    1>TopicI.obj : error LNK2019: unresolved external symbol "public: void __thiscall TextFile<unsigned short>::printValues(void)const " (?printValues@?$TextFile@G@@QBEXXZ) referenced in function _main
    1>TopicI.obj : error LNK2019: unresolved external symbol "public: void __thiscall TextFile<unsigned short>::readFile(void)" (?readFile@?$TextFile@G@@QAEXXZ) referenced in function _main
    fatal error LNK1120: 2 unresolved externals
    There it is; if anyone could point me in the direction of the problem that'd be awesome. Thanks.

    Edit: Just noticed that I didn't have a _ in push_back, but putting that in did not fix the problem.

    Edit2: Ok, so I just expanded the program quite a bit and added some more classes, and I'm having this same error between all of my classes and my main. In main, I make function calls to functions of classes 9 times, and I get 9 linker errors telling me basically the same thing as above. I still can't see what's wrong with it though. Any help would be appreciated greatly.

    Edit 3: Wow, I'm an idiot. Totally forgot that the implementation for a class goes in the same file as the definition of it, unless they're linked differently than normal, which mine were not. Fixing that fixed all my linker problems, and then I just had to fix a couple small things and my program is now in business. Awesome.
    Last edited by Freddy92; 05-03-2011 at 03:47 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You can't compile the implementation and expect to link it because the compiler has nothing to do when it compiles without a specific type in place of the template parameter... it just does nothing, no code is generated. Then, when you try and link that with the rest of your code that calls the code with a specific type (unsigned short), it can't link because the code does not exist. To get around this, when working with templates you must place your code - currently in your implementation file - within the header. So, remove that implementation file and place the code inside the header... then just include that header. Then, whenever your program instantiates that template with a specific type it can generate the appropriate code from what it sees in the header.

    Also:
    Code:
    while (!(inFile.eof()))
    {
        T tempVal = 0;
        string temp;
        stringstream ss;
    
        getline(inFile, temp); //Read in up to newline character
        ...
    Don't control your loops using an end-of-file test. Link provided is a discussion of the topic using C instead of C++ but the lesson still applies to C++. What you should probably have instead is:
    Code:
    string temp;
    
    while (getline(inFile, temp))
    {
        T tempVal = 0;
        stringstream ss;
    
        ...
    [edit]Oops, I did not see your edit3 so I guess my discussion was not needed, however the end-of-file test thing still needs to be fixed.[/edit]
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with: error LNK2019: unresolved external symbol
    By darren78 in forum C++ Programming
    Replies: 2
    Last Post: 06-16-2010, 06:26 AM
  2. error LNK2019: unresolved external symbol
    By Warlax in forum C++ Programming
    Replies: 8
    Last Post: 12-09-2008, 01:37 PM
  3. LNK2019 unresolved external symbol
    By lerckeman in forum C++ Programming
    Replies: 2
    Last Post: 12-16-2006, 02:34 AM
  4. error LNK2019: unresolved external symbol
    By Opel_Corsa in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2006, 12:12 PM
  5. LNK2019: unresolved external symbol
    By mhandlon in forum Windows Programming
    Replies: 3
    Last Post: 01-19-2006, 12:55 PM