-3

Just trying to get stack.cpp's push function to increment max_size_ variable by 1.

Array.h:

template <typename T>
class Array: public BaseArray<T>
{
public:

//loads of fun code
private:
  size_t max_size_;
};

Stack.cpp:

//...even more fun code...
template <typename T>
void Stack <T>::push (T element)
{
    ArrayStack::max_size_++;

}

Stack.h:

template <typename T>
class Stack
{
public:

  Stack (void);

private:
    Array <T> ArrayStack; 
};

Error:

 error: ‘ArrayStack’ is not a class or namespace
  ArrayStack::max_size_++;

Or if I run it with just max_size_:

template <typename T>
void Stack <T>::push (T element)
{

    max_size_++;
}

error: ‘max_size_’ was not declared in this scope
  max_size_++;
cparks10
  • 377
  • 2
  • 14
  • 1
    Recommended read: https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – user0042 Oct 07 '17 at 18:37
  • You really ought to get a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Rakete1111 Oct 07 '17 at 18:38
  • The `::` is the scope operator, not the member select operator, which is `.`. You *have* seen a syntax similar to e.g. `ArrayStack.max_size` before? However that's not your only problem, because the member variable you try to access is *private*. If you add an element to the `Array` shouldn't the size be updated automatically the `Array` class itself? – Some programmer dude Oct 07 '17 at 18:39

2 Answers2

0

You actually can't do that without changing something. max_size_++; is a private member of ArrayStack, so you cannot change it from inside a Stack method.

Option 1 (better): Add a method to your ArrayStack that increments the value by one void incMaxSize() { max_size_++; } and call it instead

Option 2 (worse): Make the Stack class a friend of ArrayStack

Option 3 (even worse): Make the max_size_ variable public and then change the call to ArrayStack.max_size_++;

Daniel Trugman
  • 8,186
  • 20
  • 41
0

Since you made max_size_ private in your Array class you'll need to create a getter and setter to access it outside of that class. Then in your Stack class you can get the max_size_ value and save it in a local variable and then set it to 1 + it's current value to achieve max_size_++

Eric S
  • 1,001
  • 10
  • 14