質問

I have some problem. I created program with dynamic stack and when I compiling get errors: 'class stos' has no member named 'push' , 'class stos' has no member named 'pop' , 'class stos' has no member named 'destroy' , 'class stos' has no member named 'isempty'. In language C program is fine, but in C++ not. I have started my adventure with C++ and I don't have idea what's wrong I do. Can you help me ? I'm putting a code:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <iostream>
#include <conio.h>

using namespace std;

class stos
{
public:
int *tab;
int licznik;
int rozmiar;

};

void init (class stos* s)
{
s->licznik = 0;
s->rozmiar = 3;
//s->tab = (int*) malloc ( 2 * sizeof * s->tab );
s->tab = (int*) malloc ( s->rozmiar * sizeof(int) );
if (s->tab == NULL)
    {
    cout << "Blad alokacji pamieci\n";
    abort ();
    }
}

void push (class stos* s, int element) 
{
if (s->licznik != s->rozmiar)
    {
    cout << "Dodaj element do stosu\n";
    cin >> element;
    s->tab[s->licznik] = element;
    s->licznik++;
    }
else
{
    s->rozmiar = 2 * (s->rozmiar);
    s->tab = (int*) realloc (s->tab, (s->rozmiar) * sizeof (int));
    cout << "Zwiekszono pamiec\n";
    cout <<"Dodaj element do stosu\n";
    cin >> element;
    s->tab[s->licznik] = element;
    s->licznik++;
}
//system ("clear");
}

int pop (class stos* s) 
{
if (s->licznik == 0)
{
    cout << "Nie podales zadnego elementu\n";
    abort ();
}
else
{
    s->licznik--;
}
return s->tab[s->licznik];
}

void destroy (class stos* s)
{

  free (s->tab);

}

bool isempty (class stos* s)
{   
bool empty = true;

if (s->licznik == 0)
    {
  empty = true;
  cout << "Stos nie posiada zadnych elementow\n";
    }
else
    {
  empty = false;
  cout << "Stos zawiera jakies elementy\n";
    }
return empty;
}

int main () 
{
int element = 0, a = 0;
 stos stos1;
 stos stos2;


    do
    {
    cout << "\nMENU\n\n";
    cout << "STOS I\nAby dodac element do stosu wcisnij '1'\nAby odjac element ze stosu wcisnij '2'\n";
    cout << "Wcisnij '3' aby usunac stos\nWcisnij '4' aby sprawdzic czy stos zawiera elementy\n\n";
    cout << "STOS II\nAby dodac element do stosu wcisnij '5'\nAby odjac element ze stosu wcisnij '6'\n";
    cout << "Wcisnij '7' aby usunac stos\nWcisnij '8' aby sprawdzic czy stos zawiera elementy\n\n";
    cout << "Wcisnij '0' aby zakonczyc dzialanie programu\n\n";
    cin >> a;
    //system ("clear");

    if (a == 1)
    {
        stos1.push (element); 
    }
    if (a == 2)
    {
        cout << "Usunieto element:" << stos1.pop;

    }
    /*if (a == 3)
    {
        destroy (&stos1);
        printf ("Wyczyszczono stos 1\n");
    }*/
    if (a == 4)
    {
        stos1.isempty;
    }
    if (a == 5)
    {
        stos2.push (element);
    }
    if (a == 6)
    {
        cout << "Usunieto element: " << stos2.pop;

    }
    /*if (a == 7)
    {
        destroy (&stos2);
        printf("Wyczyszczono stos 2\n");
    }*/

    if (a == 8)
    {
        stos2.isempty;
    }
}
while (a!=0);
    destroy (&stos1);
    destroy (&stos2);
getch();
return 0;
}
役に立ちましたか?

解決

That is because the functions stos, push, etc. are free functions, but you call them like methods. You need to call them this way:

stos s;
init(&s)
push(&s, 3)

Or better, you can convert the functions to methods:

class stos {
public:
    stos() // constructor instead of init
    {
        // access members tab, etc. directly
        licznik = 0;
        // ...
    }

    void push(int element)
    {
    }

    // ...
private:
    int *tab;
    int licznik;
    int rozmiar;
};

他のヒント

The problem is that "class stos has no member named push" and so on. push and pop are non-member functions. It looks like you need something of the form

push (&stos, element); 

and

pop(&stos1);

In your code you declare the class stos. The class only contains some fields.

Later in your code you define the functions

void push (class stos* s, int element)
int pop (class stos* s)
bool isempty (class stos* s)

Basically this is correct. You could just create an instance of the class stos and then call one of the functions and pass the stos instance as an argument of the function. In this case you need to make the function call like this:

push (stos1, element);

instead of:

stos1.push (element);

The other way is as others may already have suggested is to add the functions to your class. In that case your class definition would look like this:

class stos
{
public:
int *tab;
int licznik;
int rozmiar;

void push (int element)
int pop ()
bool isempty ()
};  

If you create the class like this you can leave the function calls in your main as they are now.

If you want to have member functions you have to write them for instance push could be written like this

class stos
{
public:
void push (int element);
int *tab;
int licznik;
int rozmiar;

};

void stos::push (int element) 
{
if (licznik != rozmiar)
    {
    cout << "Dodaj element do stosu\n";
    cin >> element;
    tab[licznik] = element;
    licznik++;
    }
else
{
    rozmiar = 2 * (rozmiar);
    tab = (int*) realloc (tab, (rozmiar) * sizeof (int));
    cout << "Zwiekszono pamiec\n";
    cout <<"Dodaj element do stosu\n";
    cin >> element;
    tab[licznik] = element;
    licznik++;
}
//system ("clear");
}

This looks like code which has been translated from C, but you have only gone half way.

Just giving hint for the "pop" problem:

Make it a method of class stos:

class stos
{
public:
int *tab;
int licznik;
int rozmiar;
int pop( void );
};

Implement the method:

int stos::pop (void ) 
{
    if (licznik == 0)
    {
        cout << "Nie podales zadnego elementu\n";
        abort ();
    }
    else
    {
        licznik--;
    }
    return tab[licznik];
}

But you should really learn more about class before going further

The functions you are trying to call are not in class stos You could do something like this:

class stos
{
public:
int *tab;
int licznik;
int rozmiar;

void init ();
void push (int element);
int pop ();
void destroy ();
bool isempty ()

};

void stos:: init ()
{
this->licznik = 0;
this->rozmiar = 3;
//this->tab = (int*) malloc ( 2 * sizeof * this->tab );
this->tab = (int*) malloc ( this->rozmiar * sizeof(int) );
if (this->tab == NULL)
    {
    cout << "Blad alokacji pamieci\n";
    abort ();
    }
}

void stos:: push (int element) 
{
if (this->licznik != this->rozmiar)
    {
    cout << "Dodaj element do stosu\n";
    cin >> element;
    this->tab[this->licznik] = element;
    this->licznik++;
    }
else
{
    this->rozmiar = 2 * (this->rozmiar);
    this->tab = (int*) realloc (this->tab, (this->rozmiar) * sizeof (int));
    cout << "Zwiekszono pamiec\n";
    cout <<"Dodaj element do stosu\n";
    cin >> element;
    this->tab[this->licznik] = element;
    this->licznik++;
}
//system ("clear");
}

int stos:: pop () 
{
if (this->licznik == 0)
{
    cout << "Nie podales zadnego elementu\n";
    abort ();
}
else
{
    this->licznik--;
}
return this->tab[this->licznik];
}

void stos:: destroy ()
{

  free (this->tab);

}

bool stos:: isempty ()
{   
bool empty = true;

if (this->licznik == 0)
    {
  empty = true;
  cout << "Stos nie posiada zadnych elementow\n";
    }
else
    {
  empty = false;
  cout << "Stos zawiera jakies elementy\n";
    }
return empty;
}

Class stos has no one of these member functions, looking into your code I think that you've two possible solutions. Or you add it to class stos, something like:

class stos
{
public:
int *tab;
int licznik;
int rozmiar;

void init ()
{
s->licznik = 0;
s->rozmiar = 3;
//s->tab = (int*) malloc ( 2 * sizeof * s->tab );
s->tab = (int*) malloc ( s->rozmiar * sizeof(int) );
if (s->tab == NULL)
    {
    cout << "Blad alokacji pamieci\n";
    abort ();
    }
}

void push (int element) 
{
if (s->licznik != s->rozmiar)
    {
    cout << "Dodaj element do stosu\n";
    cin >> element;
    s->tab[s->licznik] = element;
    s->licznik++;
    }
else
{
    s->rozmiar = 2 * (s->rozmiar);
    s->tab = (int*) realloc (s->tab, (s->rozmiar) * sizeof (int));
    cout << "Zwiekszono pamiec\n";
    cout <<"Dodaj element do stosu\n";
    cin >> element;
    s->tab[s->licznik] = element;
    s->licznik++;
}
//system ("clear");
}
...
};

or you call them in the right way:

push(&stos1, element);

instead of

stos1.push(element);

On the other hand, if you decide not to include them into stos class, please, consider using references instead of pointers:

void push (class stos& s, int element);

Use reference wherever you can, pointers wherever you must. ;-)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top