I'm trying to use G++ to compile some C++ code. It seems to work fine in other compilers, but for whatever reason, G++ won't produce working output.
Disclosure: This is part of a homework assignment, but I feel like it's more of a compiler issue, since it works in other compilers.
Here's the snippet that's wreaking havoc:
set<int> t1, t2;
It's strange because the following code works just fine:
set<int> *t1 = new set<int>();
set<int> *t2 = new set<int>();
Granted, I have to use -> instead of ., but that's expected. The first snippet produces a segmentation fault at runtime. The second does intuitively what I'd expect it to.
Anyhow, behind the scenes, the .cpp for set has this:
#include <cstdlib>
#include <iostream>
using namespace std;
template <class T>
set<T>::set() : bag<T>() {}
template <class T>
set<T>::set(const set<T>& b) : bag<T>(b) {}
The .h looks like this:
#include "bag.h"
template <class T>
class set : public bag<T>
{
public:
set( );
set(const set &b);
// ...
};
#include "set.cpp"
And last but not least, the bag.cpp and bag.h files looks like this:
using namespace std;
template <class T>
bag<T>::bag() {
head = NULL;
}
template <class T>
bag<T>::bag(const bag<T>& b) {
// ...
}
and bag.h:
template <class T>
class bag
{
public:
bag( );
bag(const bag &b);
// ...
};
#include "bag.cpp"
Again, I feel like G++ just hates me, but then again I could be doing something dumb. Just a simple nudge in the right direction would be great.
using namespace stdcombined with naming your class the same thing as a standard library name (set) can only end in tears.setandbagdo? (When you create an object on the stack as in the first code snippet, the object is destroyed at the end of its enclosing scope block. When you create an object dynamically usingnewas in the second code snippet, the object is destroyed when youdeleteit. If you neverdeleteit, the destructor will never be called; I'd guess that something in the destructor is likely the cause of your problem</psychic-debugging>).discreet_set. No effect. As for the destructor, it iterates a linked list and deletes the value of each node. Nothing fancy, but I'll tinker and let you know. Thanks so far!discrete_set. :-) [Seriously, though, this demonstrates one really good reason not to use using directives at file scope. They are nothing but trouble. In the long run it is far easier just to qualify names that are in namespaces.]discrete_set, but that's not what my fingers wanted to type. It's a little late here ;D