我建了个template<int n> class test类怎么在类方法里用n?
c++吧
全部回复
仅看楼主
level 1
wty002009 楼主
#include<iostream>
using namespace std;
template<int n>
class test{
double a[n];
public:
void show();
};
void test::show()
{
for(int i;i<n;i++)//这里的n应该用模板传过来的n怎么弄?
cout<<a[i];
}
2013年11月05日 02点11分 1
level 8
[黑线]吓尿,原来模板还可以当对象参数来使用.
2013年11月05日 03点11分 2
模板值型形参,有啥大惊小怪的,难道范磊的1000页没讲[滑稽]
2013年11月05日 03点11分
回复 gameloftyou :[汗]没看完
2013年11月05日 03点11分
毫无吓尿感,不过这种写法需要付出代价
2013年11月05日 03点11分
回复 白晓生锄禾 :什么意思?
2013年11月05日 03点11分
level 11
template<int n>
void test::show()
{
......
}
2013年11月05日 03点11分 3
不对啊 template<int n>void test<n>::show(){ for(int i;i<test<n>::n;i++) cout<<a[i];}
2013年11月05日 03点11分
level 11
#include<iostream>
using namespace std;
template <int n>
class test {
double a[n];
public:
void show();
};
template <int n>
void test<n>::show()
{
for(int i=0; i<n; i++) cout<<a[i]<<",";
}
int main()
{
test<5> t;
t.show();
return 0;
}
2013年11月05日 03点11分 4
template<int n>void test<n>::set(){ for(int i;i<n;i++) cin>>a[i]; cout<<"done";}这个不好使啊??
2013年11月05日 03点11分
回复 wty002009 :咋不好使[汗]
2013年11月05日 03点11分
回复 wty002009 :[喷]你好刁,循环里i都不初始化
2013年11月05日 03点11分
回复 gameloftyou :我又加了个上面的void set()给数组赋值然后不好使啊
2013年11月05日 03点11分
level 9
你可以考虑使用
test<1>
test<2>
test<3>
test<n>
...(可执行文件越来越大.....)
2013年11月05日 03点11分 5
1