Template class constructor

This is a discussion on Template class constructor within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; template <class T> class I1 { public: T i; I1(T a = 0): i(a){} ...

  1. #1
    DL1
    DL1 is offline
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99

    Template class constructor

    Code:
    #include <iostream>
    using namespace std;
    
    template <class T> class I1
    
    {
        public:
        T i;
        I1(T a = 0): i(a){}
        ~I1(){}
    };
    
    
    int main()
    {
        I1<int> A();
        cout << A.i << endl;
        return 0;
    }
    Hello again. There's something wrong with the constructor in the above program. The compiler gives the following message w.r.t. to the third line from bottom "error: request for member `i' in `A', which is of non-class type".

    I can't see what the problem is. T is int, and both a and i are of type T, so surely i is an int. Any suggestions?

    Also, a more general question. I have been learning C++ in my spare time for 4 or 5 months now, by working my way through the Stroustrup book. It went fine until I got to chapter 14 (templates). Now my brain seems to have gone on strike and nothing seems to make any sense any more. Is this normal??? (Fortunately, the next chapter, which deals with exceptions, is a piece of cake in comparison.)

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
        I1<int> A();
    Declares a function called A, which returns a I1<int> type. Remove the (), and you get what you want.

    Just one of those things in C++.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    The problem is that this declares a function named A that takes no arguments and returns an I1<int>:
    Code:
    I1<int> A();
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    DL1
    DL1 is offline
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    Of course. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Declare a template class as a friend?
    By AH_Tze in forum C++ Programming
    Replies: 11
    Last Post: 05-19-2004, 09:24 PM
  5. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM

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