Visual Studio C++ 在使用模板时向控制台输出奇怪的字符
Visual Studio C++ outputting strange character to console when using template
当我尝试使用 VS 2013 将某些字符输出到控制台时,我 运行 遇到了问题。
这是角色的样子(在 2 之后)。我已经用“+-/*”符号进行了测试,每个符号都会出现这种情况。
基本上,我的程序使用堆栈 class 作为模板来保存字符,然后再提取这些字符。这是我的堆栈 class.
#include <assert.h>
template <class Item>
class stack
{
public:
//TYPEDEFS AND MEMBER CONSTANT
typedef int size_type;
typedef Item value_type;
static const size_type CAPACITY = 30;
//Constructor
stack() { used = 0; } //Postcondition: The stack has been initialized as an empty stack.
//Modification member functions
void push(const Item& entry); //Precondition: size() < CAPACITY. Postcondition: A new copy of entry has been pushed onto the stack.
void pop(); //Precondition: size() > 0. Postcondition:The top item of the stack has been removed.
//Constant member functions
bool empty() const { return (used == 0); } //Postcondition: The return value is true if the stack is empty, and false otherwise.
size_type size() const { return used; } //Postcondition: The return value is the total number of items in the stack.
Item top() const; //Precondition: size() > 0. Postcondition: The return value is the top item of the stack, but the stack is unchanged. This differs slightly from the STL stack (where the top function returns a reference to the item on top of the stack).
private:
Item data[CAPACITY]; //Partially filled array.
size_type used;
};
template <class Item>
void stack<Item>::push(const Item& entry) {
assert(size() < CAPACITY);
data[used] = entry;
used++;
}
template <class Item>
void stack<Item>::pop() {
assert(size() > 0);
used--;
}
template <class Item>
Item stack<Item>::top() const {
assert(size() > 0);
return data[used];
}
然后我的驱动程序使用 myStack.push(pString[pos]);
来保存一个字符,它稍后用 cout << myStack.top();
输出
有谁知道如何让它显示正确的字符吗?谢谢。
这个:
template <class Item>
Item stack<Item>::top() const {
assert(size() > 0);
return data[used];
}
将 return 结束,因为 used
== size()
。你想要:
return data[used - 1];
当我尝试使用 VS 2013 将某些字符输出到控制台时,我 运行 遇到了问题。
这是角色的样子(在 2 之后)
基本上,我的程序使用堆栈 class 作为模板来保存字符,然后再提取这些字符。这是我的堆栈 class.
#include <assert.h>
template <class Item>
class stack
{
public:
//TYPEDEFS AND MEMBER CONSTANT
typedef int size_type;
typedef Item value_type;
static const size_type CAPACITY = 30;
//Constructor
stack() { used = 0; } //Postcondition: The stack has been initialized as an empty stack.
//Modification member functions
void push(const Item& entry); //Precondition: size() < CAPACITY. Postcondition: A new copy of entry has been pushed onto the stack.
void pop(); //Precondition: size() > 0. Postcondition:The top item of the stack has been removed.
//Constant member functions
bool empty() const { return (used == 0); } //Postcondition: The return value is true if the stack is empty, and false otherwise.
size_type size() const { return used; } //Postcondition: The return value is the total number of items in the stack.
Item top() const; //Precondition: size() > 0. Postcondition: The return value is the top item of the stack, but the stack is unchanged. This differs slightly from the STL stack (where the top function returns a reference to the item on top of the stack).
private:
Item data[CAPACITY]; //Partially filled array.
size_type used;
};
template <class Item>
void stack<Item>::push(const Item& entry) {
assert(size() < CAPACITY);
data[used] = entry;
used++;
}
template <class Item>
void stack<Item>::pop() {
assert(size() > 0);
used--;
}
template <class Item>
Item stack<Item>::top() const {
assert(size() > 0);
return data[used];
}
然后我的驱动程序使用 myStack.push(pString[pos]);
来保存一个字符,它稍后用 cout << myStack.top();
有谁知道如何让它显示正确的字符吗?谢谢。
这个:
template <class Item>
Item stack<Item>::top() const {
assert(size() > 0);
return data[used];
}
将 return 结束,因为 used
== size()
。你想要:
return data[used - 1];