473,396 Members | 2,009 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,396 software developers and data experts.

Instantiation of template method in template class

Ed
Hi, guys,
I declare a template method in one template class in one library.
Compiling is OK, but link is not OK.

Header File is:
<code>
template <typename P = float>
class TESTLIB_API Linear
{
public:
P x;
P y;
P z;

Linear(P x_, P y_, P z_): x(x_), y(y_), z(z_)
{
}

template <typename R>
void Distance(Linear<R>& rlinear);
};
</code>
Source CPP file is:
<code>
template <typename P>
template <typename R>
void Linear<P>::Distance(Linear<R>& rlinear)
{
x = (P) rlinear.x;
}

template class Linear<>;
template class Linear<double>;

template void Linear<float>::Distance(Linear<float>& rlinear);
template void Linear<float>::Distance(Linear<double>& rlinear);
template void Linear<double>::Distance(Linear<float>& rlinear);
template void Linear<double>::Distance(Linear<double>& rlinear);
</code>

Main.cpp is:
<code>
Linear<la(1,1,1);
Linear<lb(2,2,2);

la.Distance(lb);
</code>
Template and its implementation are put in library, and main code is
in application.

The link error is:
2>Linking...
2>LINK : warning LNK4075: ignoring '/INCREMENTAL' due to '/PROFILE'
specification
2>Ball.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/
OPT:REF' specification
2>Hello.obj : error LNK2019: unresolved external symbol "public: void
__thiscall Linear<float>::Distance<float>(class Linear<float&)" (??
$Distance@M@?$Linear@M@@QAEXAAV0@@Z) referenced in function _main
2>D:\My_Documents\Project\C++\Hello\Debug\Hello.ex e : fatal error
LNK1120: 1 unresolved externals

Actually, I explicit declare the function in source cpp file. I don't
know what's the next I can do.

Thanks,
Ed.
Aug 27 '08 #1
1 2352
Ed
On Aug 27, 2:35*pm, Ed <seah...@gmail.comwrote:
Hi, guys,
I declare a template method in one template class in one library.
Compiling is OK, but link is not OK.

Header File is:
<code>
template <typename P = float>
class TESTLIB_API Linear
{
public:
* * P x;
* * P y;
* * P z;

* * Linear(P x_, P y_, P z_): x(x_), y(y_), z(z_)
* * {
* * }

* * template <typename R>
* * void Distance(Linear<R>& rlinear);};

</code>

Source CPP file is:
<code>
template <typename P>
template <typename R>
void Linear<P>::Distance(Linear<R>& rlinear)
{
* * x = (P) rlinear.x;

}

template class Linear<>;
template class Linear<double>;

template void Linear<float>::Distance(Linear<float>& rlinear);
template void Linear<float>::Distance(Linear<double>& rlinear);
template void Linear<double>::Distance(Linear<float>& rlinear);
template void Linear<double>::Distance(Linear<double>& rlinear);
</code>

Main.cpp is:
<code>
* * Linear<la(1,1,1);
* * Linear<lb(2,2,2);

* * la.Distance(lb);
</code>

Template and its implementation are put in library, and main code is
in application.

The link error is:
2>Linking...
2>LINK : warning LNK4075: ignoring '/INCREMENTAL' due to '/PROFILE'
specification
2>Ball.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/
OPT:REF' specification
2>Hello.obj : error LNK2019: unresolved external symbol "public: void
__thiscall Linear<float>::Distance<float>(class Linear<float&)" (??
$Distance@M@?$Linear@M@@QAEXAAV0@@Z) referenced in function _main
2>D:\My_Documents\Project\C++\Hello\Debug\Hello.ex e : fatal error
LNK1120: 1 unresolved externals

Actually, I explicit declare the function in source cpp file. I don't
know what's the next I can do.

Thanks,
Ed.
Ahh... I know it.
I need to put a "dllexport" before the instantiation of template class
and template method, because they are declared in the library.

Corrected code is the following:
Source CPP file is:

template <typename P>
template <typename R>
void Linear<P>::Distance(Linear<R>& rlinear)
{
x = (P) rlinear.x;
}
template TESTLIB_API class Linear<>;
template TESTLIB_API class Linear<double>;

template TESTLIB_API void Linear<float>::Distance(Linear<float>&
rlinear);
template TESTLIB_API void Linear<float>::Distance(Linear<double>&
rlinear);
template TESTLIB_API void Linear<double>::Distance(Linear<float>&
rlinear);
template TESTLIB_API void Linear<double>::Distance(Linear<double>&
rlinear);
Aug 27 '08 #2

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

Similar topics

1
by: robc | last post by:
hi, I wrote a container template similar to that below, assuming that as long as I never called destroy() for a non-pointer instantiation the compiler would not attempt to generate that method...
5
by: Brandon Mitchell | last post by:
I have read that it is impossible to define a templated class outside of the specification file, due to a limitation in gcc's linker. I'm using gcc 3.2.3 (no switches) and would like to seperate...
6
by: Thomas Maier-Komor | last post by:
Hi everybody, I am a little bit confused with the syntax of explicit instantiation, and I am not sure that it is possible to do what I want to do. Maybe someone has an idea. Consider a...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
5
by: Hari | last post by:
Guys please help me to solve this strange problem what Iam getting as follows.. Trying to instantiate a global instance of a template class as follows :- when i build this code with debug and...
1
by: krunalbauskar | last post by:
Hi, Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. For example - <snippet> #include <iostream> #include <vector>
3
by: erictham115 | last post by:
Error C2555 c:\C++ projects\stat1\stdmatrix_adapt.h(41) : error C2555: 'std_tools::Matrix_adapter<T>::at': overriding virtual function return type differs and is not covariant from...
4
by: yuanhp_china | last post by:
I define a class in A.h: template <class Tclass A{ public: void get_elem( const T&) ; };
0
by: greek_bill | last post by:
Hi, I have a template function for which I use SFINAE to restrict one of the parameters. Then I also have a partial specialization of this function.I would like to provide an explicit...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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.