We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Error 2061: cannot find the identifer

brich744
brich744 asked
on
Medium Priority
365 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

Comment
Watch Question

jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
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

Author

Commented:
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

CERTIFIED EXPERT
Top Expert 2012
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
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

Author

Commented:
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

jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
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.

Author

Commented:
I am sorry but what do you mean I should use Comparable in that instance?

Author

Commented:
Never mind I see my mistake.
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
Sorry for not responding earlier, have been offline for a while. Great that you spoted the final issue yourself ;o)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.