Fórum Root.cz
Hlavní témata => Vývoj => Téma založeno: wtf 31. 01. 2011, 21:21:25
-
Zdravim,
mam problem s nasledujucim kodom:
queuetp.h
template <class T, int n>
class QueueTP
{
private:
struct Node
{
T item;
Node * next;
};
Node * front;
Node * rear;
int items;
const int qsize;
QueueTP(const QueueTP & q) : qsize(0) {}
QueueTP & operator=(const QueueTP & q) {return * this;}
public:
explicit QueueTP();
~QueueTP();
bool isempty() const;
bool isfull() const;
int queuecount() const;
bool enqueue(const T & item);
bool dequeue(T & item);
};
template <class T, int n>
QueueTP<T, n>::QueueTP() : qsize(n)
{
front = rear = NULL;
items = 0;
}
template <class T, int n>
QueueTP<T, n>::~QueueTP()
{
Node * temp;
while (front != NULL)
{
temp = front;
front = temp->next;
delete temp;
}
}
template <class T, int n>
bool QueueTP<T, n>::isempty() const
{
return items == 0;
}
template <class T, int n>
bool QueueTP<T, n>::isfull() const
{
return items == qsize;
}
template <class T, int n>
int QueueTP<T, n>::queuecount() const
{
return items;
}
template <class T, int n>
bool QueueTP<T, n>::enqueue(const T & item)
{
if (isfull())
return false;
Node * add = new Node;
if (add == NULL)
return false;
add->item = item;
add->next = NULL;
items++;
if (front == NULL)
front = add;
else
rear->next = add;
rear = add;
return true;
}
template <class T, int n>
bool QueueTP<T, n>::dequeue(T & item)
{
if (front == NULL)
return false;
item = front->item;
items--;
Node * temp = front;
front = front->next;
delete temp;
if (items == 0)
rear = NULL;
return true;
}
wrkr.h
#include <iostream>
#include <cstring>
using namespace std;
class Worker
{
private:
char * fullName;
int id;
public:
Worker(const char * fn = "no name", const int n = 0);
Worker(const Worker & w);
~Worker();
Worker & operator=(const Worker & w);
void Show() const;
};
Worker::Worker(const char * fn, const int n)
{
fullName = new char[strlen(fn) + 1];
strcpy(fullName, fn);
id = n;
}
Worker::Worker(const Worker & w)
{
fullName = new char[strlen(w.fullName) + 1];
strcpy(fullName, w.fullName);
id = w.id;
}
Worker::~Worker()
{
delete [] fullName;
}
Worker & Worker::operator=(const Worker & w)
{
if (this == &w)
return *this;
delete [] fullName;
fullName = new char[strlen(w.fullName) + 1];
strcpy(fullName, w.fullName);
return *this;
}
void Worker::Show() const
{
cout << "Meno pracovnika: " << fullName << endl
<< "ID pracovnika: " << id << endl;
}
workerqueue.cpp - viem ze by tam ten cyklus mal vyzerat inak ale o to teraz nejde
#include <iostream>
#include "queuetp.h"
#include "wrkr.h"
const int SIZE = 4;
const int AR_SIZE = 20;
int main()
{
QueueTP<Worker*, SIZE> workers;
char choice;
for (int i = 0; i < SIZE; i++)
{
cout << "p: pridat pracovnika, o: odobrat pracovnika, k: koniec\n";
char choice;
cin >> choice;
if (choice == 'k') break;
while (strchr("pok", choice) == NULL)
{
cout << "Zadajte p, o alebo k: ";
cin >> choice;
}
cin.get();
switch (choice)
{
case 'p':
{
char name[AR_SIZE];
int id;
cout << "Meno pracovinka: ";
cin.getline(name, AR_SIZE);
cout << "ID pracovnika: ";
cin >> id;
Worker temp(name, id);
workers.enqueue(temp);
break;
}
case 'o':
Worker temp;
workers.dequeue(temp);
temp.Show();
break;
}
}
return 0;
}
pri kompilacii vyprodukuje chybu:
workerqueue.cpp: In function ‘int main()’:
workerqueue.cpp:44: error: no matching function for call to ‘QueueTP<Worker*, 4>::enqueue(Worker&)’
queuetp.h:68: note: candidates are: bool QueueTP<T, n>::enqueue(const T&) [with T = Worker*, int n = 4]
workerqueue.cpp:50: error: no matching function for call to ‘QueueTP<Worker*, 4>::dequeue(Worker&)’
queuetp.h:93: note: candidates are: bool QueueTP<T, n>::dequeue(T&) [with T = Worker*, int n = 4]
predom dakujem ze odpovede
-
Uz si to moc nepamatuju, ale muj nastrel by byl chybejici const:
bool QueueTP<T, n>::enqueue(const T & item) const
-
nn to by zapricinilo len to ze premenne z triedy by boli v tom bloku read-only. ten problem bude asi koli tomu ze pri inicializacii QueueTP vo workerqueue.cpp (QueueTP<Worker *, 4>) sa pouziva ukazovatel, len nechapem tomu preco...urcite sa to da nejako spravit aby tam sli ukazovatele
-
Pravda.
z
QueueTP<Worker*, SIZE> workers;
plyne, ze se musi do enqueue vlozit ukazatel, ale vklada se reference. Takze by to chtelo asi dynamickou alokaci a predat ukazatel, nebo statickou alokaci (a pracovat rovnou s QueueTP<Worker, SIZE>) a kopirovat cely objekt.
-
tak...s referenciou to funguje (len treba upravit inicializaciu QueueTP a metody enqueue a dequeue tak aby brali namiesto ukazovatela referenciu) ale s ukazovatelmi nie
workerqueue.cpp: In function ‘int main()’:
workerqueue.cpp:44: error: no matching function for call to ‘QueueTP<Worker*, 4>::enqueue(const Worker*)’
queuetp.h:68: note: candidates are: bool QueueTP<T, n>::enqueue(const T*) [with T = Worker*, int n = 4]
workerqueue.cpp:50: error: no matching function for call to ‘QueueTP<Worker*, 4>::dequeue(Worker*)’
queuetp.h:93: note: candidates are: bool QueueTP<T, n>::dequeue(T*) [with T = Worker*, int n = 4]
vazne neviem...
-
Prosím, čtěte co vám překladač píše, máte to tam krásně napsáno:
nenalezl funkci pro volání "QueueTP<Worker*, 4>::enqueue(const Worker*)"
ale mohlo by to být tohle "bool QueueTP<T, n>::enqueue(const T*) [with T = Worker*, int n = 4]"
takže buď předávat Worker staticky a nebo předělat konstruktor na bool QueueTP<T*, n>...
obdobně odstranit i druhou chybu. Je to jenom o tom, že předáváte jiné parametry než pro které jsou devinované v definicích funkcí.
M.
-
tak konecne sa mi to podarilo opravit. zistil som ze to ako nastavim parametre sablony pri inicializacii tak zostanu stale (cize ak to nastavim na Workers* tak napr pri metode bool QueueTP<T, n>::enqueue(const T & item) by sa volanie interpretovalo ako QueueTP<Workers*, 4>::enqueue(const Workers* & item))
cize stacilo upravit prototypy a definicie metod enqueue a dequeue:
bool enqueue(const T item);
bool dequeue(T item);
a potom samozrejme v main() trebalo pri davani argumentu do enqueue/dequeue pridat adresny operator