template method

This is a discussion on template method within the C++ Programming forums, part of the General Programming Boards category; I have a class that has a template method in it. I inherit from the class and then implement the ...

  1. #1
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926

    template method

    I have a class that has a template method in it. I inherit from the class and then implement the abstract methods. However, when I call the constructor it'll give an error. If I remove the () it won't give an error and run fine.
    Code:
    #include "BaseClass.hpp"
    
    class someClassImp : public BaseClass{
      public:
       void func1(){
          std::cout << "hey" << std::endl;
       }
    
       void func2(){
          std::cout << "hey again" << std::endl;
       }
    };
    
    int main(void) {
       someClassImp printLines; //THIS WORKS FINE
      //someClassImp printLines(); //WILL GIVE ERROR BELOW
       printLines.templateMethod();
       return 0;
    }
    error:
    Code:
    main.cpp: In function ‘int main(void)’:
    error: request for member ‘templateMethod' in ‘printLines’, which is of non-class type ‘someClassImp ()()’
    Why does that make a difference, and is this the correct way to implement a template method [without the () on the constructor]?
    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    This:
    Code:
    someClassImp printLines();
    declares a function named printLines that takes no arguments and returns a someClassImp.
    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

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Thanks, but I still am wondering why it doesn't recognize it as a default constructor that takes no arguments? Is it better to do what I have done and remove the parentheses or do
    Code:
    someClassImp *printLines = new someClassImp();
    printLines->templateMethod();

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    Thanks, but I still am wondering why it doesn't recognize it as a default constructor that takes no arguments?
    Because it is recognised as a function declaration.
    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

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by laserlight View Post
    Because it is recognised as a function declaration.
    Which in turn, is because the C++ grammar is ambiguous. It could really be interpreted either way. Obviously it can't be both at once, so the standard writers just picked one.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    That's why I NEVER use () when declaring a variable with no parameters.
    If you follow that rule, you'll never run into this problem ever again.
    Code:
    std::string str;
    // instead of
    std::string str();
    ...

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. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

Tags for this Thread


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