2011-02-09 121 views
0

我想实现一个Base类,它具有我在编译时已知的大小的属性。所以我的想法是使用这个基类的模板。以下代码在VC++ 9.0下编译并运行良好。在.h文件C++模板问题

template<int N> class BaseClass 
{ 
int* idx; 
int* incr; 
int* limit; 


public: 
BaseClass(void); 
~BaseClass(void); 

void LoopMethod(void); 

}

类定义;

#include<cstdlib> 
#include "BaseClass.h" 

using namespace std; 


int main() 
{ 
BaseClass<2> baseObj; 

baseObj.LoopMethod(); 


system("PAUSE"); 
return 0; 

} 

现在我要嵌套的for循环来源:

在.cpp文件

#include "BaseClass.h" 
#include<iostream> 

using namespace std; 

// instantiation 
template class BaseClass<2>; 


template<int N> BaseClass<N>::BaseClass(void) 
{ 
idx = new int [N]; 
incr= new int [N]; 
limit = new int[N]; 

for(int m = 0; m < N; m++) 
{ 
    idx[m] = 0; 
    incr[m] = 1; 
    limit[m] = 2; 
} 

} 

template<int N> BaseClass<N>::~BaseClass(void) 
{ 
} 


template<int N> void BaseClass<N>::LoopMethod() 
{ 
for(idx[N-1]; idx[N-1] < limit[N-1]; idx[N-1] += incr[N-1]) 
{ 
    cout << "LoopMethod Nr " << N-1 << " is called." << endl; 
} 

}

主要功能的实现类的方法实现LoopMethod乘以类属性的大小。即编译器应该生成代码,我会用手写成

template<int N> void BaseClass<N>::LoopMethod() 
{ 
for(idx[0]; idx[0] < limit[0]; idx[0] += incr[0]) 
{ 
    for(idx[1]; idx[1] < limit[1]; idx[1] += incr[1]) 
    { 
     cout << "LoopMethod Nr " << 1 << " is called." << endl; 
    } 

    cout << "LoopMethod Nr " << 0 << " is called." << endl; 

} 
} 

反正,我可以提示编译器要做到这一点,如果我不声明BaseClass的是一个模板类。然后该代码,这看起来像:

#include<cstdlib> 
#include "BaseClass.h" 

using namespace std; 


int main() 
{ 
BaseClass baseObj; 

baseObj.LoopMethod<1>(); 


system("PAUSE"); 
return 0; 
} 

但解决:

class BaseClass 
{ 


int* idx; 
int* incr; 
int* limit; 


public: 
BaseClass(void); 
~BaseClass(void); 

template<int M> void LoopMethod(void); 


}; 

在.cpp文件类的方法实现

#include "BaseClass.h" 
#include<iostream> 

using namespace std; 

template void BaseClass::LoopMethod<1>();   

BaseClass::BaseClass(void) 
{ 
idx = new int [2]; 
incr= new int [2]; 
limit = new int[2]; 

for(int m = 0; m < 2; m++) 
{ 
    idx[m] = 0; 
    incr[m] = 1; 
    limit[m] = 2; 
} 

} 

BaseClass::~BaseClass(void) 
{ 
} 

template<int M> void BaseClass::LoopMethod() 
{ 
for(idx[M]; idx[M] < limit[M]; idx[M] += incr[M]) 
{ 
    cout << "LoopMethod Nr " << M-1 << " is called." << endl; 
    LoopMethod<M-1>(); 

} 
} 

template<> void BaseClass::LoopMethod<0>(void) 
{ 
idx[0] = 0; 

for(idx[0]; idx[0] < limit[0]; idx[0] += incr[0]) 
{ 
    // do something 
    cout << "Now the inner loop is executed" << endl; 

} 

} 

主要功能的实现我正在寻找的是拥有一个带有模板方法“LoopMethod”的模板类,由于它自己的模板参数告诉编译器多少次嵌套for-Loop。我尝试了各种可能性,但没有成功。有人有建议,甚至不知道这个模板问题的解决方案吗?

在此先感谢您的帮助,

Markus。

+0

你为什么要这么做这个模板? –

+6

你的问题是**方式太长了**!这里很少有人能够无畏地完整地阅读它! – Benoit

+0

你的类需要一个拷贝构造函数和一个拷贝赋值操作符。看到这个相关的[FAQ](http://stackoverflow.com/questions/4172722/)。 – fredoverflow

回答

5

有很多的问题与您的模板:

  • 到底是什么的,整个事情的目的是什么?
  • 你为什么用新的初始化指针?你知道编译时的大小,为什么不把它们放到数组中呢?
  • 你是不是删除,除非它被用于您实例氮很少值以后阵列
  • 实施大概应该是在头文件中的一个,如果新的失败,你都分配
  • 异常安全存储
  • 最好使用存在该做这样的事情,如升压::数组
  • 重构出来的各个部分的类。
0

但是我正在寻找解决的办法是 有一个模板类有 模板方法“LoopMethod”由于其 自己的模板参数,它告诉编译器 多少次窝 换环

这是你在问:

template<int N> 
struct X 
{ 
    template<int M> 
    void loop(); 
}; 

template<int N> 
template<int M> 
void X<N>::loop<M>() 
{ 
} 
+0

看起来他希望使用模板大小来循环,因此他不需要两个不同的模板参数值。他使用矢量,他将能够知道他正在循环的收集的大小。 – CashCow

+0

是的,去图... –