Archived

This topic is now archived and is closed to further replies.

capn_midnight

recently, my code dies, I don't know why

Recommended Posts

#include <iostream>
#include <fstream>
#include <string>
//Sean T. McBeth

using namespace std;

class Sequence{
public:
	bool addS(int, const string&);
	bool del(int);
	void output() const;
	Sequence() : last(-1){}
	Sequence(const char*);
	~Sequence();
protected:
	enum{MaxStr=50};
	string s[MaxStr];
	int last;
private:
	string filename;
	ifstream ifs;
	ofstream ofs;
};

bool Sequence::addS(int pos, const string& entry){
	if(last==MaxStr-1 || pos<0 || pos>last+1) return false;
	for(int i=last;i>=pos;i--)
		s[i+1]=s[i];
	s[pos]=entry;
	last++;
	return true;
}
bool Sequence::del(int pos){
	if(pos<0 || pos>last) return false;
	for(int i=pos;i<last;i++)
		s[i]=s[i+1];
	last--;
	return true;
}
void Sequence::output() const{
	for(int i=0;i<=last;i++)
		cout<<i<<" "<<s[i]<<endl;
	cout<<endl;
}
Sequence::Sequence(const char* fname){
	last=-1;
	filename=fname;
	ifs.open(fname);
	if(!ifs)
		return;
	while(last<MaxStr-1 && getline(ifs, s[last+1], ''\n'')){
		last++;
	}
	ifs.close();
}
Sequence::~Sequence(){
	if(filename=="")
		return;
	ofs.open(filename.c_str());
	for(int i=0;i<=last;i++)
		ofs<<s[i]<<endl;
	ofs.close();
}

class SortedSeq:public Sequence{
public:
	bool addSS(const string&);
	SortedSeq();
	SortedSeq(const char*);
protected:
	void sort();
private:
	using Sequence::addS;
};
void SortedSeq::sort(){
	string temp;
	int i, j;
	for(i=0;i<last;i++){
		for(j=i+1;j<=last;j++){
			if(s[j]<s[i]){
				temp=s[j];
				s[j]=s[i];
				s[i]=temp;
			}
		}
	}
}
bool SortedSeq::addSS(const string& entry){
	int i;
	for(i=0;i<=last;i++){
		if(entry>s[i])
			break;
	}
	return addS(i, entry);
};
SortedSeq::SortedSeq(const char* fname):Sequence(fname){sort();}


class RevSortedSeq:public Sequence{
public:
	bool addRSS(const string&);
	RevSortedSeq();
	RevSortedSeq(const char*);
protected:
	void sort();
private:
	using Sequence::addS;
};
void RevSortedSeq::sort(){
	string temp;
	int i, j;
	for(i=0;i<last;i++){
		for(j=i+1;j<=last;j++){
			if(s[j]>s[i]){
				temp=s[j];
				s[j]=s[i];
				s[i]=temp;
			}
		}
	}
}
bool RevSortedSeq::addRSS(const string& entry){
	int i;
	for(i=0;i<=last;i++){
		if(entry<s[i])
			break;
	}
	return addS(i, entry);
};
RevSortedSeq::RevSortedSeq(const char* fname):Sequence(fname){sort();}


int main(void){
	Sequence s1("input.txt");
	SortedSeq s2("input.txt");
	RevSortedSeq s3("input.txt");
	s1.output();
	s2.output();
	s3.output();
	s1.addS(3,"Ursula");
	s2.addSS("Ursula");
	s3.addRSS("Ursula");
	s1.output();
	s2.output();
	s3.output();
	return 0;
}
/*****output******
0 mary
1 mike
2 sean
3 dave
4 jane
5 john
6 kim
7 sam
8 tim
9 phil

0 dave
1 jane
2 john
3 kim
4 mary
5 mike
6 phil
7 sam
8 sean
9 tim

0 tim
1 sean
2 sam
3 phil
4 mike
5 mary
6 kim
7 john
8 jane
9 dave

0 mary
1 mike
2 sean
3 Ursula
4 dave
5 jane
6 john
7 kim
8 sam
9 tim
10 phil

0 dave
1 jane
2 john
3 kim
4 mary
5 mike
6 phil
7 sam
8 sean
9 tim
10 Ursula

0 Ursula
1 tim
2 sean
3 sam
4 phil
5 mike
6 mary
7 kim
8 john
9 jane
10 dave
**********/
Over the past two weeks ANY program that I write in VS (either 6.0 or .NET) gives me an error ON PROGRAM EXIT.
quote:
''sequence.exe'': Loaded ''C:\Documents and Settings\Student\My Documents\Visual Studio Projects\sequence\Debug\sequence.exe'', Symbols loaded. ''sequence.exe'': Loaded ''C:\WINNT\system32\NTDLL.DLL'', Cannot find or open a required DBG file. ''sequence.exe'': Loaded ''C:\WINNT\system32\KERNEL32.DLL'', Cannot find or open a required DBG file. The program ''[1368] sequence.exe: Native'' has exited with code 0 (0x0).
What is that? I haven''t changed anything. I''ve even tried it on different computers. I don''t understand what''s going on. It''s only been recently that this kind of stuff has been happening.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Try giving Seqence a virtual destructor


but then I would have to define the destructor for the SortedSeq class and the RevSortedSeq class, but I want them all to use the same destructor.

Share this post


Link to post
Share on other sites
quote:
Original post by capn_midnight
still, that doesn't explain WHAT is happening. I think you might be right, that it's related to the destructor, but WHAT is happening that I'm having problems?


The 'use a VIRTUAL destructor for the base seq class' seems to be correct.
It is recommended that all base classes have a virtual destructor and derivitive classes do have their own destructors regardless of whether you explicitly define them. The virtual in this case might help VC at compile time in ensuring that the proper clean up code is performed.

[edited by - citizen3019 on October 22, 2003 3:44:45 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by capn_midnight
Does the virtual destructor act as a normal destructor in the case of Sequence? Because all three classes are being used, not just the derived class.


virtual ~Sequence(void);
Is called for all derivitive classes if you define it, which I''m assuming you already have.
Infact, it is invisibly called by your undefined child class destructors.
Just like the size of your class is always a DWORD larger than the variables you add because of the this pointer and just like all __thiscall functions invisibly pass this pointer with every function call, alot is going on behind the scenes that make C++ a much more high level language(and all the ''overhead'' that it includes ) than straight C.

Share this post


Link to post
Share on other sites
The destructor (either the one you define, or the one the compiler generates) for base classes are always called when you destroy a derived object. The point of a virtual destructor is to ensure correct behavior when you have polymorphic objects e.g. when you invoke the destructor on a base class and really want to invoke the destructor on the derived class.

The classic example:

class Base {
public:
virtual ~Base() { ... }
};

class Derived : public Base {
public:
~Derived() { ... }
};

void main()
{
Base* pB = new Base;
Derived* pD = new Derived;
Base* pDB = new Derived;

// Invokes the destructor for Base
delete pB;

// Invokes the destructor for Derived
delete pD;

// Only invokes the Derived destructor if Base''s destructor
// is virtual.
delete pDB;
}

Also, the class size is only a DWORD (vtable pointer size) larger when your class declares one or more of its member functions to be virtual.


Matt

Share this post


Link to post
Share on other sites
Ok I don''t quite understand why capn_midnight would have to include a virtaul destructor in his base class.
His derived classes add no new data members.
They only add to or change the methods from the base class.

So I don''t see why the program would give him a error.

You should only have virtual added to a base if you are going to be added new data members or when using base pointers as the method to access class public methods. Now if he is gong to release this class to the outside world or that he might come up with derived classes that have new data members then by all means he should create virtaul destructor and any public methods that could be redefined.

Here his is doing nethier. Though I could be wrong.

must be a quik in M$ complier

Lord Bart

Share this post


Link to post
Share on other sites
I found out what I was doing "wrong". Because of my abhoration of taking my hands off the keyboard to grab the mouse to click the exclamation point in VC++6 to rebuild the program, I hit F5 instead. EVERY time I hit F5, I get this error. EVERY time I click the exclamation point, I get no error.

As for WHY this is wrong, I have no clue. Any idea what''s the difference?

Share this post


Link to post
Share on other sites