Thread: Template class constructor

  1. #1
    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
    28,413
    The problem is that this declares a function named A that takes no arguments and returns an I1<int>:
    Code:
    I1<int> A();
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    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, 05: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