0

I have Foo and Bar as follows:

Foo.h

#include <list>

using namespace std;

class Foo
{
private:
    template <typename BarT>
    list<BarT*> barT_lst;

public:
    template <typename BarT>
    Foo(void);

    ~Foo(void);
};

Foo.cpp

#include "Foo.h"

template <typename BarT>
Foo::Foo(void)
{
}

Foo::~Foo(void)
{
}

Bar.h

class Bar
{
public:
    Bar(void);
    ~Bar(void);
};

Bar::Bar(void)
{
}

Bar::~Bar(void)
{
}

And main()

#include "Foo.h"
#include "Bar.h"

int _tmain(int argc, _TCHAR* argv[])
{
    Foo<Bar> foo = Foo<Bar>();
    return 0;
}

I use VC++2008. Every time when I build project, it shows errors:

1>------ Build started: Project: Test, Configuration: Debug Win32 ------
1>Compiling...
1>Test.cpp
1>c:\users\duong2179\documents\visual studio 2008\projects\test\test\foo.h(12) : fatal error C1001: An internal error has occurred in the compiler.
1>(compiler file 'msc1.cpp', line 1411)
1> To work around this problem, try simplifying or changing the program near the locations listed above.
1>Please choose the Technical Support command on the Visual C++ 
1> Help menu, or open the Technical Support help file for more information
1>Build log was saved at "file://c:\Users\duong2179\Documents\Visual Studio 2008\Projects\Test\Test\Debug\BuildLog.htm"
1>Test - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Build error

This looks very strange. Please help!

duong_dajgja
  • 4,196
  • 1
  • 38
  • 65
  • 1
    Try upgrading to a more recent version of compiler – M.M Mar 11 '16 at 05:22
  • BTW `template list barT_lst;` is invalid code - there is no such thing as template member variables – M.M Mar 11 '16 at 05:22
  • @M.M How can I declare list of BarT for such a thing? – duong_dajgja Mar 11 '16 at 05:23
  • You can't - how would you specify the parameter? Maybe you mean to have `BarT` as a template parameter for `Foo`. (`Foo` is also illegal because `Foo` is not a template) – M.M Mar 11 '16 at 05:24
  • @M.M Okay. I intend to make something to be able to inititate `Foo foo = Foo();` as shown in main. Could you please show me how I can do it? – duong_dajgja Mar 11 '16 at 05:27
  • 1
    Consider switching to a recent [GCC](http://gcc.gnu.org/) or [Clang/LLVM](http://clang.llvm.org/); both are free software, and support recent C++ standards (e.g. C++14 at least) – Basile Starynkevitch Mar 11 '16 at 05:49

2 Answers2

1

Getting an access violation from the compiler should not really happen, it would be good to upgrade compiler.

However your code is also illegal. If you intend to write Foo<Bar> foo; then Foo needs to be a template, for example:

template<typename BarT>
class Foo
{
    std::list<BarT *> barT_lst;
public:
    Foo();
    ~Foo();
};

template<typename BarT>
Foo<BarT>::Foo() 
{
    // constructor code here
}

Note: this class probably violates Rule of Three , you should not have a container of raw pointers unless those pointers do not own what they are pointing to. (In which case you don't need a destructor).

M.M
  • 138,810
  • 21
  • 208
  • 365
  • If so, it also requires to declare destructor like `Foo::~Foo()` right? – duong_dajgja Mar 11 '16 at 05:38
  • @duong_dajgja to *define* destructor out-of-line, you need to do that, yes. – M.M Mar 11 '16 at 05:42
  • Also note that the out-of-line function bodies cannot just sit in a .cpp file, they must be visible (or an explicit instantiation visible) to all code which calls them. [See here](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – M.M Mar 11 '16 at 05:47
0

Try to put the implementation of Foo class in same Foo.h file, i.e after class declaration.

Khurram Shehzad
  • 261
  • 3
  • 12