[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Error 2061: cannot find the identifer

Posted on 2011-04-25
9
Medium Priority
?
357 Views
Last Modified: 2012-05-11
Hello Everyone,

I am creating a program for my class that will take a stack an within the stack call a 4 heap priority queue.  What I am having trouble with is that my ListNode class within the  "StackLList.h"  cannot used in the "PQ.h".  But, it works vice versa.  The error is saying:

Error      1      error C2061: syntax error : identifier 'ListNode'      c:\users\owner\documents\visual studio 2010\projects\4heap\4heap\pq.h      17      1      4Heap
PQ:
 
#ifndef PQ_H
#define PQ_H

#include<iostream>
#include<vector>
#include<cmath>

using namespace std;

#include"StackLList.h"

template<class Comparable>
class PQ
{
public:
	PQ();
	void insert(ListNode&);
	void delete_max();
	void swap(ListNode&, ListNode&);
	void min(ListNode&, ListNode&);
private:
	vector<ListNode> vector_PQ;
	int size;
};

template<class Comparable>
PQ<Comparable>::PQ()
{
	size = vector_PQ.size();
}

template<class Comparable>
void PQ<Comparable>::insert(ListNode &x)
{
	if(vector_PQ.size() == 0)
	{
		vector_PQ.push_back(x);
		return;
	}
	
		size++;
		vector_PQ.push_back(x);
	int child = size;
	int parent = (child/4);

	while(vector_PQ[parent] < vector_PQ[child])
	{
		swap(vector_PQ[parent], vector_PQ[child]);
		child = parent;
		parent = (child/4);
	}
}

template<class Comparable>
void PQ<Comparable>::delete_max()
{
	Comparable nextItem = vector_PQ[1];
	swap(vector_PQ[1], vector_PQ[size]);
	size--;

	int parent = 0;
	int child1 = (4*parent)+1;
	int child2 = (4*parent)+2;
	int child3 = (4*parent)+3;
	int child4 = (4*parent)+4;

	
}

template<class Comparable>
void PQ<Comparable>::swap(ListNode& a, ListNode& b)
{
	ListNode temp;

	temp = a;
	a = b;
	b = temp;
}

#endif

Open in new window


StackLList:
 
#ifndef STACKLLIST_H
#define STACKLLIST_H


#include<iostream>

using namespace std;

#include"PQ.h"

template<class Data>
class ListNode
{

public:
	
	 ListNode(Data &nodeValue, int counter_value)
      : value(nodeValue), next(NULL), counter(counter_value)  
		{}

	ListNode *next;
	Data value;
	int counter;

};

template<class Data>
class StackLList
{
public:
	StackLList();
	~StackLList();
	void push(Data&);
	void pop(Data&);
	bool isEmpty();

private:
	
	ListNode<Data> *top;
	PQ<Data> *queue;
};

template<class Data>
StackLList<Data>::StackLList()
{
	top = NULL;
	queue = new PQ<Data>;

}

template<class Data>
StackLList<Data>::~StackLList()
{
	ListNode *node_ptr, *next_node;

	node_ptr = top;

	while(node_ptr != NULL)
	{
		next_node = node_ptr->next;
		delete node_ptr;
		node_ptr = next_node;
	}
}

template<class Data>
void StackLList<Data>::push(Data& data)
{
	static int timeStamp = 0;
	timeStamp++;

	ListNode<Data> *new_node = new ListNode<Data>(data,timeStamp);

	

	if(isEmpty())
	{
		top = new_node;
		new_node->next = NULL;
	}

	else
	{
		new_node->next = top;
		top = new_node;
	}

	queue->insert(new_node);

}

template<class Data>
void StackLList<Data>::pop(Data& data)
{
	ListNode *temp;

	if(isEmpty())
		cout<<"The Stack Is Empty"<<endl;

	else
	{
		data = top->value;
		temp = top->next;
		delete top;
		top = temp;
	}

}

template<class Data>
bool StackLList<Data>::isEmpty()
{
	bool flag;

	if(top == NULL)
		flag =true;
	
	else
		flag = false;

	return flag;
}

#endif

Open in new window


Main:
 
#include<iostream>
#include<string>
using namespace std;

#include"StackLList.h"


int main()
{
	cout<<"****************** 4-HEAP PROGRAM *******************"<<endl;

	int number;
	int answer = 1;
	StackLList<int> *stack = new StackLList<int>;

	
	
	do
	{
		cout<<"1) Push"<<endl;
		cout<<"2) Pop"<<endl;
		cout<<"3) Exit"<<endl;
		cin>>answer;
		
		if(answer == 1)
		{
			cout<<"Please Enter In Your Data"<<endl;
			cin>>number;

			stack->push(number);

		}
		
		else if(answer == 2)
			cout<<"We Will Create A Delete Function"<<endl;
		
		else
			break;

	}while(answer != 3);

	system("PAUSE");

	return 0;
}

Open in new window

0
Comment
Question by:brich744
  • 5
  • 4
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 35463185
Well, 'ListNode' is a templated class and therefore needs a template argument to be recognized by the compiler - you should use
template<class Comparable,class Data>
class PQ
{
public:
	PQ();
	void insert(ListNode<Data>&);
	void delete_max();
	void swap(ListNode<Data>&, ListNode<Data>&);
	void min(ListNode<Data>&, ListNode<Data>&);
private:
	vector<ListNode<Data> > vector_PQ;
	int size;
};

Open in new window

0
 

Author Comment

by:brich744
ID: 35463373
I have changed the PQ class.  I am getting the same errors.
#ifndef PQ_H
#define PQ_H

#include<iostream>
#include<vector>
#include<cmath>

using namespace std;

#include"StackLList.h"

template<class Comparable, class Data>
class PQ
{
public:
	PQ();
	void insert(ListNode<Data>&);
	void delete_max();
	void swap(ListNode<Data>&, ListNode<Data>&);
	void min(ListNode<Data>&, ListNode<Data>&);
private:
	vector< ListNode<Data> > vector_PQ;
	int size;
};

template<class Comparable, class Data>
PQ<Comparable>::PQ()
{
	size = vector_PQ.size();
}

template<class Comparable, class Data>
void PQ<Comparable>::insert(ListNode<Data> &x)
{
	if(vector_PQ.size() == 0)
	{
		vector_PQ.push_back(x);
		return;
	}
	
		size++;
		vector_PQ.push_back(x);
	int child = size;
	int parent = (child/4);

	while(vector_PQ[parent] < vector_PQ[child])
	{
		swap(vector_PQ[parent], vector_PQ[child]);
		child = parent;
		parent = (child/4);
	}
}

template<class Comparable class Data>
void PQ<Comparable>::delete_max()
{
	Comparable nextItem = vector_PQ[1];
	swap(vector_PQ[1], vector_PQ[size]);
	size--;

	int parent = 0;
	int child1 = (4*parent)+1;
	int child2 = (4*parent)+2;
	int child3 = (4*parent)+3;
	int child4 = (4*parent)+4;

	
}

template<class Comparable, class Data>
void PQ<Comparable>::swap(ListNode& a, ListNode& b)
{
	ListNode<Data> temp;

	temp = a;
	a = b;
	b = temp;
}

#endif

Open in new window

0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 35463477
Aaah, I see - that's because 'StackLList.h' has a

#include"PQ.h"

at the beginning, which then pulls in the file with 'ListNode' still being undefined. Can you try the following?
#ifndef PQ_H
#define PQ_H

#include<iostream>
#include<vector>
#include<cmath>

using namespace std;

template<class Data> class ListNode;

template<class Comparable,class Data>
class PQ
{
public:
	PQ();
	void insert(ListNode<Data>&);
	void delete_max();
	void swap(ListNode<Data>&, ListNode<Data>&);
	void min(ListNode<Data>&, ListNode<Data>&);
private:
	vector<ListNode<Data> > vector_PQ;
	int size;
};

#include"StackLList.h"

template<class Comparable, class Data>
PQ<Comparable,Data>::PQ()
{
	size = vector_PQ.size();
}

template<class Comparable, class Data>
void PQ<Comparable,Data>::insert(ListNode<Data> &x)
{
	if(vector_PQ.size() == 0)
	{
		vector_PQ.push_back(x);
		return;
	}
	
		size++;
		vector_PQ.push_back(x);
	int child = size;
	int parent = (child/4);

	while(vector_PQ[parent] < vector_PQ[child])
	{
		swap(vector_PQ[parent], vector_PQ[child]);
		child = parent;
		parent = (child/4);
	}
}

template<class Comparable, class Data>
void PQ<Comparable,Data>::delete_max()
{
	Comparable nextItem = vector_PQ[1];
	swap(vector_PQ[1], vector_PQ[size]);
	size--;

	int parent = 0;
	int child1 = (4*parent)+1;
	int child2 = (4*parent)+2;
	int child3 = (4*parent)+3;
	int child4 = (4*parent)+4;

	
}

template<class Comparable, class Data>
void PQ<Comparable,Data>::swap(ListNode<Data>& a, ListNode<Data>& b)
{
	ListNode<Data> temp;

	temp = a;
	a = b;
	b = temp;
}


#endif

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 35463489
BTW, you wil have to change 'StackLList' also, i.e.
template<class Data, class Comparable>
class StackLList
{
public:
	StackLList();
	~StackLList();
	void push(Data&);
	void pop(Data&);
	bool isEmpty();

private:
	
	ListNode<Data> *top;
	PQ<Comparable,Data> *queue; // required now
};

Open in new window

0
 

Author Comment

by:brich744
ID: 35463724
I have made the adjustments that you have suggested and now I am getting an error

Error      1      error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'ListNode<Data>'      c:\users\owner\documents\visual studio 2010\projects\4heap\4heap\pq.h      48      1      4Heap


It is directing me to the PQ.h file inside of the insert file.

 
template<class Comparable, class Data>
void PQ<Comparable, Data>::insert(ListNode<Data> &x)
{
	if(vector_PQ.size() == 0)
	{
		vector_PQ.push_back(x);
		return;
	}
	
		size++;
		vector_PQ.push_back(x);
	int child = size;
	int parent = (child/4);

	while(vector_PQ[parent] < vector_PQ[child])//C2784
	{
		swap(vector_PQ[parent], vector_PQ[child]);
		child = parent;
		parent = (child/4);
	}
}

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 35463734
Hm, isn't that exactly the place where you should use 'Comparable' for that purpose? At least 'vector_PQ[x]' is of the type 'ListNode<Data', which is unknown to the compiler at that time.
0
 

Author Comment

by:brich744
ID: 35463809
I am sorry but what do you mean I should use Comparable in that instance?
0
 

Author Comment

by:brich744
ID: 35463847
Never mind I see my mistake.
0
 
LVL 86

Expert Comment

by:jkr
ID: 35464064
Sorry for not responding earlier, have been offline for a while. Great that you spoted the final issue yourself ;o)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Suggested Courses

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question