Я пытаюсь вызвать функцию insertBack (newVal) из контейнера. cpp внутри Stack. cpp файла, но Компилятор выдает ошибку: идентификатор "insertBack" не определен. Кроме того, попытка сделать то же самое для переменной n , которая находится в том же файле, но в закрытой области - ошибка та же: идентификатор "n" не определен.
Я впервые делаю сборку и наследование нескольких файлов.
Stack. cpp
#include "pch.h"
#include "Stack.h"
#include "container.h"
Stack::Stack()
{
container();
}
bool Stack::push(int newVal)
{
return insertBack(newVal);
}
void Stack::pop()
{
--n;
}
container.h
#ifndef CONTAINER_H
#define CONTAINER_H
#pragma once
#include <iostream>
class container
{
public:
container();
// Postcondition: data member n is initialized to -1 and all elements in the empty arr array are initialized to zero
bool insertBack(int val);
// Precondition: the container object is not full
// Postcondition: if arr array is not full, n is incremented by 1; returns true with val is inserted at the end of the arr array
// Otherwise, returns false; the value is not inserted and program execution continues.
private:
static const int CAPACITY = 10; // physical size of the arr array or the storage capacity of a container object
int arr[CAPACITY]; // arr array can store up to CAPACITY (10 in our case) integers
int n; // n is used as the subscript for the arr array. n is initialized to -1 for an empty array
// Each time a new value is inserted into the arr array, n must first be incremented
// by 1. Since n has been initialized to -1, the first inserted value is stored in arr[0],
// and the 2nd inserted value will be in arr[1], etc. and the nth inserted value will be
// stored in arr[n – 1]. Obviously, n + 1 represents the actual number of elements
// stored in the array after n rounds of insertion.
};
#endif
Stack.h
#ifndef STACK_H
#define STACK_H
#pragma once
#include <iostream>
class Stack
{
public:
Stack();
// Postcondition: an empty stack object is created (with n initialized to -1 as in P1)
bool push(int newVal);
// Postcondition: if the stack is full, returns false. Otherwise, returns true; also the newVal is pushed onto the stack
// and the stack size is incremented by 1
void pop();
// Precondition: the Stack must not be empty or this function will not be called
// Postcondition: the object (an integer value) on top of the Stack is logically popped or deleted, 3) and Stack size is decremented by 1
};
#endif