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

Started by
10 comments, last by capn_midnight 20 years, 5 months ago

#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&lt;&lt;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&lt;last;i++){
		for(j=i+1;j&lt;=last;j++){
			if(s[j]&lt;s){
				temp=s[j];
				s[j]=s;
				s=temp;
			}
		}
	}
}
bool SortedSeq::addSS(const string& entry){
	int i;
	for(i=0;i&lt;=last;i++){
		if(entry&gt;s)
			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&lt;last;i++){
		for(j=i+1;j&lt;=last;j++){
			if(s[j]&gt;s){
				temp=s[j];
				s[j]=s;
				s=temp;
			}
		}
	}
}
bool RevSortedSeq::addRSS(const string& entry){
	int i;
	for(i=0;i&lt;=last;i++){
		if(entry&lt;s)
			break;
	}
	return addS(i, entry);
};
RevSortedSeq::RevSortedSeq(const char* fname):Sequence(fname){sort();}


int main(void){
	Sequence s1("</font>input.txt<font color=darkred>");
	SortedSeq s2("</font>input.txt<font color=darkred>");
	RevSortedSeq s3("</font>input.txt<font color=darkred>");
	s1.output();
	s2.output();
	s3.output();
	s1.addS(3,"</font>Ursula<font color=darkred>");
	s2.addSS("</font>Ursula<font color=darkred>");
	s3.addRSS("</font>Ursula");
	s1.output();
	s2.output();
	s3.output();
	<font color=blue>return</font> 0;
}
<font color=gray>/*****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
**********/</font>
</pre><!–ENDSCRIPT–>
Over the past two weeks ANY program that I write in VS (either 6.0 or .NET) gives me an error &#79;N PROGRAM EXIT.
<BLOCKQUOTE><SPAN CLASS=smallfont>quote:<hr HEIGHT=1 noshade>
''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).
<hr height=1 noshade></SPAN></BLOCKQUOTE>
What is that? I haven''t changed anything. I''ve even tried it &#111;n different computers. I don''t understand what''s going &#111;n. It''s &#111;nly been recently that this kind of stuff has been happening.   

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

Advertisement
Ahh, this is what happens when you hang around the lounge too much...
Try giving Seqence a virtual destructor
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.

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

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?

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

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]
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.

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

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

This topic is closed to new replies.

Advertisement