473,757 Members | 9,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>* _systemstiffnes smatrix;
....
};

} //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::Abstrac tSolver::Abstra ctSolver(class fesolv::Model
*)" (??0AbstractSol ver@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 1830
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>* _systemstiffnes smatrix;
...
};

} //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<typena me T> Matrix<T>::Matr ix(int);
is not implemented ...
(??0?$Matrix@N@ femath@@QAE@H@Z ) referenced in function "public:
__thiscall fesolv::Abstrac tSolver::Abstra ctSolver(class fesolv::Model
*)" (??0AbstractSol ver@fesolv@@QAE @PAVModel@1@@Z)


.... and it is called from the constructor of fesolv::Abstrac tSolver.

Regards, Stephan

Jan 14 '06 #2
That is clear. The constructor is called but not found. So why?
template<class T> Matrix<T>::Matr ix(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>::Matr ix(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>* systemstiffness matrix_;
};
}

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(declaratio n 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<d ouble> DoubleVector;

void LinearSolver::C omputeSystemRea ctionVector()
{

Matrix<double>* tempStiffness = new Matrix<double>;
...
DoubleVector* uglob = new DoubleVector(0. 0,dimension);
...
(*_reactionforc e) = (*tempStiffness )*(*uglob);

delete uglob;
delete tempStiffness;
}
############### ######
Error message:
LinearSolver.ob j : error LNK2019: unresolved external symbol "class
std::valarray<d ouble> __cdecl femath::operato r*(class
femath::Matrix< double> const &,class std::valarray<d ouble> const &)"
(??Dfemath@@YA? AV?$valarray@N@ std@@ABV?$Matri x@N@0@ABV12@@Z) referenced
in function "private: void __thiscall
fesolv::LinearS olver::ComputeS ystemReactionVe ctor(void)"
(?ComputeSystem ReactionVector@ LinearSolver@fe solv@@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
2580
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, and the error I get: -------------- namespace LIST { template <typename T> struct Link; template <typename T> class List;
3
3939
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. template<class Type> class base { public: base& operator/=(const base&); Type *image;
1
1990
by: ranges22 | last post by:
****************************************************************** I am compiling a librarry which has a .h file containing th following: ****************************************************************** template<typename T> void from_string(const char Str, T &Obj); template<> void from_string(const char Str, long &); // template<> void from_string(const char Str, unsigned lon &); //
2
2019
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. This makes it difficult to spot errors. For example, F4 to "jump to next error" just jumps ot the next "template assistance" message. And since there are hundreds of them, this is quite obnoxious. Can I disable these things? Why is MSVC...
2
1978
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 "transform" which works not only on lists, but lists of list, lists of lists of lists, etc. I'm calling it "fmap" and it passes around actual lists instead of iterators (for now). In order to get it to work, I thought I'd have one templated...
2
3343
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 class B has an instance of template class A, which has some base type T (usually int or double). However, the base type T is important to calculations in B, so I would like to obtain access to the type for further variable declaration within B. ...
3
3758
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 thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out. I built a console app using Borland C++ Builder 5. The linker complained of references to...
8
5316
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
7698
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
5772
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 compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template classes. One is List1, the other one is ListNode. The codes are below: // test-linked-list.cpp :...
0
9489
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9298
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10072
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9906
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8737
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5172
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.