Why doesn't the following compile under VC2003?

Why does the following code snippet not compile in Visual C++ 2003 It compiles under GCC, so why not in VC2003. What is wrong with it // Outter class: class Outter { public: // First inner class: template class Inner1 {}; // Second Inner class: class Inner2 { public: // Method definition: Outter* Inner1() {} }; };

Answer this question

Why doesn't the following compile under VC2003?

  • developer

    Sure - well, I assumed that the site had stripped the tags. Why would you use the template keyword and not specify a template, I assume this is indeed an error.

    So no compiler accepts this then



  • ssebring

    He's probably using an older version of g++ that didn't catch this syntax error (using the template keyword w/o template parameters).

    Brian


  • Heriberto Menendez

    Mozilla did that to me too: nuke the linebreaks.  Here it is again (over-compensation courtesy of MSDN Forums.)

    class Outter

    {

    public:

    // First inner class:

    template class Inner1

    {

    };

    // Second Inner class:

    class Inner2

    {

    public:

    // Method definition:

    Outter* Inner1()

    {

    }

    };

    };

    Both VS2005 and GCC 3.4.4 give errors on this code, albeit they are worded differently:

    Microsoft:

    1>e:\tempprojects\frank8\frank8\frank8.cpp(7) : error C3413: 'Outter::Inner1' : invalid explicit instantiation

    1>e:\tempprojects\frank8\frank8\frank8.cpp(8) : error C2946: explicit instantiation; 'Outter::Inner1' is not a template-class specialization

    GCC:

    frank8.cpp:6: error: expected `<' before "class"

    The problem is that

    template class Inner1

    doesn't make sense since it lacks template parameters.  e.g.

    template<class T> class Inner1

    Brian

     

     


  • JayBhanaut

    Hi, Nektar.

    Both VS6 and early GCC compilers were quite underpar on C++ conformance, especially with template code.  If you are trying to compile code that is incorrect w.r.t. the C++ standard, then your only choice is to fix those errors because you can't force the compiler to accept code with syntax errors.  Compilers operate in a world of exactness.

    I sympathize with you having to sprinkle typename all over the place.  I've been through it myself, and it does seem like an odd thing to have to do.  But the C++ standard has added this to deal with problematic ambiguities in code using templates.

    It's not clear what you're asking of this forum to help you in your goal of compiling your large code base.  If you are confused about a compiler error, could you repost the code (make it readable, please ) along with the error

    Thanks,

    Brian

     


  • newgreen

    I did not write template class Inner1 I wrote template class Inner1 I guess that this horrible forum software thought that the < and > were some kind of malitious html tags and stripped them off. Well here it is again. I looks ok to me: the code I mean. The issue is that this is a part of a larger 3rd party project that I cannot really mess with a lot. The code was much more complicated but this to where I managed to boil down the error to. In the actual program the Outter class contained more inner class etc. You are right though that I am using an older version of GCC so the lack of error messages there might be understandable. The thing is that I need to compile the code fast and I don't really care if the compiler finds that issue or not, I simply want to compile this 3rd party program with the minimal set of changes required to make it work under Visual Studio. I have already spent a lot of time fixing stupid errors and adding typename in all the place for example. The code also might compile under VS6 but I don't use that to check it out. The error is: Debugger.cpp(16): error C2955: 'Outter::Inner1' : use of class template requires template argument list And the line where the error occurs is not this one template class Inner1 But this one: // Method definition: Outter* Inner1() So it seems that the compiler confuses the Inner1 class with the Inner1 method or something like this. Thanks. // Outter class: class Outter { public: // First inner class: template class Inner1 {}; // Second Inner class: class Inner2 { public: // Method definition: Outter* Inner1() {} }; };
  • rickkeller

    I am sorry about that. I was copying and pasting code from Notepad. I will write it by hand placing < and > in place of less than and greater than symbols. class Outter{ public: // First inner class: template<class T> class Inner1{ }; // Second inner class: class Inner2{ public: // Method declaration: Outter Inner1(){} }; }; The error given by VS2003 is: Debugger.cpp(16): error C2955: 'Outter::Inner1' : use of class template requires template argument list And is on this line: // Method definition: Outter* Inner1() So it seems that the compiler confuses the method definition with the previous class deffinition having the same name (Inner1). But why should it I cannot change the code easily since there are many places using these classes and the Inner2::Inner1 method. Plus why shouldn't it compile
  • Yicheng

    Could you space it so that it's readable please And what errors do you get



  • Aluri

    Well, it compiles fine under VC2005. And it compiles fine under VC2003, here.

    // Method declaration: Outter Inner1(){}

    // Method definition: Outter* Inner1()

    Those two lines are different, which one is it If I add the *, then it compiles in 2005 and not 2003. So I would guess that this means that it's a bug, fixed in VC2005.

    You can use search and replace to find/change the Inner1 method calls to GetInner1, or similar. I thought perhaps adding a parameter to the method might fix it ( and give it a default value ), but it doesn't help. It looks like your options are VC2005, or change the method.



  • Wes123g

    It will probably compile under VC6. VC6 would compile just about anything...

    Having a method and class with the same name is obviously able to cause confusion, can't you change it

    I think if you check the 'this post contains a code sample' down the bottom, you may keep your tags. As it stands, the forum probably supports using HTML inline, your tags are probably being swallowed by the browser.

    Perhaps you could try to post the code again, and if all else fails, press the HTML button to make sure you have ampersandLT; instead of the less than sign, and so on, in your post.



  • Why doesn't the following compile under VC2003?