Link to home
Create AccountLog in
Avatar of george08
george08

asked on

CPP Class Queue (FIFO), Template Queue, Inheritance of Queue, Totally stuck

Hi there,

i have to create within the next 24 hours a class Queue with a static array, typ int, change that to a queue template and last but not least inheritate the class Queue.

I"m totally stuck, because my class Queue does not work and therefore i cannot go on to the next task.
When i use the main and compile the ctor "QueueClass::queue1(0,0);" my compiler returns an undefined an undeclared reference to to "QueueClass::~QueueClass()


Please Help! Thanks a lot I Apreciate!
class QueueClass {
  int queue[SIZE];
      int head, tail;
public:
 
 QueueClass(int, int);
  ~QueueClass();
  
 
  void qu(int num);
  int dequ();
  int size();
  int isEmpty();
};
 
QueueClass::QueueClass (int a, int b) {
  head = a;
  tail = b;
 
}
 
void QueueClass::qu(int num)
{
  if(tail+1==head || (tail+1==SIZE && !head)) {
    cout << "Queue is full\n";
    return;
  }
  tail++;
  if(tail==SIZE) tail = 0; // cycle around
  queue[tail] = num;
}
 
int QueueClass::dequ()
{
  if(head == tail) {
    cout << "Queue is empty\n";
    return 0;                    // or some other error indicator
  }
  head++;
  if(head==SIZE) head = 0;       // cycle around
  return queue[head];
}
 
int QueueClass::size()
{
    int j=0;
  for (; j<tail; j++){
  }
  return j;
  }
 
 int QueueClass::isEmpty()
 {
    if(head == tail) {
    cout << "Queue is empty\n";
    return 1;
    }
    else return 0;
 }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of anmalaver
anmalaver

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of george08
george08

ASKER

That it!

Thanks. What about the other questions? May you give a helping hand? I"m running out of time.

Thanks!
Now i don't have my code.
I have a dynamic Queue class (not using arrays) implemented with templates, but i can post it until 8pm...
that would be great. or can u help me to adapt my code from about to a template? i appreciate

Yes, i think so, its very easy.

Maybe i get the code soon, and i will post it for u.

Bye
thanks!
Avatar of evilrix
>> You have not implemented your class contructor
>> QueueClass::~QueueClass () {}

That's a destructor :)

>> I have a dynamic Queue class (not using arrays) implemented with templates, but i can post it until 8pm...
@anmalaver, just a heads-up... I think this Q is related to homework... before you go posting full code you should check. It is against the EE rules on Academic Integrity to post full code solutions for homework. You should prefer to guide and educate in these circumstances.

https://www.experts-exchange.com/help.jsp#hi368
https://www.experts-exchange.com/help.jsp#hi21
https://www.experts-exchange.com/help.jsp#hi27 (3.4)

Good luck both of you.

-Rx.
Thanx
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Im now on the Template... please help again

i attach the code snippets,
#include <iostream>
using namespace std;
 
#define SIZE 20
 
template <class t>
Class QueueClass
{
 
  int queue[SIZE];
      int head, tail;
public:
 
 QueueClass(int, int);
  ~QueueClass();
  
 
  void qu(int num);
  int dequ();
  int size();
  int isEmpty();
};
 
#include "QUEUET.H"
template &ltclass t>
b&ltt>:b()
QueueClass::QueueClass (int a, int b) {
  head = a;
  tail = b;
 
}
 
void QueueClass::qu(int num)
{
  if(tail+1==head || (tail+1==SIZE && !head)) {
    cout << "Queue is full\n";
    return;
  }
  tail++;
  if(tail==SIZE) tail = 0; // cycle around
  queue[tail] = num;
}
 
int QueueClass::dequ()
{
  if(head == tail) {
    cout << "Queue is empty\n";
    return 0;                    // or some other error indicator
  }
  head++;
  if(head==SIZE) head = 0;       // cycle around
  return queue[head];
}
 
int QueueClass::size()
{
    int j=0;
  for (; j<tail; j++){
  }
  return j;
  }
 
 int QueueClass::isEmpty()
 {
    if(head == tail) {
    cout << "Queue is empty\n";
    return 1;
    }
    else return 0;
 }
}
 
template &ltclass t>
b&ltt>::~QueueClass () {}
{
}
 
#include <iostream>
#include "queuet.cc"
using namespace std ;
int main()
{
	QueueClass<int> queue1 (1,1);
 
  
  
 
  cout<<"If 1, Queue is Empty, else 0 aka Queue is not empty. Result: " <<queue1.isEmpty()<<endl;
  int i;
  for(i=1; i <=10; i++) {
    queue1.qu(i);
  }
  cout<<"If 1, Queue is Empty, else 0 aka Queue is not empty. Result: " <<queue1.isEmpty()<<endl;
  cout<< queue1.size() <<" test" <<endl;
 
  for(i=1; i <=10; i++)
    cout << "Dequeue 1: " << queue1.dequ() << endl;
 
  return 0;
}

Open in new window

I think that's enough, if you need more help just tell us, and don't forget to accept the answer.
i accepted your answer, i did not knew the principle here. sorry.
@george08,

You have accepted answer before your Q has been fully answered! You do not have to close a Q just because an expert tells you to. Experts shouldn't tell you to close a Q, you should close when you are ready.

Do you feel you have got all the answers you need from this Q? If so Can I point out that you answered 2 questions and the answer you selected only answers one of them. I was still quite prepared to help you with your template Q (I was 1/2 way through writing you an answer). Do you still require assistance with this? If so I am happy to carry on helping you but I would request you re-open the Q until such time as all your Qs have been answered and then you look to select all the answers that helped you.

I can see you are new to EE so I'm guessing you are not fully aware of the correct process to close a Q. Can I suggest you read the following and if you are still not sure then please ask.

https://www.experts-exchange.com/help.jsp#hi331

If you would like me to get this Q re-opened for you so we can continue helping you I am more than happy to do so.

-Rx.
>> i accepted your answer, i did not knew the principle here. sorry.
You do not have to accept an answer because an expert tells you to.. experts shouldn't do that!

@anmalaver, you should tell an OP to close a Q. This Q was not finished and I was in the process of assisting. If you want a quick fix I suggest you go to a different site, this is not the site for you. This site is about helping others, not feeding your own ego.

@george08, based upon your last post I have requested this be reopened. Meanwhile I will continue assisting you until we have solved your problem... I will post again shortly
thanks a lot! i appreciate a lot. thumbsup!
Ok, I've requested the Q be reopened for you. It usually takes about 24 hours -- so watch this space. Meanwhile I will continue to assist you :)

>> Im now on the Template... please help again
Ok, you seem to be going in the right direction (although I'd change the t in the template declaration to be a T since it is normal to use uppercase and T is the conventional character used). You now need to go through the class and find all instances of type int and change them to be type T (remember, T is just a place holder for any time).

So, for example...

template <class T>
Class QueueClass
{

      //int queue[SIZE];
      T queue[SIZE];
      //int head, tail;
      T head, tail;
public:

      //QueueClass(int, int);
      QueueClass(T, T);
      ~QueueClass();

      ...
};


Don't forget to modify ALL functions that below to class Queue.
This is the error of my queuet.h when i run the compiler... i do not have any clue

thanks!
#include <iostream>
using namespace std;
 
#define SIZE 20
 
template <class T>
Class QueueClass  //expected constructor, destroctor or type conversion before "QueueClass" //exptected ; before QueueClass
 
  T queue[SIZE];
      T head, tail;
public:
 
 QueueClass(T, T);
  ~QueueClass();
  
 
  void qu(T num);
  int dequ();
  T size();
  T isEmpty();
};

Open in new window

Look at the line you've identified... it contains an error.. I'll give you a hint, what's the difference between these two lines of code?

class foo{}; // This is correct
Class foo{};// This contains an error
Don't forget opening brace:

template <class T> class QueueClass
{
>> thanks a lot! i appreciate a lot. thumbsup!
Well spotted... I didn't see that... although, the next compiler error you'd have got once you fixed what I identified above would have told us :)
I'll try again -- this time with the correct copy/paste buffer!

>> Don't forget opening brace:
Well spotted... I didn't see that... although, the next compiler error you'd have got once you fixed what I identified above would have told us :)
Relax evilrix, i'm not here to fight...
Take a breath...
thank you all

more to come, more answers needed :-) i marked the errors in the code, could not be so bad... but i'll learn a lot in these hours



#include "QUEUET.H"
template <class T>
 
QueueClass::QueueClass (T a, T b) { //error: clonficts with function deklsation template<class T> class QueueClass
  head = a; //undeclered, first use in function, me: declared in h, so within the scoop
  tail = b; //undeclered, first use in function
 
}
 
T QueueClass::qu(T num) //error template <class T? class QueueClass used without template parameters 
{  //exptected , or ; bevor { token
  if(tail+1==head || (tail+1==SIZE && !head)) {
    cout << "Queue is full\n";
    return;
  }
  tail++;
  if(tail==SIZE) tail = 0; 
  queue[tail] = num;
}
 
T QueueClass::dequ() //does not have a name type
{
  if(head == tail) {
    cout << "Queue is empty\n";
    return 0;                   
  }
  head++;
  if(head==SIZE) head = 0;       
  return queue[head];
}
 
T QueueClass::size() //does not have a name type
{
    T j=0;
  for (; j<tail; j++){
  }
  return j;
  }
 
 T QueueClass::isEmpty() //does not have a name type
 {
    if(head == tail) {
    cout << "Queue is empty\n";
    return 1;
    }
    else return 0;
 }
}
 
 
#include <iostream>
#include "queuet.cc"
using namespace std ;
int main()
{
        QueueClass<int> queue1 (1,1);
 
  
  
 
  cout<<"If 1, Queue is Empty, else 0 aka Queue is not empty. Result: " <<queue1.isEmpty()<<endl;
  int i;
  for(i=1; i <=10; i++) {
    queue1.qu(i);
  }
  cout<<"If 1, Queue is Empty, else 0 aka Queue is not empty. Result: " <<queue1.isEmpty()<<endl;
  cout<< queue1.size() <<" test" <<endl;
 
  for(i=1; i <=10; i++)
    cout << "Dequeue 1: " << queue1.dequ() << endl;
 
  return 0;
}

Open in new window

>> Relax evilrix, i'm not here to fight...
I just congratulated you, did I not? I was just pointing out we'd have got to that next, once the compiler did -- but well done for spotting it first.

>> Take a breath...
I did, whilst I counted to 10 slowly after your little performance above!

>> more to come, more answers needed :-) i marked the errors in the code, could not be so bad... but i'll learn a lot in these hours
I am going through your code now and annotating it with comments... watch this space...
I don't know the reason, but when i did my class i had to add all the code on the .h...
Ok, one this... all your class member functions need to be declared as template member functions...

template <typename T>
QueueClass::QueueClass (T a, T b)
{
}

template <typename T>
T QueueClass::qu(T num)
{
}


etc...
oops... sorry, typo I'll try again....

Ok, one this... all your class member functions need to be declared as template member functions...

template <typename T>
QueueClass<T>::QueueClass (T a, T b)
{
}

template <typename T>
T QueueClass<T>::qu(T num)
{
}

etc...

I'll be posting back the full code shortly -- tested on a compiler to fix my typos :)
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Oh, I forgot to mention that you can find my annotations in the code simply by searching for RX:

// <-- RX: All my comments always follow this format, so you an find them by looking for the leading 'RX'
Hey, this link help! @evilrix

but i'm still stuck to a few problems i cannot figure out....

see the code snippets



template <class T>
 
QueueClass<T>::QueueClass (T a, T b) { 		//expected ; befor < token
	head = a; 
	tail = b; 				
 
}
 
template <typename T> 
 
void QueueClass<T>::qu(T num) 		//init declarator before < token //expected ; befor < token
{  
	if(tail+1==head || (tail+1==SIZE && !head)) {
		cout << "Queue is full\n";
		return;
	}
	tail++;
	if(tail==SIZE) tail = 0; 
	queue[tail] = num;
}
 
template <typename T> 
 
T QueueClass<T>::dequ()		//init declarator before < token //expected ; befor < token
{
	if(head == tail) {
		cout << "Queue is empty\n";
		return 0;                   
	}
	head++;
	if(head==SIZE) head = 0;       
	return queue[head];
}
 
template <typename T> 
T QueueClass<T>::size()
{ 							//init declarator before < token //expected ; befor < token
	T j=0;
	for (; j<tail; j++){
	}
	return j;
}
 
template <typename T> 
 
T QueueClass<T>::isEmpty()
{  				//init declarator before < token //expected ; befor < token
	if(head == tail) {
		cout << "Queue is empty\n";
		return 1;
	}
	else return 0;
}
 
///////////////////////
 
#include <iostream>
#include "queuet.cc"
using namespace std ;
int main()
{
	QueueClass<int> queue1 (1,1);  ////expected ; before int //QueueClass undeclared  //expect primary expression before int
 
	cout<<"If 1, Queue is Empty, else 0 aka Queue is not empty. Result: " <<queue1.isEmpty()<<endl;
	int i;
	for(i=1; i <=10; i++) {
		queue1.qu(i);        // queue1 undecleared, first use
	}
	cout<<"If 1, Queue is Empty, else 0 aka Queue is not empty. Result: " <<queue1.isEmpty()<<endl;
	cout<< queue1.size() <<" test" <<endl;
 
	for(i=1; i <=10; i++)
		cout << "Dequeue 1: " << queue1.dequ() << endl;
 
	return 0;
}

Open in new window

I suspect the error is before the code you posted, it looks like you have a missing semi-colon somewhere. The code I posted you built fine on VS2005... make sure you've split it up properly! did you try building it as it is (you'll have to comment out the includes that include your headers)?

BTW: What compiler are you using?

If you still can't resolve please post all the code.
>> you'll have to comment out the includes that include your headers
Try copying and pasting ALL of this into one source file and building it... you should find it builds fine. If it does then carefully separate it up into the correct files and don't forget to uncomment the includes for the headers :)
//#include "QUEUET.H" //<-- RX: Does this contain the class definition as per below?
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
 
// <--- RX: You need to have header guards
#ifndef QUEUET_H
#define QUEUET_H
 
#include <iostream>
using namespace std;
 
#define SIZE 20
 
template <class T>
//Class QueueClass <-- RX: Syntax error
class QueueClass
{ //<-- RX: Missing brace
 
	T queue[SIZE];
	T head, tail;
public:
 
	QueueClass(T, T);
	~QueueClass(){};
 
 
	void qu(T num);
	//int dequ(); <-- RX: Should return T
	T dequ();
	T size();
	T isEmpty();
};
 
// <--- RX: You need to have header guards
#endif
 
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// <-- RX: I take it you ionclude this into your code where it is needed?
//         You know the compiler needs to see the full template defintion
//         at the Point Of Instantiation (POI) right? It is normally simpler
//         to implement all the template functions in the header, inine in
//         the class rather than seperately, otherwise it makes life harder.
 
template <class T>
//QueueClass::QueueClass (T a, T b) { //error: clonficts with function deklsation template<class T> class QueueClass
QueueClass<T>::QueueClass (T a, T b) { //<-- RX: The class is a template, the signature includes the template parameter(s)
	head = a; //undeclered, first use in function, me: declared in h, so within the scoop
	tail = b; //undeclered, first use in function
 
}
 
template <typename T> //<-- RX: You need to define this as a member template function so you need this!
//T QueueClass::qu(T num) //error template <class T? class QueueClass used without template parameters 
void QueueClass<T>::qu(T num)//<-- RX: The class is a template, the signature includes the template parameter(s) (this also returns void)
{  //exptected , or ; bevor { token
	if(tail+1==head || (tail+1==SIZE && !head)) {
		cout << "Queue is full\n";
		return;
	}
	tail++;
	if(tail==SIZE) tail = 0; 
	queue[tail] = num;
}
 
template <typename T> //<-- RX: You need to define this as a member template function so you need this!
//T QueueClass::dequ() //does not have a name type
T QueueClass<T>::dequ()//<-- RX: The class is a template, the signature includes the template parameter(s)
{
	if(head == tail) {
		cout << "Queue is empty\n";
		return 0;                   
	}
	head++;
	if(head==SIZE) head = 0;       
	return queue[head];
}
 
template <typename T> //<-- RX: You need to define this as a member template function so you need this!
//T QueueClass::size() //does not have a name type
T QueueClass<T>::size()//<-- RX: The class is a template, the signature includes the template parameter(s)
{
	T j=0;
	for (; j<tail; j++){
	}
	return j;
}
 
template <typename T> //<-- RX: You need to define this as a member template function so you need this!
//T QueueClass::isEmpty() //does not have a name type
T QueueClass<T>::isEmpty()//<-- RX: The class is a template, the signature includes the template parameter(s)
{
	if(head == tail) {
		cout << "Queue is empty\n";
		return 1;
	}
	else return 0;
}
//} <-- RX: Rogue brace
 
 
#include <iostream>
//#include "queuet.cc"
using namespace std ;
int main()
{
	QueueClass<int> queue1 (1,1);
 
 
 
 
	cout<<"If 1, Queue is Empty, else 0 aka Queue is not empty. Result: " <<queue1.isEmpty()<<endl;
	int i;
	for(i=1; i <=10; i++) {
		queue1.qu(i);
	}
	cout<<"If 1, Queue is Empty, else 0 aka Queue is not empty. Result: " <<queue1.isEmpty()<<endl;
	cout<< queue1.size() <<" test" <<endl;
 
	for(i=1; i <=10; i++)
		cout << "Dequeue 1: " << queue1.dequ() << endl;
 
	return 0;
}

Open in new window

BTW: I'm real sorry but I will have to go to bed in about 5 minutes, work in about 6 hours :(
I'm using a gnu with the command line under windows xp

i have no more clue, i'm posting my whole code, which, i might add worked bevor i went over to the template business


thanx!

///////////////my queuet.h
#ifndef QUEUET_H
#define QUEUET_H
 
#include <iostream>
using namespace std;
 
#define SIZE 20
 
template <class T>
 
class QueueClass
{ 
 
  T queue[SIZE];
      T head, tail;
public:
 
 QueueClass(T, T);
  ~QueueClass();
  
 
  void qu(T num);
 
  T dequ();
  T size();
  T isEmpty();
};
 
#endif
 
////////////////////////////////my queuet.cc
 
 
template <class T>
 
QueueClass<T>::QueueClass (T a, T b) { 		//expected ; befor < token
	head = a; 
	tail = b; 				
 
}
 
template <typename T> 
 
void QueueClass<T>::qu(T num) 		//init declarator before < token //expected ; befor < token
{  
	if(tail+1==head || (tail+1==SIZE && !head)) {
		cout << "Queue is full\n";
		return;
	}
	tail++;
	if(tail==SIZE) tail = 0; 
	queue[tail] = num;
}
 
template <typename T> 
 
T QueueClass<T>::dequ()		//init declarator before < token //expected ; befor < token
{
	if(head == tail) {
		cout << "Queue is empty\n";
		return 0;                   
	}
	head++;
	if(head==SIZE) head = 0;       
	return queue[head];
}
 
template <typename T> 
T QueueClass<T>::size()
{ 							//init declarator before < token //expected ; befor < token
	T j=0;
	for (; j<tail; j++){
	}
	return j;
}
 
template <typename T> 
 
T QueueClass<T>::isEmpty()
{  				//init declarator before < token //expected ; befor < token
	if(head == tail) {
		cout << "Queue is empty\n";
		return 1;
	}
	else return 0;
}
 
/////////////////////////////my main.cc
 
#include <iostream>
#include "queuet.cc"
using namespace std ;
int main()
{
	QueueClass<int> queue1 (1,1);  ////expected ; before int //QueueClass undeclared  //expect primary expression before int
 
	cout<<"If 1, Queue is Empty, else 0 aka Queue is not empty. Result: " <<queue1.isEmpty()<<endl;
	int i;
	for(i=1; i <=10; i++) {
		queue1.qu(i);        // queue1 undecleared, first use
	}
	cout<<"If 1, Queue is Empty, else 0 aka Queue is not empty. Result: " <<queue1.isEmpty()<<endl;
	cout<< queue1.size() <<" test" <<endl;
 
	for(i=1; i <=10; i++)
		cout << "Dequeue 1: " << queue1.dequ() << endl;
 
	return 0;
}

Open in new window

This code builds fine, with the exception of (still) a missing destructor defintion
 ~QueueClass(){};


Try this...
///////////////my queuet.h
#ifndef QUEUET_H
#define QUEUET_H
 
#include <iostream>
using namespace std;
 
#define SIZE 20
 
template <typename T>
class QueueClass
{ 
 
  T queue[SIZE];
      T head, tail;
public:
 
 QueueClass(T, T);
 ~QueueClass(){};
  
 
  void qu(T num);
 
  T dequ();
  T size();
  T isEmpty();
};
 
#endif
 
////////////////////////////////my queuet.cc
 
 
template <typename T>
QueueClass<T>::QueueClass (T a, T b) { 		//expected ; befor < token
	head = a; 
	tail = b; 				
 
}
 
template <typename T> 
 
void QueueClass<T>::qu(T num) 		//init declarator before < token //expected ; befor < token
{  
	if(tail+1==head || (tail+1==SIZE && !head)) {
		cout << "Queue is full\n";
		return;
	}
	tail++;
	if(tail==SIZE) tail = 0; 
	queue[tail] = num;
}
 
template <typename T> 
 
T QueueClass<T>::dequ()		//init declarator before < token //expected ; befor < token
{
	if(head == tail) {
		cout << "Queue is empty\n";
		return 0;                   
	}
	head++;
	if(head==SIZE) head = 0;       
	return queue[head];
}
 
template <typename T> 
T QueueClass<T>::size()
{ 							//init declarator before < token //expected ; befor < token
	T j=0;
	for (; j<tail; j++){
	}
	return j;
}
 
template <typename T> 
 
T QueueClass<T>::isEmpty()
{  				//init declarator before < token //expected ; befor < token
	if(head == tail) {
		cout << "Queue is empty\n";
		return 1;
	}
	else return 0;
}
 
/////////////////////////////my main.cc
 
#include <iostream>
//#include "queuet.cc"
using namespace std ;
int main()
{
	QueueClass<int> queue1 (1,1);  ////expected ; before int //QueueClass undeclared  //expect primary expression before int
 
	cout<<"If 1, Queue is Empty, else 0 aka Queue is not empty. Result: " <<queue1.isEmpty()<<endl;
	int i;
	for(i=1; i <=10; i++) {
		queue1.qu(i);        // queue1 undecleared, first use
	}
	cout<<"If 1, Queue is Empty, else 0 aka Queue is not empty. Result: " <<queue1.isEmpty()<<endl;
	cout<< queue1.size() <<" test" <<endl;
 
	for(i=1; i <=10; i++)
		cout << "Dequeue 1: " << queue1.dequ() << endl;
 
	return 0;
}

Open in new window

^^^ Incidentally, I qualified this code using both VS2005 and gcc 4.x on Ubuntu Linux. You code builds fine, it's just failing to link because of the missing destructor... which I've now added for you.
Thanks. Sleep well. but i do have to challenge the main.cc... there are still errors...
>> Thanks. Sleep well. but i do have to challenge the main.cc... there are still errors...
In the code I posted you? I did check it on 2 compilers :)

I've added a zip files of the 3 files I think you should have based upon how I believe your code to be structured. Take a look and see if it helps.

I really do have to go to bed now (sorry but it's 11pm in the UK and I have to be up a 6am). If you have more doubts please post them here and I'll review them when I get up... but do try and fix them yourself first or you'll learn nothing. Most of the time the compiler errors should tell you what's wrong (the compiler never lies) so you just need to read the errors carefully and read the code until you spot it.

Meanwhile, hopefully a moderator will reopen this Q so it can be closed properly.

Good luck my friend.

-Rx.
tqueue.zip
This is still my problem... :-(

Hope someone could help.  I have no clue at all :-(
problem.jpg
Have you included queuet.h in queuet.cc ??? I suspect not!
Hi again

try to put your definition and implementation on the .h and compile only the main.cc
>> try to put your definition and implementation on the .h and compile only the main.cc
That isn't necessary -- I suggest you take a look at how this code is structured. At this point I don't think we need to confuse the matter more by asking george08 to restructure his code... I know this build, I've tried it on 2 compilers :)
@evilrix.

thanks good friend
>> thanks good friend
No worries my friend :)

All sorted now? Builds ok?

Right, I am now off to bed (I meant it this time!)

Night all. I'll check back tomorrow in case you have any other problems.
No, nothing works fine, even when i included the queuet.h ... :(

i gotta grab some sleep, cya tommorow...
>> No, nothing works fine, even when i included the queuet.h
Ok, then can you zip up the entire project and post it here please...

http://www.ee-stuff.com/

The username and password for this siate are the same as your normal EE userename and password (this is a EE site, it's used for hosting files).

I can then look at your project as a whole and hopefully spot what I am missing. I promise you, it is going to be something very trivial... this code builds.

-Rx.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
That worked. Thanks. (thumbsup)

>> That worked. Thanks. (thumbsup)
Good stuff :)
One last thing to attack. Inheritance. It is not a very intelligent way of inherritance, but it is inheritance, so please do not say me that this is a silly way. ;-)

Thanks again


////////////my .h
 
#include <iostream>
using namespace std;
 
#define SIZE 20
 
class QueueElement {
public: 
 int queue[SIZE];
      int head, tail;
 
 QueueElement();
 QueueElement(int, int);
  ~QueueElement();
  
 
  void qu(int num);
  int dequ();
  int size();
  int isEmpty();
};
 
class QueueClass : public QueueElement{ // QueueClass is Subclass to QueueElement{
};
 
//// my main 
 
#include <iostream>
#include "queueBase.h"
 
QueueClass::~QueueClass () {} //error: defintion of implicitly-declared  QueueClass::~QueueClass()
QueueClass::QueueClass (int a, int b) { //error prototyp for QueueClass::QueueClass(int, int) does not match any in class QueueClass //.h line 22 Kanidates are QueueClass()
 head = a;
  tail = b;
 
}
 
void QueueClass::qu(int num) //no void QueueClass::qu member function declared in  class QueueClass
{
  if(tail+1==head || (tail+1==SIZE && !head)) {
    cout << "Queue is full\n";
    return;
  }
  tail++;
  if(tail==SIZE) tail = 0; // cycle around
  queue[tail] = num;
}
 
int QueueClass::dequ()  //no int QueueClass::dequ member function declared in  class QueueClass
{
  if(head == tail) {
    cout << "Queue is empty\n";
    return 0;                    // or some other error indicator
  }
  head++;
  if(head==SIZE) head = 0;       // cycle around
  return queue[head];
}
 
int QueueClass::size() //no int QueueClass::size member function declared in  class QueueClass
{
    int j=0;
  for (; j<tail; j++){
  }
  return j;
  }
 
 int QueueClass::isEmpty() //no int QueueClass::isEmpty member function declared in  class QueueClass
 {
    if(head == tail) {
    cout << "Queue is empty\n";
    return 1;
    }
    else return 0;
 }
 
int main()
{
  QueueClass queue1(0,0); //no matching function  for call QueueClass::QueueClass(int int)
  
 
  cout<<"If 1, Queue is Empty, else 0 aka Queue is not empty. Result: " <<queue1.isEmpty()<<endl;
  int i;
  for(i=1; i <=10; i++) {
    queue1.qu(i);
  }
  cout<<"If 1, Queue is Empty, else 0 aka Queue is not empty. Result: " <<queue1.isEmpty()<<endl;
  cout<< queue1.size() <<" test" <<endl;
 
  for(i=1; i <=10; i++)
    cout << "Dequeue 1: " << queue1.dequ() << endl;
 
  return 0;
}

Open in new window

Well, it is inheritance :) It's a little pointless since you don't do any additional implementation in the sub-class to specialize the super (base) class, but it is inheritance. Were you not meant to do this using the template though?

You need to change the implementation of your functions as they are currently trying to implement QueueClass, they now need tro implement QueueElement.

E.g.

void QueueClass::qu(int num)
{
...
}

Should be

void QueueElement::qu(int num)
{
...
}

One other thing, classes that are designed to be sub-classes should generally consider making their destructors virtual so that if the derived class is deleted in terms of a pointer to the base class the correct destructor will still be executed (ie. that of the derived class) polymorphically.

http://www.codersource.net/cpp_virtual_destructors.html
thanks a lot, i'm very exhausted, so i do not care about these 'minor' details if it is silly ;-)

please help me with my last four errors

queuemain.cc line 4, queuemain.cc line 5 and queueBase.h line 22 in the code snippets i posted above.

i really appreciate....
 i never thought that i find such fine experts arround here in the internet and in the experts-exchange.com
>> i do not care about these 'minor' details if it is silly
Which detail? Nothing I've mentioned is minor :) The destructor problem is serious, if you try and delete using a base class pointer to a derived class and the constructor isn't virtual the C++ standard states the behavior is undefined... put another way your code will crash. Did you read the link I sent you? It's important you know this, maybe not for this specific bit of code but it is an important fundamental C++ principle.

>> queuemain.cc line 4, queuemain.cc line 5 and queueBase.h line 22 in the code snippets i posted above.
You'll need to give me the errors -- I'm good but not psychic :)

>> i never thought that i find such fine experts
Thanks (thumbs up)
Yes, i read your link, but - i'm too exhausted to think about it... :-(

I uploaded my file, glad that you helped me that long way last night und hope that you help me the rest of the (shorter) way

thanks

https://filedb.experts-exchange.com/incoming/ee-stuff/7176-inheritance.zip
a) You've not change the cosntructor and destructor at the top of queuemain.cc from QueueClass to QueueElement

From this...

QueueClass::~QueueClass() {}

QueueClass::QueueClass(int a, int b) {
 head = a;
  tail = b;
}
 ...to this...

QueueElement::~QueueElement() {}

QueueElement::QueueElement(int a, int b) {
 head = a;
  tail = b;
}

But now, because you are not using default compiler synthesize constructors,  you also have to implement a new constructor for QueueQlass, which then invokes the constructor of QueueElement.

e.g.

class QueueClass : public QueueElement
{
    QueueClass(int a, int b) : QueueElement(a,b){} // Invoke base class constructor, passing parameters to it
};

Thanks VM.
@george08, so you didn't come back to me after I clarified above about the need to write an explicit constructor for your derived class. I know you had a 24 hour window for this... did you have anymore questions or would you like any more clarification on the last point I made? Please feel free to ask if you are stuck, you can leave this Q open as long as you want as long as (a) we're still actively trying to solve your problem and (b) any additional question are directly related to the original one.

If you do want to close this Q now but you are not sure how or you are not sure which answers to select I'd be happy to make a recommendation and of course, as the other contributing expert, anmalaver's opinion should also be sought; however, being as we both have contributed to this thread it might be better if you follow Vee_Mod's advice and request help from a Zone Advisor so as to get an objective view (not that we wouldn't be objective, I'm sure). Anyway, we are all here to help so please don't be afraid to ask if you are not sure about anything.

-Rx.
Thanks to you all, there might me some more questions popping up later this evening or the day after tommorow. At the moment, i'm on the road.
Here's one more.

I'd like to change the inheritance to inclusion polymorphism (i hope that is the right termin)...

What i have done so far.

Change the ~ to virtual in the root class, all my methods are virtual and in my main, i created a reference object. but it does not work out, there the error i do not have a clue about. so i'm asking again for help. thank!

My Error
int main()
{
  QueueClass& queue1(0,0); //initiatilizer expression  list treated  as compond expression //invalid initialization  of non const  reference  of type Queueclass& from a tempory typ e "int"
 
ahhh my browser failed... here we go again

Here's one more.

I'd like to change the inheritance to inclusion polymorphism (i hope that is the right termin)...

What i have done so far.

Change the ~ to virtual in the root class, all my methods are virtual and in my main, i created a reference object. but it does not work out, there the error i do not have a clue about. so i'm asking again for help. thank!

My Error

int main()
{
  QueueClass& queue1(0,0);

//Error
//initiatilizer expression  list treated  as compond expression //invalid initialization  of non const   reference of type Queueclass& from a //tempory typ e "int"


please see also the code snippets

 

///Queuebase.h
 
#include <iostream>
using namespace std;
 
#define SIZE 20
 
class QueueElement {
public: 
 int queue[SIZE];
      int head, tail;
 
 QueueElement();
 QueueElement(int, int);
  virtual ~QueueElement();
  
 
  virtual void qu(int num);
  virtual int dequ();
  virtual int size();
  virtual int isEmpty();
};
 
class QueueClass : public QueueElement
{
public:
    QueueClass(int a, int b) : QueueElement(a,b){} // Invoke base class constructor, passing parameters to it
};
 
///queuemain.cc
 
#include <iostream>
#include "queueBase.h"
 
QueueElement::~QueueElement() {}
 
QueueElement::QueueElement(int a, int b) {
 head = a;
  tail = b;
}
 
void QueueElement::qu(int num) 
{
  if(tail+1==head || (tail+1==SIZE && !head)) {
    cout << "Queue is full\n";
    return;
  }
  tail++;
  if(tail==SIZE) tail = 0; // cycle around
  queue[tail] = num;
}
 
int QueueElement::dequ()  
{
  if(head == tail) {
    cout << "Queue is empty\n";
    return 0;                    // or some other error indicator
  }
  head++;
  if(head==SIZE) head = 0;       // cycle around
  return queue[head];
}
 
int QueueElement::size() 
{
    int j=0;
  for (; j<tail; j++){
  }
  return j;
  }
 
 int QueueElement::isEmpty() 
 {
    if(head == tail) {
    cout << "Queue is empty\n";
    return 1;
    }
    else return 0;
 }
 
int main()
{
  QueueClass& queue1(0,0); //initiatilizer expression  list treated  as compond expression //invalid initialization  of non const  reference  of type Queueclass& from a tempory typ e "int"
  
 
  cout<<"If 1, Queue is Empty, else 0 aka Queue is not empty. Result: " <<queue1.isEmpty()<<endl;
  int i;
  for(i=1; i <=10; i++) {
    queue1.qu(i);
  }
  cout<<"If 1, Queue is Empty, else 0 aka Queue is not empty. Result: " <<queue1.isEmpty()<<endl;
  cout<< queue1.size() <<" test" <<endl;
 
  for(i=1; i <=10; i++)
    cout << "Dequeue 1: " << queue1.dequ() << endl;
 
  return 0;
}

Open in new window

In main, do either this...

QueueClass tmpqueue1(0,0); // Object
QueueClass& queue1 = tmpqueue1; // Bind reference to object

or

QueueClass const & queue1 = QueueClass (0,0); // Bind reference to temporary

Note, if you follow the second form the reference must be const because it is bound to a temporary and temporaries are considered r-values. If the reference is const you will only be able to call const qualified member functions.

http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.10
BTW: It only makes sense to make virtual those functions those functions that you plan on being extensible in the sub-class. In some cases this wouldn't make sense and making the function virtual (a) gives it the wrong semantics and (b) is inefficient -- there is a cost to making a function virtual in both terms of performance and storage!

For example, I see little point in making IsEmpty() virtual, what possible reason could you have for wanting to specialize its behavior? It's either empty or not, surely? Functions should be made virtual with care!
I made a mistake with my inheritance.

I should have converted my class Queue so that i can use inclusion polymorphism. I wanna write a abstract class queueelement which should be used as the objects i wanna put i my queue. Need help again. Please? Thanks!
Hi george08 .

To be honest I think we are now going outside the scope of the original Q and that being the case I'd prefer if you could create a new one. One of the points of EE is to build a knowledge base of information, so each Q should only be related to one topic, any follow-up questions should be within the context of the original Q. Your original Q asked for help with templates and inheritance and it is my understanding, from your previous comments, these have now been successfully dealt with.

You'll note Vee_Mod's comment above, "As a Premium Services member, you have unlimited points. Keep your questions short, simple, and (as you have) stay active. Multiple questions in one are a real no-go". I would ask that you respect this point and log a new Q. I'd also ask that, unless there is anything specific outstanding regarding your original Q, that you consider now finalizing this Q.

If you need help with closing this Q properly please do ask. I can help you or, if you'd prefer, one of the Mods/Zone Advisors can assist (use the request for assistance link as also identified by Vee_Mod).

Many thanks george08 and I look forward to assisting you in the future.

-Rx.
>> I think we are now going outside the scope of the original Q
Just to clarify that point, although your original Q requested help with inheritance, polymorphism is a separate subject in its own right... although it can be implemented through inheritance it isn't the only way. This being the case I think it should be handled as separate subject matter.

Thanks.
george08,

May I ask why you've requested this Q be deleted in 7 days? Was this intentional or are you unsure how to finalize it? Would you like some assistance with this?
Ah, **** i just wanted to add the point to your perfect solution... didn't wanna delete it within seven days?
>> Ah, **** i just wanted to add the point to your perfect solution...
I'm blushing :)

Seriously though, you should go through the answers given here and select those few (keep it sensible though) that you think helped you in answering your question. You'll see a link labeled Accept Multiple Solutions (or something very similar) net to all the questions. If you select this the page will refresh and give you the opportunity to award the points to each answer you wish to select. You should look to assign more points to those answers that helped you more (think of it as though you are saying how helpful the answer was). Once you've assign the points to those few questions you can select Submit and the question will be finalized.

These links should help...

https://www.experts-exchange.com/help.jsp#hi331
https://www.experts-exchange.com/help.jsp#hi97
https://www.experts-exchange.com/help.jsp#hi100
https://www.experts-exchange.com/help.jsp#hi367

If you still need help let me know ir feel free to request assistance from a moderator.
>> the solution i ask in my first post is solved,
Ok, so do you need any help finalizing this Q or are you now happy you know how to do it?
i'm sorry if i offended someone, if i did so it was not on purpose. sorry.
>> i'm sorry if i offended someone, if i did so it was not on purpose. sorry.
george08, no one is offended my friend :) We are only here to try and help you and I promise that is all I wish to do.

Tell you what, so as to make you life and mine easier I'll ask Vee_Mod (or another mod) to work with you to close this. Don't worry, they doesn't bite and I'm sure you'll find with there help you'll be able to finalize this question and have a very happy time on EE.

Good luck george08, see you on your other question ;)

-Rx.
george08,

As promised, I've requested a moderator come and assist you. Whoever comes will be happy to help guide you through the process of closing a question.

Link to the request: { http:Q_23470187.html }

Many thanks.

-Rx.
Sorry, I posted the wrong link... try again...

{ http:Q_23480376.html }
To aid the moderator when they review this Q, my recommendation for finalization is as follows: -

PAQ: { http:#21745785 } anmalaver
PAQ: { http:#21746087 } evilrix
PAQ: { http:#21746853 } evilrix
PAQ: { http:#21746906 } evilrix
PAQ: { http:#21748706 } evilrix

I have tried to pick out the salient answers that would be useful to anyone reading the original Q.

Thanks.