C2555 error in VS C++

I received error C2555 when trying to compile my VS 2005 C++ code. I found the following reference and it said under STATUS it has been fixed in Visual C++ .NET version.

http://support.microsoft.com/kb/240862/

Should I not be getting this error anymore or am I missing something I am running

Microsoft Visual Studio 2005 Version 8.0.50727.42, .Net 2.0.50727, Visual C++ 2005.



Answer this question

C2555 error in VS C++

  • danych

    Thank you both for the help.

    I have seen code snippet where the template argument OptionType of the base template does not even have to be specified when declaring the derived template, like below. Is this just a compiler-dependent thing or something else

    template <class OptionType>
    class OptionFactoryTemplate
    {
    public:
    OptionFactoryTemplate(){};
    virtual ~OptionFactoryTemplate(){};
    virtual OptionType *CreateOption(const std::string OptionChoice_)=0;
    };

    template <class OptionType, class NewOption>
    class OptionFactory : public OptionFactoryTemplate
    {
    public:
    OptionFactory(){};
    virtual ~OptionFactory(){};
    virtual OptionType *CreateOption(const std::string OptionChoice_);
    };


  • Kevin Dente

    Here is the link to an example of such code snippet. The "Factory 6" example is what I am talking about. Notice Factory template declaration. Perhaps other compilers will tolerate this type of syntax

    http://www.codeproject.com/cpp/all_kinds_of_factories.asp


  • kmbarz

    I reproduced this error and can see why you are expecting this to work. 

    Change

    class OptionFactory : public OptionFactoryTemplate<class OptionType>

    to

    class OptionFactory : public OptionFactoryTemplate<OptionType>

    The Comeau compiler complains in a similar way, but it also catches the class OptionType.  Maybe the VC compiler should do the same thing.

    Here's a simplification of the code that manifests the problem.

    class X
    {
    };

    class Y : public X
    {
    };

    template <class A>
    class IBar
    {
    public:
      
    virtual A* test() = 0;
    };

    template <class A, class B>
    class Foo : public IBar<class A> // remove class to avoid C2555
    {
    public:
      
    virtual A* test() { return new B(); }
    };

    Foo<X,Y> foo;

     


  • admkca

    That's also ill-formed code. In VC8 (VS2005) you get the error:

    error C2955: 'OptionFactoryTemplate' : use of class template requires template argument list


  • bryanedds

    We can't really tell if it is expected or not to get such error without seeing the code leading to it. According to http://msdn2.microsoft.com/en-US/library/ke3es672.aspx, there could be casses that would trigger such error.

    Thanks,
    Ayman Shoukry
    VC++ Team


  • Larry Charlton

    VC6 was rather lax when it came to template parameters.  It inferred too much about template parameters, and your example may be one of those cases where it relied on VC6. 

    But VC6 was horribly non-C++ conformant.

    Well, not that horribly.  Depends on your POV.


  • bshive

    Strictly speaking this code is ill-formed. If I use the EDG compiler I get the following error message:

    "tt9.cpp", line 17: warning: template parameter "OptionType" may not be used
    in an elaborated type specifier
    class OptionFactory : public OptionFactoryTemplate<class OptionType>

    Unfortunately the Visual C++ compiler swallows this and happily assumes that you are forward declaring a class called OptionType - I am not sure in which scope it is adding this new class but where ever it is it is before the scope for the template parameters so that name lookup finds this class before the template parameter with the same name and the compiler then uses this as the return type for the virtual function - which leads to the latter error message about the return types being different - which they are: one is the template parameter and the other is the forward declared class.

    Not that it completely helps in this case but this is one reason why I always declare template type parameters using typename instead of class. [Note: I have actually met users who sincerely believe that if you declare a template type parameter with class then it is constrained to be a user-defined type while if you use typename it can be any type].



  • Belias

    Ok here is the code. Please let me know if you see what is wrong. I have highlighted the virtual functions in green and the part of the code I think is generating the error, in red.

    //OptionFactory.h

    #ifndef OPTIONFACTORY_
    #define OPTIONFACTORY_

    #include <string>

    template <class OptionType>
    class OptionFactoryTemplate
    {
    public:
    OptionFactoryTemplate(){};
    virtual ~OptionFactoryTemplate(){};
    virtual OptionType* CreateOption()=0;
    };

    template <class OptionType, class NewOption>
    class OptionFactory : public OptionFactoryTemplate<class OptionType>
    {
    public:
    OptionFactory(){};
    virtual ~OptionFactory(){};
    virtual OptionType* CreateOption(){return new NewOption;};
    };

    #endif

    //BaseOption.h
    //Define attributes and functions of a base option

    #ifndef BASEOPTION_
    #define BASEOPTION_

    #include <list>
    #include <BaseAsset.h>
    #include <OptionFactory.h>

    class BaseOption
    {
    private:
    double Strike, Expiration, RiskFreeRate, Price, Delta, Gamma, Vega, Theta;
    public:
    BaseOption(); //Constructor
    virtual ~BaseOption(){}; //Virtual destructor
    typedef OptionFactoryTemplate<BaseOption> BaseOptionFactory; //Baseoption factory
    };

    #endif

    //VanillaOptions.h

    #ifndef VANILLAOPTIONS_
    #define VANILLAOPTIONS_

    #include <BaseOption.h>
    #include <OptionFactory.h>

    class EuropeanCall : public BaseOption
    {
    private:
    BaseAsset UAsset; //Underlying asset
    public:
    EuropeanCall();
    ~EuropeanCall(){};
    static OptionFactory<BaseOption, EuropeanCall> MyFactory;
    virtual BaseAsset GetUAsset() const;
    /*virtual double CalPrice(const std::list<double>& randlist,
    const double NumOfPath_) const;*/
    };

    OptionFactory<BaseOption, EuropeanCall> EuropeanCall::MyFactory;

    #endif


  • C2555 error in VS C++