GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-May-2005, 13:17
Requoter Requoter is offline
Awaiting Email Confirmation
 
Join Date: Mar 2005
Posts: 11
Requoter is on a distinguished road

Classes problem


Hi everyone!
I'm working on this program that reads the unsorted list into an array, sorts it, inserts and deletes the number. Everything worked fine until I had to break it into classes.

Here's the main code:

CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include "sortedList.h"


using namespace std;

ifstream inData;
ofstream outData;

int main () 
{ 

	inData.open ("lab4a.dat");
	outData.open ("lab4a.out");

	cout << "\nUnsorted List:" << endl << endl;
	outData << "\nUnsorted List:" << endl << endl;
	SortedList::getData();
	SortedList::printIt();
	cout << "\nSorted List:" << endl << endl;
	outData << "\nSorted List:" << endl << endl;
	SortedList::selSort();
	SortedList::printIt();
	SortedList::Insert();
	SortedList::printIt();
	SortedList::Delete();
	SortedList::printIt();

inData.close();
outData.close();

return 0;
}

This is my specification file:

CPP / C++ / C Code:
#include "sortedList.cpp"

class SortedList
{
public:

	void getData();
	void printIt();
	void selSort();
	void Insert();
	void Delete();

private:

	int myArray[50];
	int item;
	int i;
	int count = 0;
	void binSearch(bool& found, int& position) const;

};

And this is the implementation file:

CPP / C++ / C Code:
#include "sortedList.h"
#include <fstream>

using namespace std;

void SortedList::getData()

{
	while (inData)
	{
		inData >> myArray[count];
		
		count++;
	}

count--;

}

void SortedList::printIt()
{
	for (i = 0; i < count ; i++)
	{
	cout << setw(4) << myArray[i] << endl;
	outData << setw(4) << myArray[i] << endl;
	}
}

void SortedList::selSort()
{
	int temp;
	int passCount;
	int searchIndx;
	int minIndx;

	for (passCount = 0; passCount < count - 1; passCount++)
	{
		minIndx = passCount;

		for (searchIndx = passCount + 1; searchIndx < count; searchIndx++)

			if (myArray[searchIndx] < myArray[minIndx])
				minIndx = searchIndx;
	
	temp = myArray[minIndx];
	myArray[minIndx] = myArray[passCount];
	myArray[passCount] = temp;
	}
}

void SortedList::Insert()
{
	int index;

	cout << "\nEnter the number you'd like to add to the list:" << endl;
	outData << "\nEnter the number you'd like to add to the list:" << endl << endl;

	cin >> item;
	outData << item << endl;

	index = count - 1;
	
	while (index >= 0 && item < myArray[index])
	{
		myArray[index+1] = myArray[index];
		index--;
	}
	myArray[index+1] = item;
	count++;
}

void SortedList::binSearch(bool& found, int& position)
{
	int first = 0;
	int last = count - 1;
	int middle;

	found = false;
	while (last >= first && !found)
	{
		middle = (first + last) / 2;
		if (item < myArray[middle])
			last = middle - 1;
		else if (item > myArray[middle])
			first = middle + 1;
		else
			found = true;
	}
	if (found)
		position = middle;
}

void SortedList::Delete()
{
	int index;
	bool found;
	int position;

	cout << "\nEnter the number you'd like to remove from the list:" << endl;
	outData << "\nEnter the number you'd like to remove from the list:" << endl << endl;

	cin >> item;
	outData << item << endl;

	binSearch(found, position);

	if (found)
	{
		for (index = position; index < count - 1; index++)
			myArray[index] = myArray[index+1];
		count--;
	}
}

When I compile it, it gives me the following error messages:

warning C4182: #include nesting level is 362 deep; possible infinite recursion
fatal error C1076: compiler limit : internal heap limit reached; use /Zm to specify a higher limit


If I try to input the /Zm800 in project settings but it returns with this error:

fatal error C1014: too many include files : depth = 1024

What am I doing wrong?
Thanks in advance.
  #2  
Old 01-May-2005, 14:15
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Wait until you see all the errors on the next level in...

You don't need to do this in your .h file

Quote:
Originally Posted by Requoter
CPP / C++ / C Code:
#include "sortedList.cpp"

class SortedList
{
public:

// Removed Code 

You also may want to use inclusion guards on your .h file
CPP / C++ / C Code:
//#include "sortedList.cpp"
#if !defined __sortedList_cpp__
#define __sortedList_cpp__
class SortedList
{
public:

  void getData();
  void printIt();
  void selSort();
  void Insert();
  void Delete();

private:

  int myArray[50];
  int item;
  int i;
  int count;
  void binSearch(bool& found, int& position) const;

};
#endif

This way if you have multiple objects the definition is only included once. Or something of that nature. My compiler (gnu g++ cygwin version) doesn't seem to mind if they are there or not but I think some do. Mostly though, just get rid of the .cpp include and compile it with your main.cpp

Code:
g++ -Wall main.cpp sortedList.cpp -o MyMain

That will get you past your problem. If you need help with any more of it let us know.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #3  
Old 01-May-2005, 14:20
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough
you'll also need an instance of SortedList before you can call its functions. And your SortedList class is flawed--for example, in getData(), what is inData? I don't see a declaration or a parameter.
  #4  
Old 01-May-2005, 14:37
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Quote:
Originally Posted by ubergeek
you'll also need an instance of SortedList before you can call its functions. And your SortedList class is flawed--for example, in getData(), what is inData? I don't see a declaration or a parameter.

Actually there are quite a few problems for the OP to get on, but lets burn that chicken on the other side of the fence when we get there shall we.

inData actually is a global in the main.cpp file. So declaring it in the implementation file as:

CPP / C++ / C Code:
extern ifstream inData;
extern ofstream outData;

would take care of that. I think that is the intent, not necesarily the correct way to go about it though. As Requoter said, this is all being moved to using classes they are essentially artifacts off the move. Still some boxes to unpack yet if you will.

Requoter, could you provide the test datafile so we are using the same input. Since there is no check to see, and as Uber noted no object created yet, the fact that I have no lab4a.dat is merrily ignored until I run the print routine on no data.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #5  
Old 02-May-2005, 13:52
Requoter Requoter is offline
Awaiting Email Confirmation
 
Join Date: Mar 2005
Posts: 11
Requoter is on a distinguished road
Quote:
This way if you have multiple objects the definition is only included once. Or something of that nature. My compiler (gnu g++ cygwin version) doesn't seem to mind if they are there or not but I think some do. Mostly though, just get rid of the .cpp include and compile it with your main.cpp

The problem is I'm only in the process of studying C++, so my knowledge of it is pretty limited. I was just creating classes the way our instructor told us to and following the book. I really didn't expect it to work the first time. It never does

Quote:
Requoter, could you provide the test datafile so we are using the same input. Since there is no check to see, and as Uber noted no object created yet, the fact that I have no lab4a.dat is merrily ignored until I run the print routine on no data.

It wouldn't let me attach the .dat file here, so I had to go with .txt.
Attached Files
File Type: txt lab4a.txt (165 Bytes, 13 views)
  #6  
Old 02-May-2005, 14:27
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline
Junior Member
 
Join Date: Apr 2005
Location: Arizona
Posts: 35
Stack Overflow will become famous soon enough
Hello,

I looked over you code and saw a few slight issues. One was that of your fstream file identifiers, and your main calls. I went ahead and created a Makefile to compile your source code, and have tested your program. It works quite nicely on my end after I made a few changes. You can view the new source code called requoter_src.zip.

I added comments to the main.cpp and sortedList.h file. I slightly modified sortedList.cpp, but nothing major. I just removed some #include calls.

My first change was to move the ifstream and ofstream variables inside your class. That makes things easier to handle. Also, I created a local variable to your class called sList in main(). Lastly I made a default constructor to set count to 0. ISO C++ forbids initialization of non-constant static members inside a class. As in:

CPP / C++ / C Code:
private:
	int count = 0; // Forbidden

You will also notice an #ifndef macros inside the sortedList.h file. The #ifdef (if defined) and #ifndef (if not defined) preprocessor commands are used to test if a preprocessor variable has been "defined". There are two common uses for this, with slightly different patterns. When there are definitions in a header file that can not be made twice, the code below should be used. A header file may be included twice other include files include it, or an included file includes it and the source file includes it again.

To prevent bad effects from a double include, it is common to surround the body in the include file with the following (where MYHEADER_H is replaced by a name that is appropriate for your program).

CPP / C++ / C Code:
#ifndef MYHEADER_H
#define MYHEADER_H
. . .    // This will be seen by the compiler only once
#endif /* MYHEADER_H */

Overall, your code and class was fine except for minor anomolies. If you have any questions, please feel free to ask.


- Stack Overflow
Attached Files
File Type: zip requoter_src.zip (2.2 KB, 16 views)
__________________
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [C] / [C++] tags. Your question may have been asked before, try the search facility.
  #7  
Old 02-May-2005, 14:30
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Ok, a few things, did you get rid of the loop when trying to compile? If so, what are your errors now?

You need to consider how you are handling your global variables inData and outData. Before, they look like they were just globals. Now, you might want to consider changing your class member functions to accept a reference to them. Perhaps it would be better to just add them to your class. Once you get the basics together...

nevermind. Looks like Stack runnith over...

Good Luck with it.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #8  
Old 02-May-2005, 15:39
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough
Ok, a few things to note on Stack's reply.

Quote:
Originally Posted by StackOverflow
You will also notice an #ifndef macros inside the sortedList.h file. The #ifdef (if defined) and #ifndef (if not defined) preprocessor commands are used to test if a preprocessor variable has been "defined". There are two common uses for this, with slightly different patterns. When there are definitions in a header file that can not be made twice, the code below should be used. A header file may be included twice other include files include it, or an included file includes it and the source file includes it again.

To prevent bad effects from a double include, it is common to surround the body in the include file with the following (where MYHEADER_H is replaced by a name that is appropriate for your program).

A very good explanation. A good point to remember that that header could be included from a number of locations. In fact, in this small example it is already being included from 2 (main.cpp and sortedList.cpp). Since we want to consider the effects of your header being included by other files you may want to remember that direct includes bring some other fun with them.


Quote:
Originally Posted by StackOverflow (header)
CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

What this has done is force the calling program into taking on the namespace std in total. This is not a great idea generally but really a bad idea in a header. Because of the includes and namespace use in the header your main now is using the standard namespace. How else would the cout work.

Another solution would be:

CPP / C++ / C Code:
// in main.cpp
#include <iostream>
using std::cout;
using std::endl;

// in sortedList.h
#include <fstream>
using std::ifstream;
using std::ofstream;

// in sortedList.cpp
#include <iostream>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::setw;

It's a bit more typing but will only include the actual items you are using. Something to definately keep in mind in the header. This also serves as a good reminder of what and from where you are using something. This is also a good example of why you use inclusion guards. Your code is then self sufficient on a per file basis so changes elsewhere won't cause your code (main.cpp in this example) to stop working.

Just some things to keep in mind.

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #9  
Old 03-May-2005, 00:00
Requoter Requoter is offline
Awaiting Email Confirmation
 
Join Date: Mar 2005
Posts: 11
Requoter is on a distinguished road
Quote:
Originally Posted by Stack Overflow
Hello,

I looked over you code and saw a few slight issues. One was that of your fstream file identifiers, and your main calls. I went ahead and created a Makefile to compile your source code, and have tested your program. It works quite nicely on my end after I made a few changes. You can view the new source code called requoter_src.zip.

I've tried your version. It compiles everything, but for some reason wouldn't build it, giving these errors:

error LNK2001: unresolved external symbol "public: void __thiscall SortedList:elete(void)" (?Delete@SortedList@@QAEXXZ)
error LNK2001: unresolved external symbol "public: void __thiscall SortedList::Insert(void)" (?Insert@SortedList@@QAEXXZ)
error LNK2001: unresolved external symbol "public: void __thiscall SortedList::selSort(void)" (?selSort@SortedList@@QAEXXZ)
error LNK2001: unresolved external symbol "public: void __thiscall SortedList:rintIt(void)" (?printIt@SortedList@@QAEXXZ)
error LNK2001: unresolved external symbol "public: void __thiscall SortedList::getData(void)" (?getData@SortedList@@QAEXXZ)
Debug/lab4b.exe : fatal error LNK1120: 5 unresolved externals
  #10  
Old 03-May-2005, 10:44
Requoter Requoter is offline
Awaiting Email Confirmation
 
Join Date: Mar 2005
Posts: 11
Requoter is on a distinguished road
Yay! Got it working. Thanks everyone for your help. I really appreciate it.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache / PHP problem, maybe output length? HaganeNoKokoro Apache Web Server Forum 3 07-Aug-2008 05:42
Need advice on a disturbing problem JUNK KED Open Discussion Forum 6 31-Mar-2005 14:51
problem modifying an array of char in a function ronin C Programming Language 10 28-Mar-2005 19:15
Fairly simple classes help please sammacs C++ Forum 0 30-Nov-2004 10:58
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 08:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:54.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.