dimanche 5 janvier 2020

Error: C2228 > Left of ".getelem" must have class/struct/union, but i'm passing an array

my error is with .getelem, Visual reports error C2228. Why can't it accept an array? I know I can retrieve data without using a second function in the node class, but my teacher wrote this code himself and it's full of errors, and I need to understand.

I tried using ->, I tried giving the valori[testa-1] value to another variable and nothing changed. I think there is some error with my array initialization. By the way I couldn't find a solution on SO that could help me, because most of the time this error comes with pointers.

#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

const int MAX= 100;
using namespace std;
typedef int value;

class Node
{
public:
    Node() {};
    Node(value a)
    {
        elem = a;
    }; 
    ~Node() {}; 

    void setelem(value a)
    {
        elem = a;
    };

    value getelem() const
    {
        return elem;
    };

    bool operator==(Node& n)
    {
        return(getelem() == n.getelem());
    };

 private:
   value elem; //single atom element
 };

class Pila //stack in italian
{
friend void stampapila(Pila& L); //prints stack
public:
Pila() 
{
    creapila(); //calls a function that creates stack
};

~Pila() {};

void creapila() //creates stack
{
    testa = 0;
};

bool pilavuota() const //checks if stack is empty
{
    return testa == 0;
};

value leggipila() const //reads first element of stack
{
    return valori[testa-1].getelem(); //ERROR HERE//
};
void fuoripila(); //pop, didn't copy entire code
void inpila(value); //push, didn't copy entire code

private:
  value valori[MAX];
  int testa; //testa = head in italian
};

std::ostream& operator<<(ostream& out, Node& nodo)
 {
   return out << nodo.getelem();
 };

Aucun commentaire:

Enregistrer un commentaire