CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Sep 2001
    Location
    Germany
    Posts
    22

    templating a class member function!

    Im trying to write a template member function but something goes wrong. I get this compiler error:

    error C2275: "MY_STRUCT" : illegal use of this type as an expression

    Can anybody help me?

    ////////////////////////////////////////////////////////////////////////
    // MyClass.h

    typedef struct
    {
    int n;
    long l;
    } MY_STRUCT;

    class MyClass
    {
    public:
    template <typename T> int GetSizeOf(T myStruct);
    MyClass();
    virtual ~MyClass();
    };
    ///////////////////////////////////////////////////////////////////////////

    ///////////////////////////////////////////////////////////////////////////
    // MyClass.cpp

    MyClass::MyClass()
    {
    }

    MyClass::~MyClass()
    {
    }

    template <typename T> int MyClass::GetSizeOf(T myStruct)
    {
    return sizeof(T);
    }
    ////////////////////////////////////////////////////////////////////////////

    ////////////////////////////////////////////////////////////////////////////
    // Main.cpp

    void main ()
    {
    MyClass TestObj;
    MY_STRUCT myStruct;

    int nSize = TestObj.GetSizeOf<MY_STRUCT>(myStruct);

    return;
    }

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    1) You can't put the definition of GetSizeOf in the .cpp file - it has to go in the header.

    2) Why have you made it a class member? The implementation you've given (apart from resolving to sizeof) would be better off as a free function.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Sep 2001
    Location
    Germany
    Posts
    22
    Actually the sample I've posted used to be just an example of a problem. The reason I made this function a class member is that I have to use some member variables in it.

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Fair enough, but point 1 is still valid.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  5. #5
    Join Date
    Sep 2001
    Location
    Germany
    Posts
    22
    Well, I've put the definition of 'GetSizeOf' function in the header file, unfortunately I'm still getting the same compiler error if I try using it in main function. Thank you anyway...

  6. #6
    Join Date
    Jun 2002
    Posts
    224
    It looks like a compiler bug. Are you using VC++?
    ZDF

    What is good is twice as good if it's simple.
    "Make it simple" is a complex task.

  7. #7
    Join Date
    Jun 2002
    Posts
    224
    Yes, it is a bug! Works fine with Borland 5.5 and g++ 2.95.3-5.

    Regards,
    ZDF

    What is good is twice as good if it's simple.
    "Make it simple" is a complex task.

  8. #8
    Join Date
    Sep 2001
    Location
    Germany
    Posts
    22
    Yes. I'm using VC++6.0.

  9. #9
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Ah, well. VC++ is seriously brain-damaged when it comes to template member functions.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  10. #10
    Join Date
    Jun 2002
    Location
    Sweden
    Posts
    81
    Actually the compiler deduces the type of GetSize() by itself. If you call GetSize() without the explicit template-argument you'll be fine!

  11. #11
    Join Date
    Sep 2001
    Location
    Germany
    Posts
    22
    Maybe I didn't get it right. Do you mean something like this:

    MyClass TestObj;
    MY_STRUCT myStruct;
    myStruct.l = 1;
    myStruct.n = 2;

    int nSize = TestObj.GetSizeOf(myStruct);


    I get linker error:
    unresolved external symbol "public: int __thiscall MyClass::GetSizeOf(struct MY_STRUCT)"

  12. #12
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    As Graham said, "You can't put the definition of GetSizeOf in the .cpp file - it has to go in the header."

    This is where your unresolved is coming from.

    Jeff

  13. #13
    Join Date
    Sep 2001
    Location
    Germany
    Posts
    22
    Thanks to all, this solved my problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured