|
-
December 14th, 2006, 04:59 AM
#1
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];
}
}
}
-
December 14th, 2006, 05:22 AM
#2
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 ?
-
December 14th, 2006, 05:32 AM
#3
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?
-
December 14th, 2006, 05:46 AM
#4
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
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 :
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.
-
December 14th, 2006, 06:53 AM
#5
Re: Inheritance Issue, Linker error
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
December 14th, 2006, 11:05 AM
#6
Re: Inheritance Issue, Linker error
 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
-
December 14th, 2006, 03:21 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|