Template type is a template

Suppose we have a template

1
2
3
4
5
6
7
8
9
10
11
12
template <class X>
class NestedTemplate { ... };

template <class A, class T>
class MainTemplate { ... };

class SampleClass {
private:
  MainTemplate<NestedTemplate<char>,float> x;
public:
  // methods
};


code above compiles but when running I get a "memory could not be written" error

I've tried it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <class X>
class NestedTemplate { ... };

template <class A, class T>
class MainTemplate { ... };

class FakeClass { public: NestedTemplate<char> c; };

class SampleClass {
private:
  MainTemplate<FakeClass,float> x;
public:
  // methods
};


and it didn't work...

I can't figure out what I should change. I'm novice using templates. Simple usage described in most manuals went ok, but I got stucked here. I can solve this without templates but it'll be dirty so I would like to learn how to do it correctly.

Thanks in advance for any help.
There's nothing wrong with the code samples themselves, so the error must be in the parts you omitted.
I've just got on this http://stackoverflow.com/questions/7282971/syntax-of-c-template-template-parameters

Maybe I should make it as follows:
1
2
template <template <class> class A, class T>
class MainTemplate { ... };


But then the question is: What can I do if I want a template to accept both Type and Template parameters ??
There's no difference between template types and "normal" types. Which brings us back to my previous reply.
don't know about you guys, but this code worked just fine for me..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;

template <class Class1DataType>
class Class1
{

  public:
  Class1DataType Class1Data;
  void PrintData()
  {
    cout << Class1Data<<endl;
  }
};

template <class T1, class T2>
class Class2
{
    public:

    T1 Class1Instance;    // will be using class1 here
    T2 B;                   //Some variable

    void PrintData()
    {
        Class1Instance.PrintData();
        cout << B<<endl;
    }


};
class SampleClass
{
    public:
    Class2<Class1<char>,float> Something;
    SampleClass()
    {
        Something.Class1Instance.Class1Data = 'A';
        Something.B = 3.212;
        Something.PrintData();
    }
};
int main()
{
    SampleClass sample;
    return 0;
}
Athar, thanks you were right in "There's no difference between template types and "normal" types"

But this is what I've found out by trying different methods of doing what I need...

The following works:
1
2
3
4
5
6
7
template<class A, class B> class FirstTemplate {...};

class SimpleClass {
private:
  FirstTemplate<MyType1,MyType2> x;
  // MyType1 and MyType2 are normal types, not template types, but i'm pretty sure it'll work for templates too
}


but if we add another class like this:
1
2
3
4
5
6
// code above +

class AnotherClass {
public:
  SimpleClass a,b;
};


then I get the "refferenced memory could not be written" error.

Does anyone has a clue why ?
Last edited on
There's something wrong elsewhere in your code. Probably in one of the constructors.
Exact error is
The instruction at "0x7c9101b3" referenced memory at "0x0044b5b6". The memory could not be "written".
Doesn't help. Code.
Or run the debugger to see which line causes it.
Last edited on
Topic archived. No new replies allowed.