Thread: more template woes...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    552

    more template woes...

    its been a long time since i wrote a template, but i dont remeber them giving me this kind of trouble...

    simple code, but wont work:
    Code:
    //main.cpp
    #include "tt.h"
    
    int main()
    {
      outer<int> out();
      out.f((int)10);
      return 0;
    }
    
    //tt.h
    #include <iostream>
    using namespace std;
    
    template <class T>
    class outer {
      T k;
      public:
        outer(){}
        void f(T n) { cout << n << endl; }
    };
    cxx give me:
    Error: main.cpp, line 6: left operand of the "." operator must have a
    class type
    ....out.f((int)10);
    -----^

    gcc gives me:
    main.cpp: In function `int main()':
    main.cpp:6: request for member `f' in `out', which is of non-aggregate type `out
    er<int> ()()'

    maybe ive been looking at code to long and am missing the obvious, but i just dont see any problems
    C Code. C Code Run. Run Code Run... Please!

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

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    nevermind. it was those damned parentheses. ( out() )
    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
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    okay... this is a simplified test case of the problem i had in the previous thread... It works on cxx, gcc but not on MSVC6.0

    Code:
    //tt.h
    #include <iostream>
    using namespace std;
    
    template <class T>
    class outer {
      T k;
      public:
        outer(){}
        void f(T n) { cout << n << endl; }
        class inner {
          public:
            inner(){}
            void f(T n);
        };
    };
    
    template <class T>
    void outer<T>::inner::f(T n) { cout << n << endl; }   
    
    //main.cpp
    #include "tt.h"
    
    int main()
    {
      outer<int> out;
      out.f((int)10);
      class outer<int>::inner in;
      in.f(11);
      return 0;
    }
    I get an unresolved external error for inner::f(...)
    Anyone know whats up?
    C Code. C Code Run. Run Code Run... Please!

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

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You shouldn't be trying to create external "inner's". The whole idea of nested classes is to clean up the host class's interface.
    Notice that you can do this:

    //...
    outer(){inner<T> in;}
    //...

    Seems like you should separate the two in this case...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    I would except they have to be like this (prof said so) and since the inners are iterators for the class it makes sense to keep them in (like the iterators in STL)
    C Code. C Code Run. Run Code Run... Please!

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

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    BUG: LNK2001 on Member Function When Use Nested Class Template
    Last reviewed: July 24, 1997
    Article ID: Q128789
    The information in this article applies to:
    The Microsoft C/C++ Compiler (CL.EXE) included with: - Microsoft Visual C++, 32-bit Edition, versions 2.0, 2.1, 4.0, 4.1,

    4.2, 5.0




    SYMPTOMS
    When you build a program that uses nested class templates, the following link error is generated:


    test.obj: error LNK2001: unresolved external symbol
    "?Funtion@BB@?$AA@HH@@QAEHXZ
    ( public: int __thiscall AA<int,int>::BB::Funtion(void) )"


    CAUSE
    The compiler does not generate code for the member function in the nested class template.



    RESOLUTION
    To work around the problem, use one of the following suggestions:

    Define the function in the class declaration as an inline function. Be sure to define the function body in the class declaration. Defining the function as an inline function outside the class declaration will not eliminate the problem.
    -or-

    Use the member function specialization technique to work around the problem. This technique is demonstrated in the "Sample Code" section of this article.

    STATUS
    Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.



    MORE INFORMATION
    The following code can be used to reproduce or work around the problem.

    Code:
    Sample Code
    
       /* Compile options needed: None.
       */
    
       #include <stdio.h>
    
       template <class T1, class T2>
       class AA
       {
         public:
           class BB
           {
             public:
               int Function();
    
       // Work around #1, replace the previous line with
       //        int Function() { return 0; }
           };
       };
    
       // Work around #2, uncomment the following function
       // AA<int,int>::BB::Function()
       // {
       //   return 0;
       // }
    
       template <class T1, class T2>
       int AA<T1,T2>::BB::Function()
       {
          return 0;
       }
    
       void main()
       {
         AA<int,int>::BB b;
         printf( "%d\n", b.Function() );
       }

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    Thats a relief, I was starting to suspect it. I guess we can add version 6.0 to the list of affected versions. Thanks alot, youve saved me a lot of time and stress.
    C Code. C Code Run. Run Code Run... Please!

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

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    552
    woe is me...
    I have 3000 lines of code to debug and this damned bug is in my way... my only hope is to debug on cxx over telnet *shudders*
    woe is me...
    C Code. C Code Run. Run Code Run... Please!

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. template woes
    By rippascal in forum C++ Programming
    Replies: 6
    Last Post: 03-25-2002, 11:16 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM