Function Templates

This is a discussion on Function Templates within the C++ Programming forums, part of the General Programming Boards category; Code: template <typename OldType, typename NewType> any & ChangeType() { placeholder * newcontent = new holder<NewType>(reinterpret_cast<NewType>(any_cast<OldType>(*this))); std::swap(content,newcontent); delete newcontent; return ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Function Templates

    Code:
    		template <typename OldType, typename NewType>
    		any & ChangeType()
    		{
    			placeholder * newcontent = new holder<NewType>(reinterpret_cast<NewType>(any_cast<OldType>(*this)));
    			std::swap(content,newcontent);
    			delete newcontent;
    			return *this;
    		}
    Thats part of my class.

    here's my main:

    Code:
    #include <any.hpp>
    int main()
    {
             blah.ChangeType<int,char>();
    }
    And it says that it doesn't expect the int...

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    It seems that you are trying to declare a member function of a class as a template. Ive never seen this done myself, so this is just a guess, but you might try to template the entire class if possible, or declare your member function as static, or just overload the function instead of templating it.
    C Code. C Code Run. Run Code Run... Please!

    "Love is like a blackhole, you fall into it... then you get ripped apart"

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Actually, I think your problem is in main. What is blah? I don't know, and my guess is that your compiler doesn't know either.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    lol I forgot the put the rest of the code I was using... I only wanted to give the related lines.

    there is a line like this:


    "any blah = 1;"

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I've had a similar problem before... it seems that my compiler doesn't like function templates a whole lot inside classes.

    I tried doing it without prototyping and it worked last time... i.e. I get no errors if I make them pass the types, but thats not what I want.

    Why won't VC++ 6.0 let me use a template this way? I know technically it should work...
    Last edited by Trauts; 05-02-2003 at 06:07 PM.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Found a workaround using friend. thx anyway

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Brand new to C need favor
    By dontknowc in forum C Programming
    Replies: 5
    Last Post: 09-21-2007, 11:08 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21