LNK error, unresolved external...

This is a discussion on LNK error, unresolved external... within the C++ Programming forums, part of the General Programming Boards category; Ok, first problem taken care of, but why these errors? They go away if I inline the functions in the ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    154

    Unhappy LNK error, unresolved external...

    Ok, first problem taken care of, but why these errors? They go away if I inline the functions in the header file. Thanks for any help.
    Header file
    Code:
    #include <iostream>
    using namespace std;
    template <class T>
    class Array  
    {
    public:
    	Array(int = 10);
    	virtual ~Array() {delete [] ptr;}
    	T &operator[](int);
    	int getSize() const {return size;}
    
    private:
    	T *ptr;
    	int size;
    };
    Implementation
    Code:
    #include <iostream>
    #include <cassert>
    
    using namespace std;
    
    #include "t_Array.h"
    
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////
    
    template<class T>
    Array<T>::Array(int arraySize)
    {
    	size = (arraySize > 1 ? arraySize : 10);
    	ptr = new T[size] = {0};
    	assert(ptr != 0);
    	cout << "This is the class template version" << endl;
    }
    
    template<class T>
    T &Array<T>::operator[](int subscript)
    {
    	assert(1 <= subscript && subscript < size);
    	return ptr[subscript];
    }
    Driver
    Code:
    #include <iostream>
    using namespace std;
    #include "t_Array.h"
    
    int main()
    {
    	Array<double> tArray(15);
    
    	for (int i = 0; i < tArray.getSize(); i ++)
    	{
    		tArray[i] = i + 0.5;
    		cout << tArray[i] << endl;
    	}
    	return 0;
    }
    Errors
    Code:
    Compiling...
    t_Array.cpp
    Linking...
    TempArray.obj : error LNK2001: unresolved external symbol "public: double & __thiscall Array<double>::operator[](int)" (??A?$Array@N@@QAEAANH@Z)
    TempArray.obj : error LNK2001: unresolved external symbol "public: __thiscall Array<double>::Array<double>(int)" (??0?$Array@N@@QAE@H@Z)
    Debug/TempArray.exe : fatal error LNK1120: 2 unresolved externals
    Error executing link.exe.
    
    TempArray.exe - 3 error(s), 0 warning(s)

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    MSVC and templates don't mix well. Put the definitions and declarations in the same file and the problems should disappear.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Ok, that works, no idea why it didn't the other way though. Thanks.
    Now, though, how do I declare a default initialization for the constructor? I'm trying to allow an Array to be declared as Array <int> iArray(15); which works fine, or as Array<int> iArray(); with no parameter passed. This doesn't work. The working constructor is below. I tried overloading it with a version that took no arguments, but that didn't work either. Normally the header file would have a default initialization passed to the constructor definition in the implementation file, but I got rid of that file to get the program to compile. How can I get Array<int> iArray(); (or float, double, etc) versions to work? Thanks.
    Code:
    template <class T>
    class Array  
    {
    public:
    	Array(int arraySize)
    	{
    		size = (arraySize > 1 ? arraySize : 10);	// Check & set valid array size
    		ptr = new T[size];	// Allocate memory for array 
    		assert(ptr != 0);	// Check if memory allocated
    		for (int i = 0; i < size; i++)
    			ptr[i] = 0;
    		cout << "This is the class template version of the ";
    	}

  4. #4
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Just put a default argument in -

    Code:
    Array(int arraySize=10)
    	{
    		size = (arraySize > 1 ? arraySize : 10);	// Check & set valid array size
    		ptr = new T[size];	// Allocate memory for array 
    		assert(ptr != 0);	// Check if memory allocated
    		for (int i = 0; i < size; i++)
    			ptr[i] = 0;
    		cout << "This is the class template version of the ";
    	}
    But in your driver make sure you create an instance like -

    Array<int> tArray;

    rather than

    Array<int> tArray();
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. debug to release modes
    By DavidP in forum Game Programming
    Replies: 5
    Last Post: 03-20-2003, 02:01 PM
  5. Ask about these "unresolved external symbol" error
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 06-29-2002, 11:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21