473,395 Members | 1,986 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

template in error

Hi folks,

I have a linker - problem using a Matrix-template.
Maybe someone can help me with that.

Q:
"using femath::Matrix" or "using femath::Matrix<double>";

Consider this:

##########################
namespace femath {
....
template<class T> class Matrix
{
public:
Matrix();
Matrix(int rows);
Matrix(int rows, int columns);
virtual ~Matrix();
....
};
} //end namespace

namespace fesolv {

using femath::Matrix;

class AbstractSolver
{
public:
AbstractSolver(Model* model);
virtual ~AbstractSolver();

public:
virtual void Solve()=0;
....
protected:
Model* _model;
Matrix<double>* _systemstiffnessmatrix;
....
};

} //end namespace

##########################

Everything compiles great but the linker tells me:
AbstractSolver.obj : error LNK2019: unresolved external symbol "public:
__thiscall femath::Matrix<double>::Matrix<double>(int)"
(??0?$Matrix@N@femath@@QAE@H@Z) referenced in function "public:
__thiscall fesolv::AbstractSolver::AbstractSolver(class fesolv::Model
*)" (??0AbstractSolver@fesolv@@QAE@PAVModel@1@@Z)
......

I'm using VC++2005 and there is no external *.lib.
Matrix<class T> is in the same compile/link-unit.

Thanks a lot,
Sascha

Jan 14 '06 #1
9 1808
eiji wrote:
Hi folks,

I have a linker - problem using a Matrix-template.
Maybe someone can help me with that.

Q:
"using femath::Matrix" or "using femath::Matrix<double>";
using femath::Matrix;

Consider this:

##########################
namespace femath {
...
template<class T> class Matrix
{
public:
Matrix();
Matrix(int rows);
Matrix(int rows, int columns);
virtual ~Matrix();
...
};
} //end namespace

namespace fesolv {

using femath::Matrix;

class AbstractSolver
{
public:
AbstractSolver(Model* model);
virtual ~AbstractSolver();

public:
virtual void Solve()=0;
...
protected:
Model* _model;
Matrix<double>* _systemstiffnessmatrix;
...
};

} //end namespace

##########################

Everything compiles great but the linker tells me:
AbstractSolver.obj : error LNK2019: unresolved external symbol "public:
__thiscall femath::Matrix<double>::Matrix<double>(int)"
The constructor
template<typename T> Matrix<T>::Matrix(int);
is not implemented ...
(??0?$Matrix@N@femath@@QAE@H@Z) referenced in function "public:
__thiscall fesolv::AbstractSolver::AbstractSolver(class fesolv::Model
*)" (??0AbstractSolver@fesolv@@QAE@PAVModel@1@@Z)


.... and it is called from the constructor of fesolv::AbstractSolver.

Regards, Stephan

Jan 14 '06 #2
That is clear. The constructor is called but not found. So why?
template<class T> Matrix<T>::Matrix(int)
{
...
}
exists!

Is there a "template<T>" phrase missing?
What can be the problem?

Jan 14 '06 #3
TB
eiji sade:
That is clear. The constructor is called but not found. So why?
template<class T> Matrix<T>::Matrix(int)
{
...
}
exists!

But where?
Is there a "template<T>" phrase missing?
What can be the problem?


TB
Jan 14 '06 #4
I tried this:

//main.cpp
namespace test {

template<class T> class Test
{
T i;
public:
Test(T t);

};

template<class T> Test<T>::Test(T t)
{
i = t;
}

}
int main(int argc , char *argv[])
{
using namespace test;
Test<int> T(1);
return 0;
}
#######

Compiles and links without problems!

but handling it this way:
#######
//a.h
namespace test {

template<class T> class Test
{
T i;
public:
Test(T t);

};
}
#####
//a.cpp
#include "a.h"
namespace test {

template<class T> Test<T>::Test(T t)
{
i = t;
}

}
//main.cpp
#include "a.h"
int main(int argc , char *argv[])
{
using namespace test;
Test<int> T(1);
return 0;
}

same error, the constructor is not found!
Any important linker-options?

Sorry, any suggestions?
Sascha

Jan 14 '06 #5
TB
eiji sade:
I tried this: <snip> but handling it this way:
#######
//a.h
namespace test {

template<class T> class Test
{
T i;
public:
Test(T t);

};
}
#####
//a.cpp
#include "a.h"
namespace test {

template<class T> Test<T>::Test(T t)
{
i = t;
}

} <snip>
same error, the constructor is not found!
Any important linker-options?

Sorry, any suggestions?
Sascha


Compiler limitations. The template declarations and definitions must
be in the same translation unit.

Other solutions:
* Read about support for the 'export'-keyword for you compiler.
* #include the cpp-file after the declarations in the header-file.

TB
Jan 14 '06 #6
eiji wrote:
Hi folks,

I have a linker - problem using a Matrix-template.
Maybe someone can help me with that.

class Model;

namespace femath {
template<class T> class Matrix
{
public:
Matrix();
Matrix(int rows);
Matrix(int rows, int columns);
virtual ~Matrix();
};
} //end namespace

namespace fesolv {

using femath::Matrix;

class AbstractSolver
{
public:
AbstractSolver(Model* model);
virtual ~AbstractSolver();

public:
virtual void Solve()=0;
protected:
Model* model_;
Matrix<double>* systemstiffnessmatrix_;
};
}

int main(int argc, char* argv[])
{
}

Works for me.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Jan 14 '06 #7
TB schrieb:

Compiler limitations. The template declarations and definitions must
be in the same translation unit.

Other solutions:
* Read about support for the 'export'-keyword for you compiler.
* #include the cpp-file after the declarations in the header-file.

TB


Most of the linker errors I have solved with that(declaration and
definition in the same translation unit). One more to go(link
operator). This is nearly the same problem.

Please Consider this:
#####################
namespace femath {

template<class T> class Matrix
{
public:
Matrix();
...
friend std::valarray<T> operator*(const Matrix& A, const
std::valarray<T>& b);
...
};
....
template<class T> std::valarray<T> operator*(const Matrix<T>& A, const
std::valarray<T>& b)
{
...
}
};

typedef std::valarray<double> DoubleVector;

void LinearSolver::ComputeSystemReactionVector()
{

Matrix<double>* tempStiffness = new Matrix<double>;
...
DoubleVector* uglob = new DoubleVector(0.0,dimension);
...
(*_reactionforce) = (*tempStiffness)*(*uglob);

delete uglob;
delete tempStiffness;
}
#####################
Error message:
LinearSolver.obj : error LNK2019: unresolved external symbol "class
std::valarray<double> __cdecl femath::operator*(class
femath::Matrix<double> const &,class std::valarray<double> const &)"
(??Dfemath@@YA?AV?$valarray@N@std@@ABV?$Matrix@N@0 @ABV12@@Z) referenced
in function "private: void __thiscall
fesolv::LinearSolver::ComputeSystemReactionVector( void)"
(?ComputeSystemReactionVector@LinearSolver@fesolv@ @AAEXXZ)
#####################

The code works fine before translating Matrix into a template. The
operator works as expected.
Now the operator is not found during linkage. There must be one reason
why the definition is not accepted, but the declaration does.

Regards,
Sascha

Jan 14 '06 #8
eiji wrote:
I tried this:

//main.cpp
namespace test {

template<class T> class Test
{
T i;
public:
Test(T t);

};

template<class T> Test<T>::Test(T t)
{
i = t;
}

}
int main(int argc , char *argv[])
{
using namespace test;
Test<int> T(1);
return 0;
}
#######

Compiles and links without problems!

but handling it this way:
#######
//a.h
namespace test {

template<class T> class Test
{
T i;
public:
Test(T t);

};
}
#####
//a.cpp
#include "a.h"
namespace test {

template<class T> Test<T>::Test(T t)
{
i = t;
}

}
//main.cpp
#include "a.h"
int main(int argc , char *argv[])
{
using namespace test;
Test<int> T(1);
return 0;
}

same error, the constructor is not found!
Any important linker-options?


Test is a template, so the compiler must be able to see the definition.

http://www.parashift.com/c++-faq-lit...html#faq-35.12

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Jan 14 '06 #9
> Test is a template, so the compiler must be able to see the definition.

http://www.parashift.com/c++-faq-lit...html#faq-35.12

Ben Pope


Thanks Ben, great link!
################
//pre-declare each template friend function, add <> in the friend
lines
template<class T> class Matrix;
template<class T> std::valarray<T> operator* (const Matrix<T>& A, const
std::valarray<T>& b);
template<class T> Matrix<T> operator*(const Matrix<T>& A, const
Matrix<T>& B);

template<class T> class Matrix
{
public:
...
friend std::valarray<T> operator* <> (const Matrix& A, const
std::valarray<T>& b);
friend Matrix operator* <> (const Matrix& A, const Matrix& B);
...
};
################

Works fine!
Thank you all for a lesson on template-programming :-)

Jan 14 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mat DeLong | last post by:
I have never been stuck on programming something before to the point I give up... this is a first. I am programming what should be something very easy in C++... using Templates. Here is the code,...
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
1
by: ranges22 | last post by:
****************************************************************** I am compiling a librarry which has a .h file containing th following:...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
2
by: Greg Buchholz | last post by:
/* I've been experimenting with some generic/polytypic programs, and I've stumbled on to a problem that I can't quite figure out. In the program below, I'm trying to define a generic version of...
2
by: pagekb | last post by:
Hello, I'm having some difficulty compiling template classes as containers for other template objects. Specifically, I have a hierarchy of template classes that contain each other. Template...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
8
by: Jess | last post by:
Hi, I have a template function that triggered some compiler error. The abridged version of the class and function is: #include<memory> using namespace std; template <class T>
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.