2

Hi all, I have a compilation error I can't seem to get through.

Here's what I do: I declare an object of class2, and I call its function "function2". This function, in turn, declares an object of class1 and calls its function "function1". Right now, this code gets a compilation error at that point ("function1" can't be properly called):

error: expected primary-expression before ‘)’ token

However, if I uncomment the useless "function1", then the code compiles. I find this confusing, because this is not the function being called, and should not affect at all.

#include <iostream>
using namespace std;

template<int parameter1>
class class1 {
    public:

    template < int parameter2 > void function1() {
        cout << "We do useful things here" << endl;
    }
};

template < int parameter3 >
class class2 {
    public:

    //template < char a, char b > bool function1() {
    //    cout << "Useless definition (?)" << endl;
    //}

    void function2() {
        class1 < parameter3 > an_instance_of_class1;
        an_instance_of_class1.function1 < 999 > ();
    }
};

int main(int argc, char** argv) {
    class2 < 99 > an_instance_of_class2;
    an_instance_of_class2.function2();
}

Does anyone know what I'm missing? Thanks in advance.

Compiler version:

$ g++ --version
g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3

1 Answers1

6

You need to use .template:

an_instance_of_class1.template function1 < 999 > ();

Look at the accepted answer here for more details.

Community
  • 1
  • 1
arnoo
  • 501
  • 2
  • 9
  • Good catch. Everyone remembers `typename`, but not as many `template`. – chris Aug 24 '12 at 10:27
  • Thanks for the short answer, and for the guide also. – user1620254 Aug 24 '12 at 11:33
  • this is a common phenomenon when porting code from Visual Studio to g++/clang. Visual Studio will happily compile code missing `.template` parts, and similarly for code calling dependent base class members it will happily accept missing `this->` parts. – TemplateRex Aug 24 '12 at 13:33