[C++] Help using Templates menu

Shout-Out

User Tag List

Results 1 to 2 of 2
  1. #1
    Amrok's Avatar Banned
    Reputation
    4
    Join Date
    Apr 2009
    Posts
    59
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [C++] Help using Templates

    Hi, i'd like to use a Template as a placeholder for a argument type. Like:
    function(int arg1, T arg2)

    my header file looks as following:

    Code:
    class MyClass
    {
    	public:
    		template <typename T>
    		bool MyFunction(int arg1, T arg2);
    }
    this is my class.cpp code;

    Code:
    template <typename T>
    bool MyClass::MyFunction(int arg1, T arg2)
    {
    	if (arg1 > arg2)
    		return true;
    	else
    		return false;
    }
    i call this function as following:

    Code:
    MyClass MyClass; //create object
    MyClass.MyFunction<float>(int arg1, float arg2);
    but it simply wont work..

    Can somebody help me please?
    How can i implement this if i have more than one source file...?

    This is the error output...
    Code:
    1>main.obj : error LNK2001: unresolved external symbol ""public: bool __thiscall MyClass::MyFunction<float>(unsigned int,float)" (??$MyFunction@M@MyClass@@QAE_NIIIIIM@Z)".
    EDIT: It won't even work if i define all members as 'inline'...

    EDIT2: Yes i know it works if i put the whole code into the header file.. but lulz .. thats not clean
    Last edited by Amrok; 07-19-2011 at 03:17 PM.

    [C++] Help using Templates
  2. #2
    Cypher's Avatar Kynox's Sister's Pimp
    Reputation
    1356
    Join Date
    Apr 2006
    Posts
    5,368
    Thanks G/R
    0/4
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't put an implementation of a template in a .cpp file if it's going to be called from other translation units, because the compiler needs to be able to see the implementation at the time the template is instantiated, it can't be deferred to link time*. You need to put the implementation in your header file, then you won't get errors about unresolved externals.

    * Nitpick corner: Yes, there is the 'export' keyword, but nobody actually implements it except Comeau, and it's deprecated in C++0x anyway for that reason.

Similar Threads

  1. Need help using money
    By lol@exploits in forum WoW Scams Help
    Replies: 0
    Last Post: 05-25-2008, 03:13 AM
  2. [Help] Sales Template not there?
    By MisterEMU in forum World of Warcraft Emulator Servers
    Replies: 7
    Last Post: 05-19-2008, 06:18 PM
  3. [Help] Using different .m2's per spell, not with file swapping?
    By Seuldieu in forum WoW ME Questions and Requests
    Replies: 2
    Last Post: 05-05-2008, 06:03 PM
  4. [Need help] using Chrispee's Repack .
    By Merso88 in forum World of Warcraft Emulator Servers
    Replies: 1
    Last Post: 11-12-2007, 10:13 AM
  5. Help-Using Drop MPQ on mac, it won't compress.
    By Nightz12 in forum WoW ME Questions and Requests
    Replies: 1
    Last Post: 09-13-2007, 10:56 PM
All times are GMT -5. The time now is 09:53 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search