Hey guys,
I'm getting an error at lines #9 and #14. Could someone tell me what I'm doing wrong?

StackType::StackType(const StackType & st)	// copy c-tor
{ 
	if ( st.topPtr == NULL )
		topPtr = NULL;
	else			    
	{	
		NodeType * stptr;	// pointer to source Stack
		NodeType * p;	// pointer to target Stack  
		topPtr = new NodeType(st.topPtr->info);    // first node
		stptr = st.topPtr->next;	// point to second Node
		p = topPtr;        // initialize pointer to Stack copy
		while ( stptr != NULL )   // deep copy other nodes
		{	
			p->next = new NodeType(stptr->info);
			p = p->next;	// update pointers
			stptr = stptr->next;
		} // endwhile
		p->next = NULL;
	} // endif
}

Recommended Answers

All 8 Replies

>>Could someone tell me what I'm doing wrong?
No because you need to post the class and the error message(s)

Oops. Here are my errors (same in both spots):

Error 1 error C2664: 'NodeType::NodeType(const NodeType &)' : cannot convert parameter 1 from 'ItemType' to 'const NodeType &' j:\Projects\ch5 identical linked stack\ch5 identical linked stack\StackType.cpp 66 ch5 identical linked stack

again you have to post the class declaration. But my guess from that error message is that the parameter in the function prototype is not the same as in the function implementation. But the only way to tell is for you to post the class.

Here's all of my code, to avoid further confusion. My apologies.

#pragma once
#include <iostream>
using namespace std ;

typedef char ItemType; 
struct NodeType
{
	ItemType info ;
	NodeType* next ;
};

class FullStack
// Exception class thrown by Push when stack is full.
{};

class EmptyStack
// Exception class thrown by Pop and Top when stack is emtpy.
{};

class StackType
{
public:
  StackType();		
  StackType(const StackType & copy) ;
  ~StackType();		
  void Push(ItemType);
  void Pop();
  ItemType Top();
  bool IsEmpty() const;
  bool IsFull() const;
  bool Identical(StackType stack) ;
private:
  NodeType* topPtr;
  int length ;
};



#include "StackType.h"

// Implementation file for linked StackType
void StackType::Push(ItemType newItem)
// Adds newItem to the top of the stack.
// Stack is bounded by size of memory.
// Pre:  Stack has been initialized.
// Post: If stack is full, FullStack exception is thrown;
//       else newItem is at the top of the stack.

{
  if (IsFull())
    throw FullStack();
  else
  {
    NodeType* location;
    location = new NodeType;
    location->info = newItem;
    location->next = topPtr;
    topPtr = location;
	length++ ;
  }
}
void StackType::Pop()
// Removes top item from Stack and returns it in item.
// Pre:  Stack has been initialized.
// Post: If stack is empty, EmptyStack exception is thrown;
//       else top element has been removed.
{
  if (IsEmpty())
    throw EmptyStack();
  else
  {  
    NodeType* tempPtr;
    tempPtr = topPtr;
    topPtr = topPtr->next;
    delete tempPtr;
	length++ ;
  }
}
ItemType StackType::Top()
// Returns a copy of the top item in the stack.
// Pre:  Stack has been initialized.
// Post: If stack is empty, EmptyStack exception is thrown;
//       else a copy of the top element is returned.
{
  if (IsEmpty())
    throw EmptyStack();
  else
    return topPtr->info;  
}
StackType::StackType()	// Class constructor.
{
    topPtr = NULL;
	length = 0 ;
}

StackType::StackType(const StackType & st)	// copy c-tor
{ 
	if ( st.topPtr == NULL )
		topPtr = NULL;
	else			    
	{	
		NodeType * stptr;	// pointer to source Stack
		NodeType * p;	// pointer to target Stack  
		topPtr = new NodeType(st.topPtr->info);    // first node
		stptr = st.topPtr->next;	// point to second Node
		p = topPtr;        // initialize pointer to Stack copy
		while ( stptr != NULL )   // deep copy other nodes
		{	
			p->next = new NodeType(stptr->info);
			p = p->next;	// update pointers
			stptr = stptr->next;
		} // endwhile
		p->next = NULL;
	} // endif
}

bool StackType::IsFull() const
// Returns true if there is no room for another ItemType 
//  on the free store; false otherwise.
{
  NodeType* location;
  try
  {
    location = new NodeType;
    delete location;
    return false;
  }
  catch(std::bad_alloc exception)
  {
    return true;
  }
}

bool StackType::IsEmpty() const
{
	return (topPtr == NULL) ;
}

StackType::~StackType()
// Post: stack is empty; all items have been deallocated.
{
  NodeType* tempPtr;

  while (topPtr != NULL)
  {
    tempPtr = topPtr;
    topPtr = topPtr->next;
    delete tempPtr;
  }
}

bool StackType::Identical(StackType stack)
{
	NodeType* tmp = stack.topPtr ;
	NodeType* tmp2 = topPtr ;
	bool iden = true ;

	if (length != stack.length)
		iden = false ;

	while(tmp->next != NULL && tmp2->next != NULL && iden)
	{
		if (tmp->info == tmp2->info)
		{
			iden = true ;
			tmp = tmp->next ;
			tmp2 = tmp2->next ;
		}
		else
			iden = false ;
	}

	return iden ;	
}


#include "StackType.h"

int main()
{
	ItemType a = 'a' ;
	ItemType b = 'b' ;
	ItemType c = 'c' ;
	ItemType d = 'd' ;

	StackType stack ;
	stack.Push(a) ;
	stack.Push(b) ;
	stack.Push(c) ;
	stack.Push(d) ;

	StackType stack_two ;
	stack_two.Push(a) ;
	stack_two.Push(b) ;
	stack_two.Push(c) ;
	stack_two.Push(d) ;

	bool iden = stack.Identical(stack_two) ;
	if (iden)
		cout << "Identical!\n" ;
	else
		cout << "Not Identical!\n" ;

	cout << "\n-- Program Complete --\n" ;

	return 0 ;
}

The only reason I'm even trying to do the copy c-tor is because after the program runs, it gives me a run-time error at the very end when it tries to destruct the already destructed memory. I figure this is the only way around that?

NodeType is a structure and you have not declared a constructor for it. Fix it like this:

struct NodeType
{
	ItemType info ;
	NodeType* next ;
    NodeType(ItemType t) {info = t; next = NULL;}
    NodeType() {info = 0; next = NULL;}
};

Hm, now I'm getting LINK errors:

Error 2 error LNK2020: unresolved token (0A0002C1) "public: bool __thiscall StackType::Identical(class StackType)" (?Identical@StackType@@$$FQAE_NV1@@Z) main.obj ch5 identical linked stack


Error 1 error LNK2034: metadata inconsistent with COFF symbol table: symbol '?Identical@StackType@@$$FQAE_NV1@@Z' (06000043) has inconsistent metadata with (0A0002C1) in main.obj StackType.obj ch5 identical linked stack


Error 3 fatal error LNK1120: 1 unresolved externals J:\Projects\ch5 identical linked stack\Debug\ch5 identical linked stack.exe 1 ch5 identical linked stack

must be some other problem because I didn't get any link errors when I put all that code into one *.cpp file and commented out the #include "StackType.h".

Might have something to do with the fact I was running it off of my flash drive. I'll give it a whirl again tomorrow.

Thanks for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.