473,657 Members | 2,554 Online
Bytes | Software Development & Data Engineering Community
+ 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 2257
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(withou t 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
2156
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 containting a template for a tuple, the function then finds a tuple that matches this template and then returns it. The template may contain variable names, which match against anything in that position in the tuple - this is all working fine,...
3
1376
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
3203
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 says that the is the public method: Eigenvalue (const TNT::Array2D< Real > &A) I write this program
6
1965
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 parameter.and no of parameter is calling function can be anything. for example.suppose my function is function2. then when i call function1(int i ,char j,float d) { function2()
6
1579
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 a public member function called get_value() that is not present in the class OptionBase. My problem is that when calling get_value() I have to cast to Option<some_type>* and I was wondering if I can somehow remove the cast so I dont have to...
13
10672
by: john.constantine | last post by:
Hi I have this code: template<typename ClassType> struct S { template<typename FunctionType> void member() {}; };
8
3055
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
1601
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 function in another class. The template function (func2) won't compile. I'm hoping that I'm just missing something simple, but I can figure out what it is. struct A { template<typename T>
6
11570
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 another convention unless they have an ellipsis argument.) I can force GCC and other compilers to use such a convention by declaring all methods with an ellipsis, but I'd rather not clutter my method definitions with these.
0
8392
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
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7321
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...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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
4151
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
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1607
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.