Search code examples
visual-c++c++-climanaged-c++

C++/CLI virtual method, can't use the virtual method in derived class


Okei, i'm quit new to C++/CLI so this should be an easy question. I am trying to use a virtual method from an abstract class.

Here is my code:

// Figurer.h

#pragma once

using namespace System;

namespace Figurer {

public ref class Figur
{
public:
    virtual double areal();
    virtual double omkrets();
};

public ref class Sirkel : public Figur
{
private:
    double radius;
    double static PI = 3.141593;
public:
    Sirkel(double sirkelradius){
        radius = sirkelradius;
    }
    double areal(){
        return radius * radius * PI;
    }

};
}

Visual studio tells me: Error: 'new' or 'override' is required because this declaration matches function "Figurer::Figur::areal"


Solution

  • You posted the error message and the solution:

    Error: 'new' or 'override' is required because this declaration matches function "Figurer::Figur::areal"
    

    So you either need to use new or override in front of the function signature:

    virtual double areal() override  {
        return radius * radius * PI;
    }
    

    See also: http://msdn.microsoft.com/en-us/library/41w3sh1c.aspx