8 Replies - 1924 Views - Last Post: 19 February 2012 - 06:26 PM Rate Topic: -----

#1 killermunk1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 05-February 12

Error : unable to match function definition to an existing declaration

Posted 19 February 2012 - 03:18 PM

Hello, I have been working on my c++ homework for a bit. I have done what I think i need to but I get an error. it states

c:\users\killermunk1\desktop\gsp125 projects\gsp125_mulvaney_lab7_parta\gsp125_mulvaney_lab7_parta\queuetp.h(42): error C2244: 'QueueTP<Item,n>::{ctor}' : unable to match function definition to an existing declaration
definition
'QueueTP<Item,n>::QueueTP(void)'
existing declarations
'QueueTP<Item,n>::QueueTP(int)'
'QueueTP<Item,n>::QueueTP(const QueueTP<Item,n> &)'


Does anyone have an idea on how to fix this error? I will be looking over it and post If I fix it before. I am guessing that I overlooked something and cannot seem to find it.
This is the code for the queuetp.h

// queuetp.h -- interface for a queue template
#ifndef _QUEUETP_H_
#define _QUEUETP_H_

//the Queue class on page 619(ignore the Customer class) will be a template, so we need to define it here.  
//You just need to add the proper code to create a template to that class and place it all here.

//start with a line of code that defines a template(hint - see tips document)

// I will start you off with the class definition and private variables.  
template <class Item, int n>
class QueueTP
{
// class scope definitions
    // Node is a nested structure definition local to this class
    struct Node { Item item; struct Node * next;};
private:
    Node * front;        // pointer to front of Queue
    Node * rear;        // pointer to rear of Queue
    int items;        // current number of items in Queue
    const int qsize;    // maximum number of items in Queue
    QueueTP(const QueueTP & q) : qsize(0) { }    // preemptive definition
    QueueTP & operator=(const QueueTP & q) { return *this;}
public:
// place public variables and methods here
	explicit QueueTP(int qs= 10) : qsize(qs), front(NULL), rear(NULL), items(0) {}
	~QueueTP();
	bool isEmpty() const;
	bool isFull() const;
	int queuecount() const;
	bool enqueue( const Item &item);
	bool dequeue(Item &item);

};

// QueueTP methods - I will give you the first method (see top of page 620)
template <class Item, int n>
QueueTP<Item,n>::QueueTP() : qsize(n)
{
    front = rear = NULL;
    items = 0;
}

//create the rest here using page 620 as a guide
template <class Item, int n>
QueueTP <Item,n>::~QueueTP()
{
	Node*temp;
	while (front != NULL)
	{
		temp = front;
		front = front=>next;
		delete temp;
	}
}

template <class Item, int n>
bool QueueTP<Item,n>::isEmpty() const { return items == 0;}

template <class Item, int n>
bool QueueTP<Item,n>::isFull() const { return items == qsize;}

template <class Item, int n>
int QueueTP<Item,n>::queuecount() const { return items; }

template <class Item, int n>
bool QueueTP<Item,n>::enqueue(const Item &item)
{
	if (isFull()){
		return false;}
	Node * add = new Node;
	if (add == NUUL) {
		reutrn false; }
	add->item = item;
	add->next = NULL;
	items++;
	if (front == NULL) {
		front = add; }
	else {
		rear->next = add; }
	rear = add;
	return true;

}
template <class Item, int n>
bool QueueTP<Item,n>::dequeue(Item &item)
{
	if (front == NULL) {
		reutrn false; }
	item = front -> item;
	items--;
	Node *temp = front;
	front = front->next;
	delete temp;
	if (items == 0) {
		rear = NULL; }
	return true;
}



#endif



Is This A Good Question/Topic? 0
  • +

Replies To: Error : unable to match function definition to an existing declaration

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: Error : unable to match function definition to an existing declaration

Posted 19 February 2012 - 03:42 PM

in your class definition, your QueueTP default constructor is defined as
explicit QueueTP(int qs= 10) : qsize(qs), front(NULL), rear(NULL), items(0) {}



However, your also have another rogue bit of code (which looks like you've partially duplicated your default constructor):
// QueueTP methods - I will give you the first method (see top of page 620)
template <class Item, int n>
QueueTP<Item,n>::QueueTP() : qsize(n)
{
    front = rear = NULL;
    items = 0;
} 

This post has been edited by Bench: 19 February 2012 - 03:57 PM

Was This Post Helpful? 0
  • +
  • -

#3 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: Error : unable to match function definition to an existing declaration

Posted 19 February 2012 - 03:50 PM

Also, on an unrelated note - since you have an int template parameter, there's no point duplicating it with a const int qsize member in your class. You may aswell get rid of 'qsize', since the template parameter is part of your class' type information anyway


Alternatively, people sometimes use an enum to make the type information part of the public interface - e.g.
template <int N>
class Queue
{
public:
    enum { max_size = N };
}; 




edit: fixed broken tag

This post has been edited by Bench: 19 February 2012 - 03:50 PM

Was This Post Helpful? 0
  • +
  • -

#4 killermunk1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 05-February 12

Re: Error : unable to match function definition to an existing declaration

Posted 19 February 2012 - 04:28 PM

All right so I was looking back and forth trying to figure out what you meant about the default constructor. I have to say that I am overlooking something because I do not understand what is wrong. I do have to say I am more or less a beginner-. I did add in what I think you meant for your second post.
// queuetp.h -- interface for a queue template
#ifndef _QUEUETP_H_
#define _QUEUETP_H_

//the Queue class on page 619(ignore the Customer class) will be a template, so we need to define it here.  
//You just need to add the proper code to create a template to that class and place it all here.

//start with a line of code that defines a template(hint - see tips document)

// I will start you off with the class definition and private variables.  
template <class Item, int n>
class QueueTP
{
// class scope definitions
    // Node is a nested structure definition local to this class
    struct Node { Item item; struct Node * next;};
	enum {Q_SIZE = 10};
private:
    Node * front;        // pointer to front of Queue
    Node * rear;        // pointer to rear of Queue
    int items;        // current number of items in Queue
    const int qsize;    // maximum number of items in Queue
    QueueTP(const QueueTP & q) : qsize(0) { }    // preemptive definition
    QueueTP & operator=(const QueueTP & q) { return *this;}
public:
// place public variables and methods here
	explicit QueueTP(int qs = Q_SIZE) : qsize(qs), front(NULL), items(0) {}
	~QueueTP();
	bool isEmpty() const;
	bool isFull() const;
	int queuecount() const;
	bool enqueue( const Item &item);
	bool dequeue(Item &item);

};

// QueueTP methods - I will give you the first method (see top of page 620)
template <class Item, int n>
QueueTP<Item,n>::QueueTP() : qsize(n)
{
    front = rear = NULL;
    items = 0;
}

//create the rest here using page 620 as a guide
template <class Item, int n>
QueueTP <Item,n>::~QueueTP()
{
	Node*temp;
	while (front != NULL)
	{
		temp = front;
		front = front=>next;
		delete temp;
	}
}

template <class Item, int n>
bool QueueTP<Item,n>::isEmpty() const { return items == 0;}

template <class Item, int n>
bool QueueTP<Item,n>::isFull() const { return items == qsize;}

template <class Item, int n>
int QueueTP<Item,n>::queuecount() const { return items; }

template <class Item, int n>
bool QueueTP<Item,n>::enqueue(const Item &item)
{
	if (isFull()){
		return false;}
	Node * add = new Node;
	if (add == NUUL) {
		reutrn false; }
	add->item = item;
	add->next = NULL;
	items++;
	if (front == NULL) {
		front = add; }
	else {
		rear->next = add; }
	rear = add;
	return true;

}
template <class Item, int n>
bool QueueTP<Item,n>::dequeue(Item &item)
{
	if (front == NULL) {
		reutrn false; }
	item = front -> item;
	items--;
	Node *temp = front;
	front = front->next;
	delete temp;
	if (items == 0) {
		rear = NULL; }
	return true;
}



#endif


Was This Post Helpful? 0
  • +
  • -

#5 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: Error : unable to match function definition to an existing declaration

Posted 19 February 2012 - 05:47 PM

C++ defines the term "default constructor" as A constructor capable of taking 0 arguments.

The following meets the criteria of 'default constructor' since its only parameter has a default argument associated with it (meaning it can be called with 0 arguments)
	explicit QueueTP(int qs = Q_SIZE) : qsize(qs), front(NULL), items(0) {} 

This is a complete constructor, which includes the signature, initialiser list and an empty body (two curly parenthesis)




However, later in your code you've got something else (which isn't part of your class), which "looks like" anothe default constructor - except that it's not part of your class, and if it were, then it would be conflicting with the default constructor you've got already.
// QueueTP methods - I will give you the first method (see top of page 620)
template <class Item, int n>
QueueTP<Item,n>::QueueTP() : qsize(n)
{
    front = rear = NULL;
    items = 0;
} 
It seems to me that you don't need this - you've already got a constructor which initialises qsize, front, rear and items. It's causing an error, so you may as well take it out



Also, it appears you have a typo in your destructor - presumably you intended to use the -> operator and not =>
//create the rest here using page 620 as a guide
template <class Item, int n>
QueueTP <Item,n>::~QueueTP()
{
	Node*temp;
	while (front != NULL)
	{
		temp = front;
		front = front=>next;
		delete temp;
	}
}







edit: fixed a code tag typo

This post has been edited by Bench: 19 February 2012 - 05:52 PM

Was This Post Helpful? 0
  • +
  • -

#6 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: Error : unable to match function definition to an existing declaration

Posted 19 February 2012 - 05:57 PM

Actually, I've noticed a few other typos in your template member functions (check your use of the return keyword) - Because you're writing a template class, your compiler is not generating any code for some of the member functions.... YET! These errors will come along to bite you as soon as you use those functions though.

I would strongly suggest that once you get this compiling, you test each of your member functions and fix any compiler errors before making any more changes. The usual way to write a template class is to create the class without templates first, then add the templates in afterwards, once everything is working; since you seem to be doing this the other way around, you're going to be confronted with more cryptic errors which you might find difficult to decypher.
Was This Post Helpful? 0
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1343
  • View blog
  • Posts: 4,604
  • Joined: 19-February 09

Re: Error : unable to match function definition to an existing declaration

Posted 19 February 2012 - 06:14 PM

You have a function inside the class definition. Notice the parameter has a default value.

027	    explicit QueueTP(int qs = Q_SIZE) : qsize(qs), front(NULL), items(0) {}





And a function declared outside the class definition.

038	template <class Item, int n>
039	QueueTP<Item,n>::QueueTP() : qsize(n)
040	{
041	    front = rear = NULL;
042	    items = 0;
043	}



The error is stating that it cannot find the function definition inside the class definition.

You could try and create a function definition in the class, but there could be a confusion of which function to call.

There is confusion about how qsize is being initialized. Is it using the template or a constant value or an input value.

Default constructors (C++ only)
Was This Post Helpful? 0
  • +
  • -

#8 killermunk1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 05-February 12

Re: Error : unable to match function definition to an existing declaration

Posted 19 February 2012 - 06:20 PM

Thank you for your help so far. Alright I commented out the default constructor and fixed all of those return typos(I'm surprised how many). An now the original error is gone (along with the typo errors)and this one is in its place.

1>c:\users\killermunk1\desktop\gsp125 projects\gsp125_mulvaney_lab7_parta\gsp125_mulvaney_lab7_parta\queuetp.h(53): error C2059: syntax error : '>'
1> c:\users\killermunk1\desktop\gsp125 projects\gsp125_mulvaney_lab7_parta\gsp125_mulvaney_lab7_parta\queuetp.h(48) : while compiling class template member function 'QueueTP<Item,n>::~QueueTP(void)'
1> with
1> [
1> Item=Worker *,
1> n=5
1> ]
1> c:\users\killermunk1\desktop\gsp125 projects\gsp125_mulvaney_lab7_parta\gsp125_mulvaney_lab7_parta\gsp125_mulvaney_lab7_parta.cpp(20) : see reference to class template instantiation 'QueueTP<Item,n>' being compiled
1> with
1> [
1> Item=Worker *,
1> n=5
1> ]


// queuetp.h -- interface for a queue template
#ifndef _QUEUETP_H_
#define _QUEUETP_H_

//the Queue class on page 619(ignore the Customer class) will be a template, so we need to define it here.  
//You just need to add the proper code to create a template to that class and place it all here.

//start with a line of code that defines a template(hint - see tips document)

// I will start you off with the class definition and private variables.  
template <class Item, int n>
class QueueTP
{
// class scope definitions
    // Node is a nested structure definition local to this class
    struct Node { Item item; struct Node * next;};
	enum {Q_SIZE = 10};
private:
    Node * front;        // pointer to front of Queue
    Node * rear;        // pointer to rear of Queue
    int items;        // current number of items in Queue
    const int qsize;    // maximum number of items in Queue
    QueueTP(const QueueTP & q) : qsize(0) { }    // preemptive definition
    QueueTP & operator=(const QueueTP & q) { return *this;}
public:
// place public variables and methods here
	explicit QueueTP(int qs = Q_SIZE) : qsize(qs), front(NULL), items(0) {}
	~QueueTP();
	bool isEmpty() const;
	bool isFull() const;
	int queuecount() const;
	bool enqueue( const Item &item);
	bool dequeue(Item &item);

};

// QueueTP methods - I will give you the first method (see top of page 620)
//template <class Item, int n>
//QueueTP<Item,n>::QueueTP() : qsize(n)
//{
//    front = rear = NULL;
//    items = 0;
//}

//create the rest here using page 620 as a guide
template <class Item, int n>
QueueTP <Item,n>::~QueueTP()
{
	Node*temp;
	while (front != NULL)
	{
		temp = front;
		front = front=>next;
		delete temp;
	}
}

template <class Item, int n>
bool QueueTP<Item,n>::isEmpty() const { return items == 0;}

template <class Item, int n>
bool QueueTP<Item,n>::isFull() const { return items == qsize;}

template <class Item, int n>
int QueueTP<Item,n>::queuecount() const { return items; }

template <class Item, int n>
bool QueueTP<Item,n>::enqueue(const Item &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 Item, int n>
bool QueueTP<Item,n>::dequeue(Item &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;
}



#endif


Was This Post Helpful? 0
  • +
  • -

#9 killermunk1  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 05-February 12

Re: Error : unable to match function definition to an existing declaration

Posted 19 February 2012 - 06:26 PM

Alright since I cannot find how to edit this. I found the issue i had => instead of -> the program can run now.

// queuetp.h -- interface for a queue template
#ifndef _QUEUETP_H_
#define _QUEUETP_H_

//the Queue class on page 619(ignore the Customer class) will be a template, so we need to define it here.  
//You just need to add the proper code to create a template to that class and place it all here.

//start with a line of code that defines a template(hint - see tips document)

// I will start you off with the class definition and private variables.  
template <class Item, int n>
class QueueTP
{
// class scope definitions
    // Node is a nested structure definition local to this class
    struct Node { Item item; struct Node * next;};
	enum {Q_SIZE = 10};
private:
    Node * front;        // pointer to front of Queue
    Node * rear;        // pointer to rear of Queue
    int items;        // current number of items in Queue
    const int qsize;    // maximum number of items in Queue
    QueueTP(const QueueTP & q) : qsize(0) { }    // preemptive definition
    QueueTP & operator=(const QueueTP & q) { return *this;}
public:
// place public variables and methods here
	explicit QueueTP(int qs = Q_SIZE) : qsize(qs), front(NULL), items(0) {}
	~QueueTP();
	bool isEmpty() const;
	bool isFull() const;
	int queuecount() const;
	bool enqueue( const Item &item);
	bool dequeue(Item &item);

};

// QueueTP methods - I will give you the first method (see top of page 620)
//template <class Item, int n>
//QueueTP<Item,n>::QueueTP() : qsize(n)
//{
//    front = rear = NULL;
//    items = 0;
//}

//create the rest here using page 620 as a guide
template <class Item, int n>
QueueTP <Item,n>::~QueueTP()
{
	Node*temp;
	while (front != NULL)
	{
		temp = front;
		front = front->next;
		delete temp;
	}
}

template <class Item, int n>
bool QueueTP<Item,n>::isEmpty() const { return items == 0;}

template <class Item, int n>
bool QueueTP<Item,n>::isFull() const { return items == qsize;}

template <class Item, int n>
int QueueTP<Item,n>::queuecount() const { return items; }

template <class Item, int n>
bool QueueTP<Item,n>::enqueue(const Item &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 Item, int n>
bool QueueTP<Item,n>::dequeue(Item &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;
}



#endif


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1