我正在尝试使用 G++ 编译一些 C++ 代码。它似乎在其他编译器中工作正常,但无论出于何种原因,G++ 都不会产生工作输出。
披露:这是家庭作业的一部分,但我觉得这更多是编译器问题,因为它可以在其他编译器中工作。
这是造成严重破坏的片段:
set<int> t1, t2;
这很奇怪,因为下面的代码运行得很好:
set<int> *t1 = new set<int>();
set<int> *t2 = new set<int>();
当然,我必须使用->
代替.
,但这是预期的。第一个片段在运行时产生分段错误。第二个直观地实现了我的预期。
无论如何,在幕后,.cpp
for set
有这个:
#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
看起来像这样:
#include "bag.h"
template <class T>
class set : public bag<T>
{
public:
set( );
set(const set &b);
// ...
};
#include "set.cpp"
最后但并非最不重要的一点是,bag.cpp
and bag.h
文件看起来像这样:
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"
再说一遍,我觉得 G++ 只是讨厌我,但话又说回来,我可能做了一些愚蠢的事情。只要朝着正确的方向进行简单的推动就可以了。