473,467 Members | 1,430 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Calling of template function

105 New Member
Hi all,

I have a template function implemented in test.cpp.

test.h
Expand|Select|Wrap|Line Numbers
  1. class test
  2. {
  3. template<typename T>
  4. void function1( );
  5. }
  6.  
  7. test.cpp
  8. {
  9. template<typename T>
  10. test::function1( )
  11. {
  12. statements
  13. }
  14.  
  15. }
Now I have my main program

main.cpp
Expand|Select|Wrap|Line Numbers
  1. #include"test.h"
  2.  
  3. main( )
  4. {
  5. test object1;
  6. object1.function1<float>( );
  7. }
  8.  
When I compile this rogram I get the linking error

test.obj : error LNK2001: unresolved external symbol "public: void __thiscall test::function1<float>(void)" (??$function1@M@test@@QAEXXZ)

If at all I call this function on the topmost of main.cpp instead of in the class and call it without any object I get no errors at all.

I am not able to identify the problem.Am I calling the template function in a wrong way?

Could anyone help me with this?

Thanks in advance.
Aug 25 '07 #1
9 2246
weaknessforcats
9,208 Recognized Expert Moderator Expert
First, this won't compile as you are missing a semi-colon at the end of the class declaration.
Second, your method is private.
Third, the return type of the method in the class is different from the return type of the method definition.
Fourth, main() has no return type specified. The default is int and that is not supported in C++. main() returns an explicit int.
Fifth, there are various extra braces scattered about.

Other than that, it shoukld compile.
Aug 26 '07 #2
RRick
463 Recognized Expert Contributor
Also, be careful of what compiler you are using. If you're using gnu g++, you'll have to put all of the code into the .h header file.

In this case, be sure to add inline to methods defined outside of the class definition.
Aug 26 '07 #3
gsi
51 New Member
Hi,
I guess irrespective of the compiler used, it is not possible to seperate the interface and implementation in case when templated class or member functions are used. The source file has to have both the declaration and definition (In this case both test.h and test.cpp must be in main.cpp or test.h itself must have all the definitions and be included in main.cpp). The compiler generates code for the templates only when they are explicitly instantiated, hence it is not possible to seperately compile test.cpp and main.cpp(without the prototype definitions) and link them together .

Please advice if misleading.

Thanks,
gsi.
Aug 26 '07 #4
mickey22
105 New Member
the code I have wriiten just as an example..To clear the things, I have declared the function as public and main also I have specified as

int main( )

I am compiling test.cpp and main.cpp together at the same time.

I am not able to undestand how to call my my template function in the main using the object..I have to give my template T as float.My guess is the way I am calling template function is something wrong in it.

Could you please suggest any way.

Thanks.
Aug 27 '07 #5
mickey22
105 New Member
I just tried including the template definition inside test.h instead of in the test.cpp.I don't get any linker errors when I call in the same way using the object.
But including the definition in the header file is not optimal solution.Is there any other way of doing this?

Thanks
Aug 27 '07 #6
sicarie
4,677 Recognized Expert Moderator Specialist
mickey22-

You're an established enough user to know that you need to follow the Posting Guidelines, so I'm issuing a formal warning - please conform to the Posting Guidelines from now on.

Thanks,

sicarie
Aug 27 '07 #7
weaknessforcats
9,208 Recognized Expert Moderator Expert
I guess irrespective of the compiler used, it is not possible to seperate the interface and implementation in case when templated class or member functions are used.
Separating the interface from the implementation has nothing to do with where the code is located.

This only applies to polymorphic hierarchies. That is, base classes with virtual functions. In this case the interface is the set of public non-virtual methods. The implementation is separated because these base class functions are not overriden by derived classes. Generally, the public non-virtual base class methods call a private virtual base class method for the implementation. It is this function that is overriden by a private virtual function in the derived class.

Remember, the base class method is the interface and the virtual base class method is the implementation. You do not want these to be the same function.

Check out the design pattern called Template Method.

Textbooks and teachers notwithstanding, public virtual functions in a polymorphic base class are a big no-no.
Aug 27 '07 #8
RRick
463 Recognized Expert Contributor
Keeping the class definition in the same file as the declaration only applies to templates and not to your typical base/derived class.

The GNU compiler generates all of the template code at compiler time. This is why all of the template code needs to be in the same file. Other compilers allow you to separate template code into header and source code files. Check with your compiler for the specific rules and details.

My understanding is that resolving templates during linkage is not a trivial process, and for sume reason GNU has decided not to tackle this problem.
Aug 27 '07 #9
weaknessforcats
9,208 Recognized Expert Moderator Expert
My understanding is that resolving templates during linkage is not a trivial process, and for sume reason GNU has decided not to tackle this problem.
Neither has Microsoft.
Aug 28 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Andrew Wilkinson | last post by:
Hi, First off I know that in almost all cases this would be a terrible thing to do, but this is an unusual case where this makes sense. Basically I have a procedure where you pass a string...
3
by: ogtindeed | last post by:
Hi all, I need some help. I am trying to instantiate a Template and call a member function, but have not been able - Now this is what I am trying to do: foo.h =========
10
by: jim.brown | last post by:
Please refer me to the right place if this is the wrong place to post this question. I'm looking for an example of calling the Eigenvalue routines of JAMA from a C++ program. The documentation...
6
by: komal | last post by:
hi all basically my problem is i have to write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function...
6
by: Eric Lilja | last post by:
Hello, I have a std::vector storing pointers to an abstract base class OptionBase. OptionBase is not templated but I am deriving a template class called Option from OptionBase. The class Option has...
13
by: john.constantine | last post by:
Hi I have this code: template<typename ClassType> struct S { template<typename FunctionType> void member() {}; };
8
by: Plissken.s | last post by:
I have a template function which print out the content of a list: The following compiles: void PrintInt2 (int i) { cout << i << " "; } template <class T> void printList(T& list) {
5
by: Jeff Newman | last post by:
Hi all, I am trying to figure out what is causing the compile error in the following example. I have two functions which are identical (except one is templated), both trying to call a template...
6
by: Ole Nielsby | last post by:
VC has a __cdecl specifier which allows functions and methods to be called with varying parameter count. (I understand this is the default for functions in general but in VC, instances use...
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:
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
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,...
1
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,...
0
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...
0
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...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.