template class default constructor problem

This is a discussion on template class default constructor problem within the C++ Programming forums, part of the General Programming Boards category; I get an error: request for member `setPairX' in `anotherPoint', which is of non-class type `Pair<float, int> ()()' which I ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    99

    template class default constructor problem

    I get an error:
    request for member `setPairX' in `anotherPoint', which is of non-class type `Pair<float, int> ()()'
    which I really do not understand... I only suspect there is something wrong with the default constructor, but I have no clue what.
    I have my template in .h file (all declarations and definitions). The part I'm concerned is as follows:
    Code:
    template< class T1, class T2 > 
    class Pair {
              public:
                     Pair();
                     Pair(const T1 &x, const T2 &y) : first(x), second(y) {}
                 // ~Pair();
                     void setPairX(const T1 &x);
                     void setPairY(const T2 &y);
                     T1 getPairX() const { return first; }  //inline def
                     T2 getPairY() const { return second; } // inline def
                 // void swapPair();
              private:
                      T1 first;
                      T2 second;
    };
    
    //definitions for the template
    template< class T1, class T2 >
    Pair< T1, T2 >::Pair()
    {
          first = 0;
          second = 0;
    }
    
    
    template< class T1, class T2 >
    void Pair< T1, T2 >::setPairX(const T1 &x)
    {
         first = x;
         
    }
    
    template< class T1, class T2 >
    void Pair< T1, T2 >::setPairY(const T2 &y)
    {
         second = y;
        
    }
    If I call parametrized constructor, everything works fine. But when I call the default one, I get the compiler error I quoted.
    That's how I call the default constructor in main:
    Code:
        Pair <float, int> anotherPoint();
        anotherPoint.setPairX(1.05);
        cout << anotherPoint.getPairX() << " is first in anotherPoint." << endl;
        anotherPoint.setPairY(4);
        cout << anotherPoint.getPairY() << " is second in anotherPoint." << endl;
    Can you tell me what I'm doing wrong?

    Also: maybe a stupid question. Do I need to define the destructor? If yes, how?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Can you tell me what I'm doing wrong?
    You declared a function here:

    Pair <float, int> anotherPoint();

    caled anotherPoint, which returns a Pair<> and has no parameters. Then you tried to access the function's member variable here:

    anotherPoint.setPairX(1.05);

    and functions don't have member variables because they aren't classes. So the error message says:

    request for member `setPairX' in `anotherPoint',
    where

    [anotherPoint] is of non-class type `Pair<float, int> ()()'
    The syntax informs you that anotherPoint is a function: the rightmost (), that returns a Pair<> and takes no parameters: the other ().

    But when I call the default one
    Crack open your beginning C++ text and examine the syntax for calling the default constructor of a class.
    Last edited by 7stud; 04-22-2006 at 11:04 AM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    It is a common mistake to add parenthesis when declaring a variable, but that isn't allowed (it declares a function, as 7stud said). So remove the parentheses from the anotherPoint variable declaration.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Oh....... Thanks Daved. I knew it is something so trivial...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template class nested in template class
    By yahn in forum C++ Programming
    Replies: 7
    Last Post: 04-18-2009, 11:40 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. help with template class using a template node
    By aciarlillo in forum C++ Programming
    Replies: 11
    Last Post: 06-02-2005, 05:46 PM
  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. Operator overloading in template classes
    By moejams in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2003, 05:16 PM

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