Reputation: 11281
I'm trying to implement the following in C++:
template <class T>
class Inner
{
public:
Inner(T inData){data = inData;};
~Inner(void){};
T data;
};
template <class T>
class Outer
{
public:
Outer(Inner<T> in){inner = in;};
~Outer(void){};
Inner<T> inner;
};
int main(void)
{
Inner<int> in (10);
Outer<int> out (in);
std::cout << out.inner.data;
}
Compiled in MSVS 2010, I get the following error:
c:\...\main.cpp(15): error C2512: 'Inner<T>' : no appropriate default constructor available
with
[
T=int
]
c:\users\gaff\documents\visual studio 2010\projects\pgp(c++)\pgp(c++)\main.cpp(15) : while compiling class template member function 'Outer<T>::Outer(Inner<T>)'
with
[
T=int
]
c:\users\gaff\documents\visual studio 2010\projects\pgp(c++)\pgp(c++)\main.cpp(24) : see reference to class template instantiation 'Outer<T>' being compiled
with
[
T=int
]
Clearly I'm missing some basic understanding here but I have no clue how to fix this, or even if what I'm trying to achieve is possible.
For what it's worth, here is a Java implementation that compiles without any problems:
public class Inner<T> {
public T data;
public Inner(T inData) {
data = inData;
}
}
public class Outer<T> {
public Inner<T> inner;
public Outer(Inner<T> in) {
inner = in;
}
}
public class Main {
public static void main(String args[]){
Inner<Integer> in = new Inner<Integer>(10);
Outer<Integer> out = new Outer<Integer>(in);
System.out.println(out.inner.data);
}
}
Any suggestions?
Cheers
JimmidyJoo
Upvotes: 2
Views: 223
Reputation: 44093
The problem is that you don't have a default constructor for inner. When the compiler initializes the object it calls the default constructor of inner
before the assignment.
EDIT:
It's probably a good idea to properly implement a copy constructor and instead initialize inner like so:
Outer(Inner<T> in) : inner(in) { };
You could use the copy constructor like so:
Inner(const Inner<T> & in) : data(in.data){
}
Upvotes: 3
Reputation: 33202
You need to use an initializer list in the constructor:
Outer(Inner<T> in): inner(in) {}
otherwise the constructor has to default construct inner before it gets into the constructor. That's impossible since there is no default constructor.
Upvotes: 2