views:

94

answers:

1

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.

+4  A: 

Here's a general hint that will make your life a million times easier.

Compile this program with the "-g" and "-Wall" flags:

gcc -g -Wall foo.cpp

The "-g" adds debugging information. The "-Wall" spits out additional warnings when compiling. Then use the debugger:

gdb ./a.out

Hit run to start your program. Use bt to dump your call stack once your code crashes. You can then see exactly where the crash is happening in your code.

While you're at it, google "gdb tutorial". Spending an hour or two learning how to use gdb properly will pay itself back, with interest. I promise you.

nsanders
Wow was I off. And also, **holy crap**. Best thing ever. `gdb` is my new favorite "toy". Much appreciated!
mattbasta