CPP-模板范围错误
cpp - Template scope Error?
我正在尝试使用模板在数组中实现基本堆栈。基于用户输入,形成了首选的堆栈类型。但是,在编译时,在检查IF条件下检查stack_type之后,它会出现"在此范围中声明s"的错误。如果评论条件检查,则不会显示任何错误。有人会介意解释为什么会遇到此错误吗?
#include<iostream>
using namespace std;
template < class Typ, int MaxStack >
class Stack {
Typ items[MaxStack];
int EmptyStack;
int top;
public:
Stack();
~Stack();
void push(Typ);
Typ pop();
int empty();
int full();
};
template < class Typ, int MaxStack >
Stack< Typ, MaxStack >::Stack() {
EmptyStack = -1;
top = EmptyStack;
}
template < class Typ, int MaxStack >
Stack< Typ, MaxStack >::~Stack() {
delete []items;
}
template < class Typ, int MaxStack >
void Stack< Typ, MaxStack >::push(Typ c) {
items[ ++top ] = c;
}
template < class Typ, int MaxStack >
Typ Stack< Typ, MaxStack >::pop() {
return items[ top-- ];
}
template< class Typ, int MaxStack >
int Stack< Typ, MaxStack >::full() {
return top + 1 == MaxStack;
}
template< class Typ, int MaxStack >
int Stack< Typ, MaxStack >::empty() {
return top == EmptyStack;
}
int main(void) {
int stack_type;
char ch;
cout << "Enter stack type: nt1.tcharater stacknt2.tInteger stacknt3.tFloat stackn";
cin >> stack_type;
if(stack_type == 1)
Stack<char, 10> s; // 10 chars
/* if(stack_type == 2)
Stack<int, 10> s; // 10 integers
if(stack_type == 3)
Stack<float, 10> s; // 10 double*/
while ((ch = cin.get()) != 'n')
if (!s.full())
s.push(ch);
while (!s.empty())
cout << s.pop();
cout << endl;
return 0;
}
if(stack_type == 1)
Stack<char, 10> s; // 10 chars
等效于:
if(stack_type == 1)
{
Stack<char, 10> s; // 10 chars
}
s
当然不是以下行中的范围。
我无法提出解决方案,因为您没有解释您希望在程序中完成的工作。
相关文章:
- 错误:未在此范围内声明'reverse'
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- 错误:"imread"未在此范围内声明
- 我在范围内未声明的错误类有问题
- 未在此范围内声明错误 'xy'
- 在C++中使用变量而不是"#define"来指定数组大小是不是一种糟糕的做法?(C错误:在文件范围内
- 运行时错误:矢量下标超出范围:正在检查空集
- 错误:'[' 之前预期的非限定 id 和错误:'users'未在此范围内声明
- 在此范围内未声明错误"Clreol"
- "myClock"未在此范围错误中声明
- 包含来自另一个文件的函数会导致范围错误(openFoam)
- 构造中错误:未在此范围中声明"data"
- 使用基于范围的指针循环和向量时出现C++错误
- DEV C++ 第 69 行 [错误] ISO "for" 范围更改了"i"的名称查找 [-允许]
- C++ 中的类之间的数据重新循环 - 错误:'<class name>'未在此范围内声明
- 使用to_string、reverse、stoi组合的C++反转编号给出运行时错误实例超出范围
- 如何避免指针超出范围(多态性)的C++分段错误
- 代码作为 C 文件工作,但不作为C++文件,错误:'__builtin_types_compatible_p'未在此范围内声明
- 如何修复范围内的字符串声明错误
- 如何修复错误:"vreinterpretq_u32_f64"未在此范围内声明 - 在Android上使用Eigen构建