Thread: Undefined Reference to a generic function error

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    Undefined Reference to a generic function error

    I'm doing this example that involves two generic functions, one that takes a vector of type T and another that takes an array of type T. The functions worked when I put both the declaration and definition within main, but when I split them up between the header file and implementation I get an undefined reference to the functions when I call them from main.

    Code:
    //main.cpp
    #include <cstdlib>
    #include <iostream>
    #include <vector>
    #include "headers.h"
    
    
    
    using std::vector;
    using std::cout;
    using std::endl;
    
    
    int main()
    {
    
        int a_grades[8] = {87, 95, 100, 83, 75, 49, 56, 94};  
        vector<int> v_grades(a_grades, a_grades + 9);
           
        cout << median(a_grades) << endl;
        cout << median(v_grades) << endl;
        
        //49 56 75 83 87 94 95 100
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Code:
    //headers.h
    #ifndef headers_h
    #define headers_h
    
    #include <vector>
    
    template <class T> double median(std::vector<T> vec);
    template <class T> double median(T vec);
    
    #endif
    Code:
    //headers.cpp
    #include <iostream>
    #include <vector>
    #include <stdexcept>
    #include <cstddef>
    #include "headers.h"
    
    using std::vector;
    
    
    template <class T> double median(vector<T> vec)
    {
    
    	typedef typename vector<T>::size_type vec_sz;
    
    	vec_sz size = vec.size();
    	if (size == 0)
    		throw std::domain_error("median of an empty vector");
    
    	std::sort(vec.begin(), vec.end());
    
    	vec_sz mid = size/2;
    
    	return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid];
    }
    
    template <class T> double median(T vec)
    {   
        size_t size = sizeof(vec)/sizeof(*vec);
        
    	if (size == 0)
    		throw std::domain_error("median of an empty vector");
    
    	std::sort(vec, vec + size);
    	    
               
    	size_t mid = size/2;
    
    	return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid];   
    }
    errors
    Code:
    In function `main': 
    [Linker error] undefined reference to `double median<int*>(int*)' 
    [Linker error] undefined reference to `double median<int>(std::vector<int, std::allocator<int> >)' 
    ld returned 1 exit status
    [Build Error]  [practice.exe] Error 1
    Last edited by indigo0086; 06-26-2006 at 09:48 PM.

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    template implementations must be #include'd by the code using them. typically this means defining the template class or functions in a header.

    basiaclly move all your code in headers.cpp to headers.h and you'll be sweet. If you're interested why this is google for the C++ export keyword. (only one compiler that I've heard of supports this, Comeau C++ http://www.comeaucomputing.com/tryitout/ )
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    cool, thanks man.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  2. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM