Thread: template method

  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
    28,413
    This:
    Code:
    someClassImp printLines();
    declares a function named printLines that takes no arguments and returns a someClassImp.
    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

  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
    28,413
    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.
    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

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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, 12:54 PM

Tags for this Thread