11 Replies - 1089 Views - Last Post: 09 October 2012 - 07:10 AM Rate Topic: -----

#1 amc0029  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-October 12

unsorted linked list, undelcared identifier

Posted 08 October 2012 - 02:19 PM

I am using Microsoft Visual Studio 2008 C++. I am having a problem with my unsorted linked list. I keep recieving an error saying undeclared identifier 'info'... but it is clearly defined inside my struct. i am not sure exatly what to do.

My program is to put different people into different companies and demote(moe down the list).promote(move up list) and so on


Im also guessing each company has its own seperate linked list??



here are my UnsortedType header and cpp, and my ItemType header and cpp files. I know that i have not created a main function but it should still compile with only the error of not having a main...



//ItemType header file

#include <fstream>
#include <cstdlib>


const int MAX_ITEMS = 20;
enum RelationType {LESS,GREATER,EQUAL};



class ItemType
{
public:
	ItemType();
	RelationType ComparedTo(ItemType) const;
	void Print (std::ofstream&) const;
	void Initialize(int number);
private:
	int value;
};



//ItemType cpp file

#include<fstream>
#include<iostream>
#include<cstdlib>
#include "ItemType.h"
using namespace std;

ItemType::ItemType():value(0)
{
}

RelationType ItemType::ComparedTo(ItemType otherItem) const
{
	if(value < otherItem.value)
		return LESS;
	else if(value > otherItem.value)
		return GREATER;
	else return EQUAL;
}

void ItemType::Initialize(int number)
{
	value = number;
}

void ItemType::Print(std::ofstream& out) const
{
	out << value << " ";
}



//UnsortedType header file




#include <iostream>
#include <cstdlib>
#include "ItemType.h"



struct NodeType;    // Im not sure exactly where im suppose to declare this and i think this is what is causing the problem

class UnsortedType
{
public:
	UnsortedType();
	void MakeEmpty();
	bool IsFull() const;
	int GetLength() const;
	ItemType GetItem(ItemType item, bool& found);
	void PutItem(ItemType item);
	void DeleteItem(ItemType item);
	void ResetList();
	ItemType GetNextItem();

private:
	NodeType* listData;
	int length;
	NodeType* currentPos;

};


// UnsortedType cpp file


#include "UnsortedType.h"
#include <iostream>
#include <cstdlib>
using namespace std;

struct NodeType                          // this is where i declared the struct NodeType from above
	{
		ItemType info;
		NodeType* next;
	};


void UnsortedType::PutItem(ItemType item)
{
	NodeType* location;

	location = new NodeType;
	location->info = item;
	location->next = listData;

	listData = location;

	length++;
}



UnsortedType::UnsortedType()
{
	length = 0;
	listData = NULL;
}

bool UnsortedType::IsFull() const
{
	NodeType* location;
	try
	{
		location = new NodeType;
		delete location;
		return false;
	}
	catch(std::bad_alloc excecption)
	{
		return true;
	}
}

int UnsortedType::GetLength() const
{ 
	return length;
}


void UnsortedType::MakeEmpty()
{
	NodeType* tempPtr;
	
	while(listData != NULL)
	{
		tempPtr = listData;
		listData = listData->next;
		delete tempPtr;
	}
	length = 0;
}



ItemType UnsortedType::GetItem(ItemType item, bool& found)
{
	bool moreToSearch;
	NodeType* location;

	location = listData;
	found = false;
	moreToSearch = (location != NULL);
		while (moreToSearch && !found)
		{
		switch (item.ComparedTo(info[location]))                             // this is where the error occurs
			{
			case LESS :
			case GREATER: location = location->next;
						  moreToSearch = (location != NULL);
						  break;
			case EQUAL: found = true;
						item = location->info;
						break;
			}
		}
		return item;
}



void UnsortedType::DeleteItem(ItemType item)
{
	NodeType* location = listData;
	NodeType* tempLocation;

	if(item == listData->info)
	{
		tempLocation = location;
		listData = listData->next;
	}
	else
	{
		while (!(item == (location->next)->info))
		{
			location = location->next;
			tempLocation = location->next;
			location->next = (location->next)->next;
		}
	}
	delete tempLocation;
	length--;
}

void UnsortedType::ResetList ()
{
	currentPos = NULL;
}

ItemType UnsortedType::GetNextItem()
{
	if (currentPos == NULL)
		currentPos = listData;
	else
		currentPos = currentPos->next;
	return currentPos->info;
}


//error messages

documents\visual studio 2008\projects\program2\unsortedtype.cpp(81) : error C2065: 'info' : undeclared identifier
documents\visual studio 2008\projects\program2\unsortedtype.cpp(102) : error C2784: 'bool std::operator ==(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'ItemType'
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\string(90) : see declaration of 'std::operator =='

//This basically keeps repeating for several lines and the last error message before fatal error is below

documents\visual studio 2008\projects\program2\unsortedtype.cpp(109) : error C2676: binary '==' : 'ItemType' does not define this operator or a conversion to a type acceptable to the predefined operator




any help would be greatly appreciated

MOD EDIT: Added code tags. When posting code, USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 08 October 2012 - 02:29 PM


Is This A Good Question/Topic? 0
  • +

Replies To: unsorted linked list, undelcared identifier

#2 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 770
  • Joined: 27-June 09

Re: unsorted linked list, undelcared identifier

Posted 08 October 2012 - 02:27 PM

info is not defined at the line where the error occurs. You need to specify the NodeType that the info belongs to. Is it supposed to be location->info instead of info[location]?
Was This Post Helpful? 0
  • +
  • -

#3 amc0029  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-October 12

Re: unsorted linked list, undelcared identifier

Posted 08 October 2012 - 02:32 PM

wow. that took the error away, thank you so much mojo... the way i posted it, is exactly as the book says, i dont know why the book would put that in there if it wasnt right... and one more question on the bool operator == , do i just overload the operator == inside the ItemType class??
Was This Post Helpful? 0
  • +
  • -

#4 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 770
  • Joined: 27-June 09

Re: unsorted linked list, undelcared identifier

Posted 08 October 2012 - 02:36 PM

Yes, if you want to compare two ItemTypes with the == operator, you would overload it in the ItemType class.
Was This Post Helpful? 0
  • +
  • -

#5 amc0029  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-October 12

Re: unsorted linked list, undelcared identifier

Posted 08 October 2012 - 02:40 PM

so even though the error occurs right here



void UnsortedType::DeleteItem(ItemType item)

100 {

101 NodeType* location = listData;

102 NodeType* tempLocation;

103

104 if(item == listData->info) // errror occurs right here

105 {

106 tempLocation = location;

107 listData = listData->next;

108 }

109 else

110 {

111 while (!(item == (location->next)->info)) // and im pretty sure it would occur here too

112 {

113 location = location->next;

114 tempLocation = location->next;

115 location->next = (location->next)->next;

116 }

117 }

118 delete tempLocation;

119 length--;

120 }




i still overload the == operator in the ItemType class??
Was This Post Helpful? 0
  • +
  • -

#6 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 770
  • Joined: 27-June 09

Re: unsorted linked list, undelcared identifier

Posted 08 October 2012 - 02:47 PM

Yes. Overloading an operator really is no different than defining a function in a class. Right now, the error occurs at those lines because the compiler does not have any definitions that match that operator, so it is telling you that it doesn't understand what those lines mean. It would be the same if you said "if(listData->info.NoFunction())". It would give a similar error at that line and would be fixed by defining NoFunction in the class. When you provide the definitions in the class, the compiler will know how to handle those lines.
Was This Post Helpful? 0
  • +
  • -

#7 amc0029  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-October 12

Re: unsorted linked list, undelcared identifier

Posted 08 October 2012 - 02:52 PM

awesome! thank you mojo... im sure ill have more questions once i get to loading names into different companies, but thank you for the help
Was This Post Helpful? 0
  • +
  • -

#8 amc0029  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-October 12

Re: unsorted linked list, undelcared identifier

Posted 08 October 2012 - 03:06 PM

i am now creating a runtime stack overflow... goodness, any help is appreciated


documents\visual studio 2008\projects\program2\itemtype.cpp(14) : warning C4717: 'operator==' : recursive on all control paths, function will cause runtime stack overflow




here is where i am trying to compare the two items


void UnsortedType::DeleteItem(ItemType item)
{
NodeType* location = listData;
NodeType* tempLocation;

if(item == listData->info) // this is where im trying to compare both of them
{
tempLocation = location;
listData = listData->next;
}
else
{
while (!(item == (location->next)->info))
{
location = location->next;
tempLocation = location->next;
location->next = (location->next)->next;
}
}
delete tempLocation;
length--;
}



friend bool operator ==(ItemType& item1,ItemType& item2);






bool operator ==(ItemType& item1, ItemType& item2)
{
return (item1 == item2);
}
Was This Post Helpful? 0
  • +
  • -

#9 amc0029  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-October 12

Re: unsorted linked list, undelcared identifier

Posted 08 October 2012 - 03:22 PM

nvm, i fixed that... can you read a string into a list or do you need to create a character array for each element in the list??
Was This Post Helpful? 0
  • +
  • -

#10 amc0029  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-October 12

Re: unsorted linked list, undelcared identifier

Posted 08 October 2012 - 05:36 PM

okay so i have finally gotten my program to compile and was able to get some of these functions to output certain data of list, like the functions who have a return type... i am having trouble inserting, say any number or charcter into the list and actually storing it.... the only way i can print this list is to do a while statement and print within the while statement... i am having trouble learning how to actually store these items, and delete an item if need be, or print this list at a seperate list... i have multiple lists... i am having trouble with main function


ANY HELP WOULD BE GREATLY APPRECIATED!!

here is a sample main that im having trouble with

#include "UnsortedType.h"
#include <iostream>
using namespace std;

int main()
{
	UnsortedType myList[20];
	ItemType list[20];
	int a = 0;
	int b = 0;
	int c = 0;

	ofstream out;

	out.open("OutputData.txt");
	while(a<20)
	{
		list[0].Initialize();
		list[1].Initialize();
		myList[b].PutItem(list[b]);
		a++;
		c++;
		list[0].Print(out);
		cout << myList[0].GetLength() << " ";
		list[1].Print(out);
		
	}

	myList[0].ResetList();
	cout << myList[0].IsFull();

        return 0;
}


This post has been edited by Oler1s: 08 October 2012 - 10:01 PM
Reason for edit:: Code needs to be posted in CODE tags

Was This Post Helpful? 0
  • +
  • -

#11 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: unsorted linked list, undelcared identifier

Posted 08 October 2012 - 10:02 PM

Code (or computer output, etc.) goes into CODE tags.
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3538
  • View blog
  • Posts: 10,952
  • Joined: 05-May 12

Re: unsorted linked list, undelcared identifier

Posted 09 October 2012 - 07:10 AM

Post your completed updated code. In post #10, you are calling ItemType::Initialize() with no parameters on lines 18-19, but based on your post #1, ItemType does not have an Initialize() method that take no parameters.

Additionally, be sure to tell us what behavior you are seeing and what you were expecting to see.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1