CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2006
    Posts
    11

    Inheritance Issue, Linker error

    Okay, this is part of my final for my OOP c++ class, and for the life of me i can't figure out why i'm getting this linker error.

    Main.obj : error LNK2001: unresolved external symbol "public: __thiscall Prob3TableInherited<int>::Prob3TableInherited<int>(char *,int,int)" (??0?$Prob3TableInherited@H@@QAE@PADHH@Z)
    Debug/Final.exe : fatal error LNK1120: 1 unresolved externals

    Prob3Table = base class
    Prob3TableInheritance = derived class

    My very newbie hunch is that it has something to do with my derived class' constructor.

    Sorry for posting so much code and being such a newb.

    This is the professor-written driver, it shouldnt need changes:
    Code:
    void problem3()
    {
    	cout<<"Entering problem number 3"<<endl;
    
    	int rows=5;
    	int cols=6;
    
    	Prob3TableInherited<int> tab("Problem3.txt",rows,cols);
    
    	//Initializes naugT to tab.table
    	const int *naugT=tab.getTable();
    
    	//Outputs Table
    	for(int i=0;i<rows;i++)
    	{
    		for(int j=0;j<cols;j++)
    		{
    			cout<<naugT[i*cols+j]<<" ";
    		}
    		cout<<endl;
    	}
    
    	cout<<endl;
    
    	//Initializes augT to tab.augTable (table with summed columns/rows)
    	const int *augT=tab.getAugTable();
    
    	for(i=0;i<=rows;i++)
    	{
    		for(int j=0;j<=cols;j++)
    		{
    			cout<<augT[i*(cols+1)+j]<<" ";
    		}
    		cout<<endl;
    	}
    }
    This is the base class Prog3Table:
    Code:
    template <class T>
    Prob3Table<T>::Prob3Table(char *in, int row, int col)
    :rows(row), cols(col), grandTotal(0)
    {
    	ifstream inStream;
    	inStream.open(in);
    
    	//Creates 2-dimensional array
    	table = new T[rows*cols];
    	
    	//File input to array
    	for(unsigned int i = 0; i < rows; i++)
    	{
    		for(unsigned int j = 0; j < cols; j++)
    		{
    			inStream << augTable[i][j];
    		}
    	}
    
    	//Row/Col sum arrays
    	rowSum = new T[rows] = 0;
    	colSum = new T[cols] = 0;
    }
    
    template <class T>
    void Prob3Table<T>::calcTable()
    {
    	//row sum
    	for(unsigned int i = 0; i < rows; i++)
    	{
    		for(unsigned int j = 0; j < cols; j++)
    		{
    			rowSum[i] += table[i][j];
    			grandTotal += table[i][j];
    		}
    	}
    
    	//col sum
    	for(i = 0; i < cols; i++)
    	{
    		for(j = 0; j < rows; j++)
    		{
    			colSum[i] += table[i][j];
    			grandTotal += table[i][j];
    		}
    	}
    }
    And this is the inherited class, Prog3TableInherited
    Code:
    #include "Prob3TableInherited.h"
    
    template <class T>
    Prob3TableInherited<T>::Prob3TableInherited(char *in, int row, int col)
    {
    	//Aug array declared with rows+1/cols+1 to make room for totals
    	augTable = new T[(rows+1)*(cols+1)];
    
    	//CalcTable is called from base class to calculate sums/total
    	calcTable();
    
    	//Column sum is added to augTable columns
    	for(i = 0; i < rows; i++)
    	{
    		for( j = 0; j < cols; j++)
    		{
    			augTable[rows+1][j] = colSum[j];
    		}
    	}
    
    	//Row sum is added to augTable rows
    	for(j = 0; j < cols; j++)
    	{
    		for(i = 0; i < rows; i++)
    		{
    			augTable[i][cols+1] = rowSum[i];
    		}
    	}
    
    
    }

  2. #2
    Join Date
    Dec 2006
    Posts
    154

    Re: Inheritance Issue, Linker error

    this is a template clas, so the code should be in a header file included by the file that calls the constructor.

    is it ?

  3. #3
    Join Date
    Oct 2006
    Posts
    11

    Re: Inheritance Issue, Linker error

    in my main.cpp i dont have template declared anywhere.
    upon your suggestion i tried putting template <class T> right above my problem3() function, and that just changed the linker error, it didnt get rid of it.

    I also tried throwing it up in the header, but quickly learned that didnt work either.

    Sorry for being so ignorant but could you clarify?

  4. #4
    Join Date
    Dec 2006
    Posts
    154

    Re: Inheritance Issue, Linker error

    i meant that the code of the function should be accessible to the main.cpp file, not just the interface :

    for instance,

    main.cpp
    Code:
    new Test<int>(1);
    test1.h
    Code:
    template< typename T >
    class Test
    {
       Test(T val);
    };
    test1.cpp:
    Code:
    template< typename T >
    Test<T>::Test(T val)
    {
    ...
    }
    This above doesn't work, because main.cpp has to know not only the interface of the test class but also the implementation, because the compiler will then generate the specific code.

    This works :
    Code:
    new Test<int>(1);
    test1.h
    Code:
    template< typename T >
    class Test
    {
       Test(T val);
    };
    
    template< typename T >
    Test<T>::Test(T val)
    {
    ...
    }
    then by including "Test.h" you know about the implementation, and main.cpp can generate the specific code it needs.

  5. #5
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

  6. #6
    Join Date
    Sep 2005
    Location
    United States
    Posts
    799

    Re: Inheritance Issue, Linker error

    Quote Originally Posted by ccrabb
    in my main.cpp i dont have template declared anywhere.
    upon your suggestion i tried putting template <class T> right above my problem3() function, and that just changed the linker error, it didnt get rid of it.

    I also tried throwing it up in the header, but quickly learned that didnt work either.

    Sorry for being so ignorant but could you clarify?
    Basically, what screetch is saying is that when you are dealing with templates, you really can't separate the class declaration from the members functions like you would with normal, non-templated classes.

    Unless you use one of the tricks described in exterminator's link, then both the class declaration and member function definitions have to be in the same header file.
    (Meaning you can't have the class in the .h file, and the member functions in a separate .cpp file)

    If it's just a school assignment, this is probably the easiest way to solve unresolved external linker errors when dealing with templates.
    Last edited by dcjr84; December 14th, 2006 at 11:09 AM.
    Please rate my post if you felt it was helpful

  7. #7
    Join Date
    Oct 2006
    Posts
    11

    Re: Inheritance Issue, Linker error

    Oh okay, thats clears up a lot.
    Thank you very much gentlemen.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured