Please excuse me for bad brevity. I am not an english speaker. I was working on some simple programme and i have found some errors.
pch.h
#ifndef PCH_H
#define PCH_H
enum class TYPE
{
STACKARRAY, QUEUEARRAY, DEQUEARRAY, DUALARRAYDEQUE, UNDEFINED
};
#include <iostream>
#include <memory>
#include <utility>
#include <optional>
#include <string>
#include <assert.h>
#include "Array.h"
#include "ItemType.h"
#include "DualArrayDeque.h"
#include "ArrayStack.h"
#include "ArrayQueue.h"
#include "ArrayDeque.h"
#include "Application.h"
#endif PCH_H
If i compile these, i get error like these.
enter image description here
However somehow, if i change the #include order i get different errors.
pch.h
#ifndef PCH_H
#define PCH_H
enum class TYPE
{
STACKARRAY, QUEUEARRAY, DEQUEARRAY, DUALARRAYDEQUE, UNDEFINED
};
#include <iostream>
#include <memory>
#include <utility>
#include <optional>
#include <string>
#include <assert.h>
#include "Array.h"
#include "ItemType.h"
#include "ArrayStack.h"
#include "ArrayQueue.h"
#include "ArrayDeque.h"
#include "DualArrayDeque.h"
#include "Application.h"
#endif PCH_H
My first question is, Is there any particular order or rule to follow when creating pch files?
+)
And this is my second question. Second image tells me i can’t access to the private or protected members. Yes, clearly something is wrong. I made these codes.
DualArrayDeque.h
#pragma once
template <typename T>
class DualArrayDeque
{
private:
ArrayStack<T> front, back;
public:
DualArrayDeque();
~DualArrayDeque() {}
int GetSize() const noexcept;
std::optional<T> Get(int _i);
std::optional<T> Set(int _i, T _x);
bool Add(int _i, T _x);
std::optional<T> Delete(int _i);
void Balance();
void Print() noexcept;
};
ArrayStack.h
#pragma once
template <typename T>
class ArrayStack
{
public:
ArrayStack();
~ArrayStack();
std::optional<T> Get(int _i);
std::optional<T> Set(int _i, T _x);
bool Add(int _i, T _x);
std::optional<T> Delete(int _i);
int GetSize() const noexcept;
void Print() noexcept;
private:
void Resize();
protected:
Array<T> mArr;
int mSize;
friend class DualArrayDeque<T>;
};
DualArrayDeque.h
template <typename T>
void DualArrayDeque<T>::Balance()
{
if (3 * front.GetSize() < back.GetSize() ||
3 * back.GetSize() < front.GetSize())
{
int n(front.GetSize() + back.GetSize());
int nf(n / 2);
int nb(n - nf);
Array<T> af(std::max(2 * nf, 1));
Array<T> ab(std::max(2 * nb, 1));
for (int i = 0; i < nf; i++)
{
af[nf - i - 1] = Get(i).value();
}
for (int i = 0; i < nb; i++)
{
ab[i] = Get(nf + i).value();
}
front.mArr = af; // Can't access here.
front.mSize = nf; //
back.mArr = ab; //
back.mSize = nb; //
}
return;
}
Then again, tons of errors occur. I guess the ‘friend class DualArrayDeque’ would be the problem. I would like to access member variables of Array front, back without making any extra functions but i am not sure how to do that. I thought making friend class would be the good idea 🙁 . Is it impossible to access those member variables? Or, am i doing something wrong here?
Source: Windows Questions C++