在派生 class 中使用提取运算符 (>>) 的方法以及在 C++ 中处理静态数据成员的方法

Ways to use extraction operator (>>) in derived class and ways to handle static data members in C++

我的计划有以下目标:

下面是我的代码:

#include <iostream>

using namespace std;

class Base {
private:
    string name;
    int rollNum;
public:
    friend std::istream& operator>>(std::istream& in, Base &base);
};

std::istream& operator>>(std::istream& in, Base &base) {
    string name;
    int rollNum;
    cout << "Enter name: ";
    in >> base.name;
    cout << "Enter roll no: ";
    in >> base.rollNum;
    return in;
}

class Derived : public Base {
private:
    int myNumOne;
    static int x;
    static int y;
public:
    friend std::istream& operator>>(std::istream& in, Derived Derived);
    void add();
    static void init();
    static int getX();
    static int getY();
};

std::istream& operator>>(std::istream & in, Derived derived) {
    in >> static_cast<Derived> (derived);
    cout << "Enter a number: ";
    in >> derived.myNumOne;
    return in;
}

static void Derived::init() {
    x = 100;
    y = 100;
}   

static int Derived::getX() {
    return x;
}

static int Derived::getY() {
    return y;
}

void Derived::add() {
    myNumOne += getX() + getY();
    cout << "Number = " << myNumOne << endl;
}

int main() {
    Derived obj;
    cin>>obj;
    Derived objOne;
    objOne.add();
    Derived objTwo;
    objTwo.add();
    return 0;
}

我收到如下错误:

newmain.cpp:43:31: error: cannot declare member function 'static void Derived::init()' to have static linkage [-fpermissive]
     static void Derived::init() {
                               ^
newmain.cpp:48:30: error: cannot declare member function 'static int Derived::getX()' to have static linkage [-fpermissive]
     static int Derived::getX() {
                              ^
newmain.cpp:52:30: error: cannot declare member function 'static int Derived::getY()' to have static linkage [-fpermissive]
     static int Derived::getY() {
                              ^

在实现时删除 static 关键字,只在声明时需要它:

void Derived::init() {
    x = 100;
    y = 100;
}   

您还需要像这样实现静态 xy

int Derived::x = 0;
int Derived::y = 0;

此外,operator>> 必须引用该对象,因为它将对其进行修改。那么这应该可以工作:

std::istream& operator>>(std::istream & in, Derived& derived) 
{
    Base& base = derived;
    in >> base;
    cout << "Enter a number: ";
    in >> derived.myNumOne;
    return in;
}

最后,这会编译,但可能不会达到您的预期。您可以修改运算符>>:

#include <iostream>

using namespace std;

class Base {
private:
    string name;
    int rollNum;
public:
    friend std::istream& operator>>(std::istream& in, Base &base);
};

std::istream& operator>>(std::istream& in, Base &base) {
    string name;
    int rollNum;
    cout << "Enter name: ";
    in >> base.name;
    cout << "Enter roll no: ";
    in >> base.rollNum;
    return in;
}

class Derived : public Base {
private:
    int myNumOne;
    static int x;
    static int y;
public:
    friend std::istream& operator>>(std::istream& in, Derived& Derived);
    void add();
    static void init();
    static int getX();
    static int getY();
};

int Derived::x = 0;
int Derived::y = 0;

std::istream& operator>>(std::istream & in, Derived& derived) 
{
    Base& base = derived;
    in >> base;
    cout << "Enter a number: ";
    in >> derived.myNumOne;
    return in;
}

void Derived::init() {
    x = 100;
    y = 100;
}   

int Derived::getX() {
    return x;
}

int Derived::getY() {
    return y;
}

void Derived::add() {
    myNumOne += getX() + getY();
    cout << "Number = " << myNumOne << endl;
}

int main() {
    Derived obj;
    cin>>obj;
    Derived objOne;
    objOne.add();
    Derived objTwo;
    objTwo.add();
    return 0;
}

不确定您是否真的想要 xy 静态。为什么它们 Derived 对象没有属性?