Mein C++
Code ist wie folgt:Kann ich nur eine Methode in Erben überschreiben?
#include<iostream>
using namespace std;
class A
{
public:
virtual void f(int i)
{
cout << "A's f(int)!" << endl;
}
void f(int i, int j)
{
cout << "A's f(int, int)!" << endl;
}
};
class B : public A
{
public:
virtual void f(int i)
{
cout << "B's f(int)!" << endl;
}
};
int main()
{
B b;
b.f(1,2);
return 0;
}
während der Kompilierung ich:
g++ -std=c++11 file.cpp
file.cpp: In function ‘int main()’:
file.cpp:29:9: error: no matching function for call to ‘B::f(int, int)’
file.cpp:29:9: note: candidate is:
file.cpp:20:16: note: virtual void B::f(int)
file.cpp:20:16: note: candidate expects 1 argument, 2 provided
Wenn ich Überschreibung nach B f (int) verwenden versucht, ich den gleichen Fehler habe.
Ist es in C++ möglich, nur eine Methode zu überschreiben? Ich habe nach einem Codebeispiel mit override
gesucht, das auf meinem Computer kompiliert wird und noch keinen gefunden hat.
möglich Duplikat von [Virtuelle Methode verursacht Kompilierungsfehler in der abgeleiteten Klasse] (http://stackoverflow.com/questions/7274723/virtual-method-causes-compilation-error-in-derived-class) –
Nein, sorry, Das war ein anderes Problem. –
mögliches Duplikat von [C++: Begründung hinter der Regel] (http://stackoverflow.com/questions/4837399/c-rationale-behind-hiding-rule) – Angew