C++ C++;将父方法作为派生对象调用时出错 #包括 #包括 使用名称空间std; 班级家长 { int x=0; 公众: int getx() { 返回x; } }; 类子:公共父类 { int x=7; }; int main() { 母猫;

C++ C++;将父方法作为派生对象调用时出错 #包括 #包括 使用名称空间std; 班级家长 { int x=0; 公众: int getx() { 返回x; } }; 类子:公共父类 { int x=7; }; int main() { 母猫;,c++,methods,parent,derived,C++,Methods,Parent,Derived,C++;将父方法作为派生对象调用时出错 #包括 #包括 使用名称空间std; 班级家长 { int x=0; 公众: int getx() { 返回x; } }; 类子:公共父类 { int x=7; }; int main() { 母猫; 在你发布你的Stack和LinkedList的代码以及你所说的“停止工作”之前,难道没有办法回答这个问题吗我编辑了这篇文章,展示了一个与我目前遇到的问题非常相似的例子。对我来说似乎很好-好多了!我现在没有答案给你,但我相信其他人会接受。你说的

C++;将父方法作为派生对象调用时出错
#包括
#包括
使用名称空间std;
班级家长
{
int x=0;
公众:
int getx()
{
返回x;
}
};
类子:公共父类
{
int x=7;
};
int main()
{
母猫;

在你发布你的
Stack
LinkedList
的代码以及你所说的“停止工作”之前,难道没有办法回答这个问题吗我编辑了这篇文章,展示了一个与我目前遇到的问题非常相似的例子。对我来说似乎很好-好多了!我现在没有答案给你,但我相信其他人会接受。你说的“停止工作”是什么意思?
#include <iostream>
#include <string>
using namespace std;

class parent
{
    int x=0;
    public:
    int getx()
    {
        return x;
    }
};

class child : public parent
{
    int x=7;
};
int main()
{
  parent cat;
  cout << cat.getx();
  child s;
  cout << s.getx();    //stops working here
  return 0;
}
#include <iostream>
#include <string>

class parent
{
protected:
    int m_x;
public:
    parent(int x = 0) : m_x(x) {}
    int getx(){return m_x;}
};
class child : public parent
{
public:
    child():parent(7){}
};
int main()
{
  parent cat;
  std::cout << cat.getx() << std::endl;

  child s;
  std::cout << s.getx() << std::endl;

  return 0;
}
dgs@dhome:~/Develop/Test2$ make getx
g++     getx.cpp   -o getx
dgs@dhome:~/Develop/Test2$ ./getx
0
7