Globalny wskaźnik typu klasowego.

0

Witam.
Potrzebuję globalny dostęp do obiektu klasy lista_p i chcę utworzyć globalny wskaźnik typu właśnie tej klasy.
Z tym że wyskakują mi błędy. Prosiłbym o jakąś poradę co może być nie tak. Pisane w Visual Studio.

 #include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <windows.h>


using namespace std;

vector<string> dane; // vector komendy

lista_p *x;  -------------------ERROR
//x = new lista_p;

//============================KOD PROCESU===================================

class proces {
protected:
	int id=0;
	int time;
	string status;
public:

	proces() {
		id++;
		cout << "Podaj czas wykonania procesu id " << id << " :"; cin >> time;
		status = "new";
	}

	//((((((((((((( WYSWIETL PARAMTERY ))))))))))))))))
	void get_id(){
		cout << id;
	}
	
	void get_time() {
		cout << time;
	};
	
	void get_status() {
		cout << status;
	}
	//((((((((((((( USTAW PARAMTERY ))))))))))))))))
	void set_time(int n) {
		time = n;
	}

	void set_status(string s) {
		status = s;
	}
	
	void set_id(int i) {
		id = i;
	}

};


//========================== LISTA PROCESOW ===============================

class lista_p {
protected:
public:
	vector<proces*> list;
	void dodaj_do_listy(proces *s)
	{
		list.push_back(s);
	}

	void new_p()
	{
		int x;
		cout << "Ile dodac procesow?"<<endl;
		cin >> x;
	
		for (int z = 0; z < x; z++)
		{
			dodaj_do_listy(new proces());
		}
			

	}

};



//============================ANALIZA KOMEND===================================

void komenda(string line)
{
	istringstream line_stream(line);
	istream_iterator<string> begin(line_stream), end;
	copy(begin, end, back_inserter(dane));


	if (dane.empty() == false) {

		if (dane[0] == "help") {
			cout << endl;
			cout << endl << "DOSTEPNE KOMENDY: " << endl << endl
				<< "add - dodawanie procesu " << endl
				<< "cls - czyszczenie konsoli" << endl;
		}

		if (dane[0] == "cls") system("cls");
		if (dane[0] == "add") {


		}

		dane.clear();
	}

}


//================================= MAIN =====================================

int main() {
	cout << "Welcome in SJF simulator 2017!" << endl;
	cout << "For more information please type: help" << endl;

	
	while (true)
	{
		
		string a;
		getline(cin, a);
		komenda(a);
	
	}

	return 0;
}
5

Próbujesz użyć typu lista_p zanim poinformowałeś kompilator o jego istnieniu. Zastosuj forward-deklarację:

class lista_p;
lista_p *x;

Przy okazji przemyśl design, zmienne globalne to prawie zawsze zło.

0

Właśnie niestety to nie działa, próbowałem nawet przy samym mainie to deklarować i nic.

0
class lista_p;
lista_p* x;
0

Ok dzięki Panowie, działa wersja od carlosmay. Generalnie nie chce mi się całego tego kodu przekształcać, więc wolę już wykorzystać te zmienną globalną i brnąć dalej.

0

Witam.
Aktualnie mam podobny problem. Zadeklarowałem na samej górze klasę jednak gdy w miejscu zaznaczonym w kodzie "ERROR" dodam wskaźnik do funkcji klasy CPU to niestety wywala błąd że klasa niezdefiniowana. Robię w Visual Studio. Proszę o radę jak to naprawić.

 #include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <windows.h>



using namespace std;

vector<string> dane; // vector komendy

class cpu;
cpu* y; //wskaźnik na licznik rozkazow
class lista_p;
lista_p* x;  // wskaźnik na klase z lista procesow (uzytkownika)

int id_s=0;  // najnowszy id procesu
bool cpu_run=false;  //informacja o uruchomieniu procesora







//============================KOD PROCESU===================================

class proces {
protected:
	int id=0;
	int time=0;
	string status="status";
public:

	proces() {
		id = id_s;
		if (id == 0) {
			time = 0;
			status = "ready";
		}
		
		else {
			cout << "Podaj czas wykonania procesu id " << id << " :      "; cin >> time;
			status = "new";
		}

		id_s++;
	}

	//((((((((((((( WYSWIETL PARAMTERY ))))))))))))))))
	int get_id(){
		return  id;
	}
	
	int  get_time() {
		return time;
	};
	
	string get_status() {
		return status;
	}
	//((((((((((((( USTAW PARAMTERY ))))))))))))))))
	void set_time(int n) {
		time = n;
	}

	void set_status(string s) {
		status = s;
	}
	
	void set_id(int i) {
		id = i;
	}

};


//========================== LISTA PROCESOW + OBSŁUGA ===============================

class lista_p {
protected:
public:
	vector<proces*> list;
	void dodaj_do_listy(proces *s)
	{
		list.push_back(s);
	}

	void new_p()													 // DODAWANIE PROCESU
	{
		int v;
		cout<<endl << "Ile dodac procesow? :  ";
		cin >> v;
		for (int z = 0; z < v; z++)
		{
			dodaj_do_listy(new proces());
		}
		y->add(); ---------------- ERROR
	}

																	// WYŚWIETLANIE LISTY PROCESOW
	void show()
	{
		
		if (x->list.empty() == true) cout << "Aktualnie brak procesow" << endl;
		else {

			cout.width(5);
			cout << endl << "ID";
			cout.width(11);
			cout << "STATUS";
			cout.width(17);
			cout << "CZAS TRWANIA" << endl;
			
			int dlugosc = x->list.size();
			for (int m = 0; m < dlugosc; m++)
			{
				cout.width(5);
				cout<< x->list[m]->get_id();
				cout.width(11);
				cout << x->list[m]->get_status();
				cout.width(17);
				cout << x -> list[m]->get_time();
				cout << endl;
			}
		}
	}
																		// USUWANIE PROCESU
	void del(int which) {
		bool found = false;
		int dlugosc = x->list.size();
		
		for (int z = 0; z < dlugosc; z++)
		{
			if (which == x->list[z]->get_id())
			{
				x->list.erase(x->list.begin() + z); 
				found = true;
				cout << endl << "Proces zostal usuniety!" << endl;
				break;
			}
		}
		if (found == false) cout << "Nie ma procesu z takim ID!" << endl;
	}


																		// ZMIANA STANU PROCESU
	void change(int id)													
	{
		int o; bool found = false; string s="nullptr";
		int dlugosc = x->list.size();

		for (int z = 0; z < dlugosc; z++)
		{
			if (id == x->list[z]->get_id())
			{
				found = true;
				cout << endl << "Jaki stan procesu chcesz ustawic?" << endl;
				cout << "1) running" << endl << "2) waiting" << endl << "3) ready" << endl << "4) terminated" << endl;
				cout << endl << "Podaj stan do ustawienia: ";
				cin >> o;

				if (id == 0 && o == 4) { cout << "Proces bezczynnosci procesora nie moze zostac zakonczony!" << endl; break; }
				if (id == 0 && o == 2) { cout << "Proces bezczynnosci procesora nie moze zmienic statusu na waiting!" << endl; break; }
				
				if (id == 0 && o == 3) {
					
					if (x->list[0]->get_status() == "ready") { cout << "Proces ten juz jest w gotowosci!" << endl; break; }
					if (x->shortest_ready() == 0) { cout << "Aktualnie brak innych procesow w gotowosci!" << endl; break; }
					if (x->shortest_ready() != 0 && x->list[0]->get_status() != "ready") {
						int b = x->shortest_position();
						x->list[b]->set_status(s = "running");
						x->list[z]->set_status(s="ready");
						break;
					}
					
				}

				if (id == 0 && o == 1) {
					if (x->list[0]->get_status() == "running") {cout << "Proces ten juz jest obslugiwany przez procesor!" << endl; break;}
					if (x -> list[0]->get_status() == "ready") {
						int p = x->run_pos(); x->list[p]->set_status(s="ready");
						x->list[z]->set_status(s="running");
						break;
					}
				}



				if (id != 0 && o == 1)
				{
					if (x->list[z]->get_status() == "terminated") { cout << "Nie mozna uruchomic procesu terminated!" << endl; break;}
					if (x->list[z]->get_status() != "ready") { cout << "Proces nie jest ready!" << endl; break; }
					if (x->list[z]->get_status() == "running") { cout << "Proces jest juz obslugiwany przez procesor!" << endl; break; }
					if (x->list[z]->get_status() == "ready") {
						int p = x->run_pos(); x->list[p]->set_status(s = "ready");
						x->list[z]->set_status(s = "running");
						break;
					}
				}



				if (id != 0 && o == 2)
				{
					if (x->list[z]->get_status() == "waiting") { cout << "Ten proces juz ma status waiting!" << endl; break; }
					if (x->list[z]->get_status() == "running") {
						int p = x->shortest_position();
						x->list[p]->set_status(s = "running");
						x->list[z]->set_status(s="waiting");
						break;
					}
	
					if (x->list[z]->get_status() == "ready") {
						x->list[z]->set_status(s = "waiting");
						break;
					}
					if (x->list[z]->get_status() == "terminated") {
						cout << "Nie mozna zmienic statusu procesowi terminated!" << endl;
						break;
					}
					if (x->list[z]->get_status() == "new") {
						x->list[z]->set_status(s = "waiting");
						break;
					}
				}


				if (id != 0 && o == 3){
					if (x->list[z]->get_status() == "ready") {
						cout << "Ten proces juz ma status ready!" << endl; break;
					}
					if (x->list[z]->get_status() == "waiting") {
						x->list[z]->set_status(s = "ready"); break;
					}
					if (x->list[z]->get_status() == "running") {
						int p = x->shortest_position();
						x->list[p]->set_status(s = "running");
						x->list[z]->set_status(s = "ready"); break;
					}
					if (x->list[z]->get_status() == "terminated") {
						cout << "Nie mozna zmienic statusu procesowi terminated!" << endl; break;
					}
					
					if (x->list[z]->get_status() == "new") {
						x->list[z]->set_status(s = "ready"); break;
					}
				}


				if (id != 0 && o == 4) {
					if (x->list[z]->get_status() == "terminated") {
						cout << "Ten proces juz jest terminated!" << endl; break;
					}
					if (x->list[z]->get_status() == "new") {
						x->list[z]->set_status(s = "terminated"); break;
					}
					if (x->list[z]->get_status() == "waiting") {
						x->list[z]->set_status(s = "terminated"); break;
					}
					if (x->list[z]->get_status() == "ready") {
						x->list[z]->set_status(s = "terminated"); break;
					}
					if (x->list[z]->get_status() == "running") {
						int p = x->shortest_position();
						x->list[p]->set_status(s = "running");
						x->list[z]->set_status(s = "terminated"); break;
					}
				}

				//if (o == 1) s = "running";
				//if (o == 2) s = "waiting";
				//if (o == 3) s = "ready";
				//if (o == 4) s = "terminated";

				//x->list[z]->set_status(s);
				break;
			
			
			
			}
		}
		
		if (found == false) cout << "Nie ma procesu z takim ID!" << endl;

	}

															// id najkrotszego procesu ready
	int shortest_ready() {
		int dlugosc = x->list.size(); int shortest_id = 0; int time = 0;

		for (int d = 1; d < dlugosc; d++)
		{
			if (time == 0 && x->list[d]->get_status() == "ready") { shortest_id = x->list[d]->get_id(); time = x->list[d]->get_time(); }
			if ((x->list[d]->get_time() < time) && (x->list[d]->get_status() == "ready")) { shortest_id = x->list[d]->get_id(); time = x->list[d]->get_time(); }
		}
		return shortest_id;
	}


															//pozycja najkrotszego procesu ready
	int shortest_position() {
		int dlugosc = x->list.size(); int shortest_pos = 0; int time = 0;
		for (int d = 1; d < dlugosc; d++)
		{
			if (time == 0 && x->list[d]->get_status() == "ready") { shortest_pos = d; time = x->list[d]->get_time(); }
			if ((x->list[d]->get_time() < time) && (x->list[d]->get_status() == "ready")) { shortest_pos = d; time = x->list[d]->get_time(); }
		}
		return shortest_pos;
	}



															//sprawdza czy istnieje proces o statusie running
	bool run_test() {
		
		int dlugosc = x->list.size();

		for (int d = 0; d < dlugosc; d++)
		{
			if (x->list[d]->get_status() == "running") { return true; break; }
		}
		return false;
	}



				
											// pozycja aktualnie uruchomionego procesu
	int run_pos() {
		int pos=0; int dlugosc = x->list.size();

		for (int d = 0; d < dlugosc; d++)
		{
			if (x->list[d]->get_status() == "running") { pos = d; }
		}
		return pos;
	}

};



//============================IMPLEMENTACJA PROCESORA Z LICZNIKIEM ROZKAZOW===================================


class cpu {
protected:

	struct str {
		str(int id, int burst_time) {
			this->id = id;
			this->burst_time = burst_time;
		}
		int id; // id procesu
		int burst_time;
		int nr = 0; //miejsce w którym jest w programie
	};

public:
	vector<str*> licznik; //licznik rozkazow

	void add() {
		int dlugosc = x->list.size();
		int id, time;
		for (int i = 0; i < dlugosc; i++)
		{
			if ((x->list[i]->get_status() != "terminated") && (check_id(x->list[i]->get_id()) == false))
			{
				id = x->list[i]->get_id(); time = x->list[i]->get_time();
				licznik.push_back(new str(id, time));
			}
		}

	}

	// sprawdza czy w liczniku rozkazow jest dany id
	bool check_id(int id) {
		int dlugosc = y->licznik.size();
		for (int i = 0; i < dlugosc; i++)
		{
			if (y->licznik.empty() == true) { return false; break; }
			if (y->licznik[i]->id == id) { return true; break; }
		}
		return false;
	}
	//procedura uruchomienia procesora
	void run() {
		if (cpu_run == false) {
			cpu_run = true;
			int dlugoscx = x->list.size(); string s;
			for (int i = 0; i < dlugoscx; i++)
			{
				if (x->list[i]->get_status() == "new") { x->list[i]->set_status(s = "ready"); }
			}

			if (x->run_test() == true)
			{
				x->list[x->run_pos()]->set_status(s = "ready");
			}
			x->list[x->shortest_position()]->set_status(s = "running");
			cout << "Procesor uruchomiony pomyslnie!" << endl;
		}
		else cout << "Procesor juz jest uruchomiony!" << endl;
	}


	void tact()
	{



	}


	void show() {
		int dlugosc = y->licznik.size();
		cout.width(5);
		cout << "ID";
		cout.width(17);
		cout << "STAN LICZNIKA";
		cout.width(15);
		cout << "PRZERWANIE" << endl;
		for (int i = 0; i < dlugosc; i++)
		{
			cout.width(5);
			cout << y->licznik[i]->id;
			cout.width(17);
			cout << y->licznik[i]->nr;
			cout.width(15);
			cout << y->licznik[i]->burst_time << endl;
		}

	}

};



//============================ANALIZA KOMEND===================================

void komenda(string line)
{
	istringstream line_stream(line);
	istream_iterator<string> begin(line_stream), end;
	copy(begin, end, back_inserter(dane));


	if (dane.empty() == false) {

		if (dane[0] == "help") {
			cout << endl;
			cout << endl << "DOSTEPNE KOMENDY: " << endl << endl
				<< "ad       - dodawanie procesu " << endl
				<< "del      - usuwanie procesu " << endl
				<< "da       - usuwanie wszystkich procesow " << endl
				<< "show     - wyswietlanie procesow " << endl
				<< "c        - zmiana stanu procesu " << endl
				<< "run      - uruchamia procesor " << endl
				<< "cls      - czyszczenie konsoli" << endl;
		}

		if (dane[0] == "cls") system("cls");
		if (dane[0] == "ad") {
			x->new_p();
		}

		if (dane[0] == "show") {
			x->show();
		}


		if (dane[0] == "del") {
			cout << "Podaj id procesu do usuniecia: ";
			int which;
			cin >> which;
			if (which == 0) cout << "Nie mozna usunac procesu bezczynnosci procesora!" << endl;
			else x->del(which);
		}

		if (dane[0] == "c") {
			cout << "Podaj id procesu, ktoremu zmienic status: ";
			int g; cin >> g;
			x->change(g);
		}

		if (dane[0] == "da") {
			x->list.clear();
			id_s = 0;
			x->dodaj_do_listy(new proces());  //proces bezczynnosci
		}

		if (dane[0] == "run") {
			y->run();
		}


		if (dane[0] == "l") {
			y->add();
			y->show();
		}


		dane.clear();
	}

}


//================================= MAIN =====================================

int main() {
	cout << "Welcome in SJF simulator 2017!" << endl;
	cout << "For more information please type: help" << endl;
	
	x = new lista_p;  // obiekt zarzadzania lista procesow
	y = new cpu;		// obiekt utworzenia klasy procesora

	x->dodaj_do_listy(new proces());  //proces bezczynnosci


	while (true)
	{
		string a;
		getline(cin, a);
		komenda(a);
	}

	return 0;
}
1

Weź ponazywaj zmiennie po ludzku, bo y, v i tym podobne to jakaś masakra.

Błąd wynika z tego, że w tym miejscu

y->add();

nie było jeszcze definicji klasy cpu (była jedynie deklaracja), a definicja jest potrzebna, żeby móc operować na obiekcie (bo skąd kompilator ma wiedzieć, że klasa cpu ma metodę add()).

Żeby to działało, to musisz definicję metody wyrzucić poza klasę i dać po definicji cpu.

class cpu;

class lista_p
{
    void new_p();
};

class cpu
{
    ... // definicja klasy
};

void lista_p::new_p()
{
    ...
}

Poza tym te klasy są już na tyle nietrywialne, że powinny być we własnych plikach .h/.cpp.

1 użytkowników online, w tym zalogowanych: 0, gości: 1