![](/img/trans.png)
[英]how do i send a method in a template class a print function from main c++
[英]C++ Template class to print string or int output based on the call from main function
我需要编写一个名为 Pattern 的模板 class,它根据来自主 function 的调用打印 int 行或字符串。 RowInt 和 RowString 在非模板 class 中定义。
如何编写 RowInt、RowStr class 和模板 class “模式”,使其能够识别需要生成哪一行,即带有字符串“*”的行或带有 int 1、2、3 的行......序列应该直到满足用户输入的数字才会生成。 即如果用户输入 4,那么它应该为 Pattern mypattern(number) 打印 1 2 3 4; 它应该为调用 Pattern mypattern(number); 打印 ****; 请建议。
int main()
{
int number = 0;
do
{
cout << "Please input the number of rows (max 10:)" ;
cin >> number;
}
while ((number < 1) || (number > 10))
Pattern<RowStr> mypattern(number); //shows string * ,number of string will be equal to the number entered by the user
Pattern<RowInt> mypattern(number); //generates rows with numbers 1,2,3,4....untill the number entered by the user
我正在使用 Cygwin 运行这个程序。
class PatternString
{
private:
typedef string strvalue;
strvalue sstr;
public:
PatternString(int num =0)
{
for (int i = 1, k = 0; i <= num; ++i, k = 0)
{
for (int space = 1; space <= num - i; ++space)
{
sstr += " ";
cout<<"from firstloop"<< sstr<<endl;
}
while (k != 2 * i - 1)
{
sstr += "X";
cout<<"from secondloop"<< sstr<<endl;
k++;
}
}
}
strvalue get_disc() const
{
return sstr;
}
};
给我一个 output 的,请。 建议我哪里出错了。
X
XXXX
X XXXXXXXX
X XXX XXXXXXXXXXXX
X XXX XXXXX XXXXXXXXXXXXXXXX
我需要一个 output 星形金字塔,在每次调用时生成一行并存储在字符串变量中,该字符串变量将依次返回并存储在链表节点中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.