在第二個類中,我只想將數字添加到字串,并且收到錯誤“main.cpp:38:19: error: 'virtual void NumericInput::add(char)' is private in this context 38 | input- >添加('1');' 每次我為 Numeric 物件添加類時。我在這里做錯了什么,不是所有的東西都已經公開了嗎?謝謝!
#include <string>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
using namespace std;
class TextInput
{
public:
string s="";
virtual void add(char c)
{
s =c;
}
string getValue()
{
return s;
}
};
class NumericInput : public TextInput
{
//modified
void add(char c)
{
if(isdigit(c))
{
s =c;
}
}
};
int main()
{
NumericInput* input = new NumericInput();
input->add('1');
input->add('a');
input->add('0');
cout<<input->getValue();
}
uj5u.com熱心網友回復:
如果沒有公共訪問說明符,則類中的所有宣告默認都是私有的。您還應該覆寫 NumericInput 中的虛函式。
uj5u.com熱心網友回復:
在 NumericInput 之前添加 public
class NumericInput : public TextInput
{
public:
//modified
void add(char c)
{
if(isdigit(c))
{
s =c;
}
}
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/379552.html
上一篇:動態實體化鏈接的物件鏈
