Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unresolved external linker error - I'm sure I'm doing it right! 1

Status
Not open for further replies.

biot023

Programmer
Nov 8, 2001
403
GB
Hallo.
I'm going to list a few files (edited for relevance) that I cannot get to compile. All .cpp files are added to the project, and there is nothing wrong that I can identify.
Nonetheless, I am getting the three functions for Tconsole_script that are implemented in the "ftp_console.cpp" file as unresolved externals.
Can anybody see why?



My main unit file "ftpcwe_mainunit.cpp"
// start ------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include<string>
using namespace std;

#include &quot;ftp_console.h&quot;
#include &quot;ftp_console_exceptions.h&quot;
#include &quot;ftpcwe_mainunit.h&quot;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Tconsole_script<string> Buffy;
}
// end --------------------------------------------------



Header file &quot;ftp_console.h&quot;
// start ------------------------------------------------
#if !defined(MARYHADALITTTLEWOLFITATEHERHEADOFF023)
#define MARYHADALITTTLEWOLFITATEHERHEADOFF023

#include<vcl.h>
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<dir>
#include&quot;ftp_console_exceptions.h&quot;
using namespace std;

template<class T>
class Tconsole_script:public vector<T>
{
public:
Tconsole_script():filename(&quot;&quot;),index(-1){}
virtual ~Tconsole_script(){}

virtual int& Index(){return index;}
virtual void MoveBy(int& incr);
virtual void MoveBack();
virtual void MoveNext();

protected:
T filename;
int index;
};

#endif // 'MARYHADALITTTLEWOLFITATEHERHEADOFF023' defined
// end --------------------------------------------------




cpp file &quot;ftp_console.cpp&quot;
// start ------------------------------------------------
#include&quot;ftp_console.h&quot;

template<class T>
void Tconsole_script<T>::MoveBy(int& incr)
{
if(index<0)throw EScript_exception e(&quot;MoveBy(int&) invalid on empty script&quot;);
if(index+incr<0 || index+incr>=size())throw EScript_exception e(&quot;MoveBy(int&) - array out of bounds&quot;);
index+=incr;
}
//---------------------------------------------------------------------------

template<class T>
void Tconsole_script<T>::MoveBack()
{
if(index<0)throw EScript_exception e(&quot;MoveBack() invalid on empty script&quot;);
if(index==0)throw EScript_exception e(&quot;MoveBack() takes array out of bounds&quot;);
index--;
}
//---------------------------------------------------------------------------

template<class T>
void Tconsole_script<T>::MoveNext()
{
if(index<0)throw EScript_exception e(&quot;MoveNext() invalid on empty script&quot;);
if(index==size())throw EScript_exception e(&quot;MoveNext() takes array out of bounds&quot;);
index++;
}
// end --------------------------------------------------




The file &quot;ftp_console_exceptions.h&quot; is present, and its .cpp file is fine & added to the project.
I'm stumped.
Any help VERY appreciated.

Cheers,
Douglas JL

Common sense is what tells you the world is flat.
 
After searching, I came up w/ the following answer:
&quot;OK, here's the trouble. When the compiler generates the code for each .cpp file (the ones with the template definitions in them) it doesn't know what types you will instantiate the template for, so it doesn't create any instances (unless you used a particular instance in that file - like if you used Form<int> as a variable, or something like that).
When it compiles the file that actually does the instantiation, it doesn't have the code to make the instance, and assumes that it has been done somewhere else (ie. where it was defined). So when you link, all of the modules think the other modules defined the instance (along with all the appropriate member functions, etc.) and no one actually did.
This is a problem that needs to be resolved in future versions of the compiler (no compiler handles it well yet, that I know of).&quot;

This came from a chap called dovr on experts exchange.
So I just put the line
static Tconsole_script<string> does_nothing;
under the #include in &quot;ftp_console.cpp&quot; & everything worked out fine.
Man, I wish I could give myself a star!

Douglas JL

Common sense is what tells you the world is flat.
 
> Man, I wish I could give myself a star!

just ask for it !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top