我有两个模板声明如下:
template<typename T>
class A{
public:
T& getData();
//rest of public
private:
shared_ptr<A<T>>& X;
shared_ptr<A<T>>& Y;
shared_ptr<A<T>>& Z;
T& data;
};
//template constructor definition
template <typename T> A<T>::A(T& rData,
shared_ptr<A<T>>& rX,
shared_ptr<A<T>>& rY,
shared_ptr<A<T>>& rZ): X(rX),
Y(rY),
Z(rZ),
data(rData){
//rest of definition}
//definition for getData()
template <typename T> T& A<T>::getData(){return data;}
template<typename T1, typename T2>
class B{
public:
using typAContainter = class std::list<shared_ptr<A<T1>>>;
B(int, T2&);
//rest of public
private:
typeAContainer C;
//rest of private
};
在B的构造函数定义中,我使用两个不同的(但在逻辑上相似)调试代码段(注意,我不得不求助于调试代码段1,因为调试代码段2没有给出预期的输出,这是我面临的问题):
template<typename T1, typename T2> B<T1, T2>::B(int I, T2& rData){
auto iter = C.begin();
auto count = 0;
//a workaround to create three
//dummy shared_ptr instances to
//type A<T>
vector<shared_ptr<A<T1>>>dummys(3);
for(; count < I ; ++count, ++iter)
{
*iter = make_shared<A<T1>>(rData[count],
dummys[0],
dummys[1],
dummys[2]
);
//Debug code segment 1
#ifdef DEBUG
std::cout << (*iter).get()->getData() << std::endl;
#endif
}
//Debug code segment 2
//doesn't give any output
for(auto Aref : C)
{
std::cout << Aref.get()->getData() << std::endl;
}
}
由于调试代码段2不会产生任何编译器错误,我认为语法是正确的。任何人都可以指出由于我没有得到预期输出的逻辑错误吗?