Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++基础中的类模板_C++_Templates_Undeclared Identifier - Fatal编程技术网

C++基础中的类模板

C++基础中的类模板,c++,templates,undeclared-identifier,C++,Templates,Undeclared Identifier,这不应该是t而不是int吗?这样,不管模板专门化如何,您只读取int 这不应该是t而不是int吗?这样,无论模板专门化如何,您都只能读取int。请修复这个可怕的缩进…您自己尝试过修复它吗?你看到错误消息了吗?请修复那个可怕的缩进…你自己尝试过修复它吗?你看到错误信息了吗?好的,我更正了int a部分。但是,假设我想根据用户输入创建一个模板,那么我该如何实现它?@slash:对于模板,T需要在编译时知道,因此您可以在某个表中创建所有可能的类型,并在以后使用与用户请求的类型对应的类型。然后您还需要某

这不应该是t而不是int吗?这样,不管模板专门化如何,您只读取int


这不应该是t而不是int吗?这样,无论模板专门化如何,您都只能读取int。

请修复这个可怕的缩进…您自己尝试过修复它吗?你看到错误消息了吗?请修复那个可怕的缩进…你自己尝试过修复它吗?你看到错误信息了吗?好的,我更正了int a部分。但是,假设我想根据用户输入创建一个模板,那么我该如何实现它?@slash:对于模板,T需要在编译时知道,因此您可以在某个表中创建所有可能的类型,并在以后使用与用户请求的类型对应的类型。然后您还需要某种方法从用户那里获取类型。@按一下,您的意思是说我创建了。。。堆栈a、堆栈a等?我不明白exactly@shash学习C++基础知识怎么样?模板是为高级程序员准备的。好吧,我更正了int a部分。但是,假设我想根据用户输入创建一个模板,那么我该如何实现它?@slash:对于模板,T需要在编译时知道,因此您可以在某个表中创建所有可能的类型,并在以后使用与用户请求的类型对应的类型。然后您还需要某种方法从用户那里获取类型。@按一下,您的意思是说我创建了。。。堆栈a、堆栈a等?我不明白exactly@shash学习C++基础知识怎么样?模板是为高级程序员准备的。
template<class T>
class Stack 
{
public:
    Stack()
    {
        cout<<"Enter size\n";
        cin>>size;
        //stackPtr=new int[size];
        stackPtr= new T[size];
        top =0;
    }

    ~Stack()
    {
        delete[] stackPtr;
    }

    void display()
    {
        cout<<"*****************************\n";
        for(int i=0;i<top;i++)
            cout<<stackPtr[i]<<endl;
        cout<<"*****************************\n";
    }

    bool Push()
    {
        int a;
        cout<<"Enter element\n";
        cin>>a;
        if(isFull()==false)
        {
            stackPtr[top++]=a;
            return true;
        }
        else
            return false;
    }

    bool Pop()
    {
        if(isEmpty()==false)
        {   
            top--;
            return true;
        }
        else 
            return false;
    }

    bool isEmpty()
    {
        if(top==0)
            return true;
        else
            return false;
    }

    bool isFull()
    {
        if(top==size)
            return true;
        else
            return false;
    }

private:
    //int *stackPtr;
    T *stackPtr;
    int size;
    int top;
    //location of the last element added, -1 means empty stack
};



void main()
{

    int typeArray;
    cout<<"What type of array do you want\n1.Integer\n2.Character\n";
    cin>>typeArray;
    //If instead of these if statements, i simply
        //write Stack<int> Stacks, everything works fine
        //So i think theres something wrong with using char or float or ANY other 
        //datatype?

    if(typeArray==1)
        Stack <int>Stacks;
    else 
        Stack <float>Stacks;

    cout<<"1.PUsh\n2.Pop\n3.Display all\n4.exit\n";
    int ch=1;
    cin>>ch;
    while(ch!=-1)
    {

        switch(ch)
        {
        case 1: if(Stacks.Push())
            {
                cout<<"Stack Full\nPop to enter other        values\n";

            }
            break;
        case 2: if(Stacks.Pop()==false)
                    cout<<"Stack EMpty\n";
            break;
        case 3: Stacks.display();
            break;
        case 4: exit(0);
        default:cout<<"Reenter you option\n";
            break;
        }
        cout<<"1.PUsh\n2.Pop\n3.Display all\n4.exit\n";
        cin>>ch;
    }
}
test.cpp(104): error C2065: 'Stacks' : undeclared identifier
test.cpp(104): error C2228: left of '.Push' must have class/struct/union type
                            is ''unknown-type''
test.cpp(110): error C2065: 'Stacks' : undeclared identifier 
test.cpp(110): error C2228: left of '.Pop' must have class/struct/union type
                            is ''unknown-type''
test.cpp(113): error C2065: 'Stacks' : undeclared identifier
test.cpp(113): error C2228: left of '.display'  must have class/struct/union type
                            is ''unknown-type''
if(typeArray==1)
    Stack <int>Stacks;
else 
    Stack <float>Stacks;
bool Push()
{
    int a;
    cout<<"Enter element\n";
    cin>>a;