【问题标题】:Getting inheritance issues when trying to compile尝试编译时遇到继承问题
【发布时间】:2021-09-24 23:34:56
【问题描述】:

我有一个名为 stackTester 的类,它试图从另一个类 stackofChars 继承。我试图在stackTester中使用的stackofChars中定义的方法都是虚拟的,但是当我尝试在stackTester中使用它们时,我得到一个错误

请求非类类型的成员

这是我的 stackofChars.h 文件:

#define STACK_OF_CHARS_H
#include "node.h"

class stackofChars
{
    private:
    node* m_top;
    
    public:
    //constructor for the stack, takes in no paramaters
    stackofChars();
    
    //copy constructor, takes in a referance to the original stack
    stackofChars(const stackofChars& orig);
    
    //destructor for the stack, no parameters
    ~stackofChars();
    
    //destructor for the copy, takes in a referance to the copy
    void operator=(const stackofChars& rhs);
    
    //pushes the stack back and creates a new node at the stop, takes in an entry, returns nothing
    virtual void push(char entry);
    
    //deletes the top entry and pushes the stack up, takes in nothing, returns nothing
    virtual void pop();

    //peeks at the top entry, takes in nothing, returns a char, const because nothing is changed
    virtual char peek() const;
    
    //checks if the stack is empty, takes in no parameters, returns nothing, const because nothing is changed
    virtual bool isEmpty() const;
};
#endif

这是我的 stackTester.h 文件:

#ifndef STACK_TESTER_H
#define STACK_TESTER_H
#include "stackofChars.h"

class stackTester : public stackofChars
{   
    public: 

    stackTester();

    //This will call all your test methods
    void runTests();
     
    private:

    //Creates an empty stack and verifies isEmpty() returns true
    void test1();

    //Creates an empty stack pushes 1 value, verifies isEmpty() returns false
    void test2();

    //Creates an empty stack, then pushes once, pops once, and verifies isEmpty returns true
    void test3();

    //more test methods as needed
};
#endif

如果需要,这是我的 stackTester.cpp 文件:

#include "stackTester.h"
#include "stackofChars.h"
#include <iostream>

void stackTester::test1()
{
    stackofChars test();
    std::cout << "Test#1: Newly created stack is empty: ";
    if(test.isEmpty() == true)
    {
        std::cout << "Pass\n";
    }
    else
    {
        std::cout << "Fail\n";
    }
}

void stackTester::test2()
{
    stackofChars test();
    test.push(???);
    std::cout << "Test#2: Push on empty stack makes it non-empty: ";
    if(test.isEmpty() == true)
    {
        std::cout << "Pass\n";
    }
    else
    {
        std::cout << "Fail\n";
    }
}

void stackTester::test3()
{
    stackofChars test();
    test.push(???);
    test.pop();
    std::cout << "Test#3: Popping all elements makes stack empty: ";
    if(test.isEmpty() == true)
    {
        std::cout << "Pass\n";
    }
    else
    {
        std::cout << "Fail\n";
    }
}

谁能告诉我为什么会出现这个错误?

【问题讨论】:

    标签: stack class-hierarchy


    【解决方案1】:

    stackofChars test();是错误的。声明类对象的正确方式应该是:stackofChars test;stackofChars test{}; 这里有解释enter link description here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-18
      • 1970-01-01
      • 2020-01-05
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多