注册 登录
编程论坛 C++教室

不知道哪里错了,求指导!O(∩_∩)O谢谢~

落木啦啦啦 发布于 2015-10-20 15:33, 427 次点击
创建一个栈,将10、12、14入栈,输出栈顶元素,然后退栈,代码如下
#include<iostream.h>
const int size=10;
class stack
{
    int stck[size];
    int tos;
public:
    stack(int tos)
    {tos=0;};
    void push(int ch);
    int pop();
}

void stack::push(int ch)
{
    if(tos==size)
        cout<<"stack is full\n"<<endl;
    stack[tos]=ch;
    tos++;
}

int stack::pop()
{
    if(tos==0)
        cout<<"stack is empty\n"<<endl;
    tos--;
    return stck[tos];
}

void main()
{
    stack s1;
    int i;
    s1.push(10);
    s1.push(12);
    s1.push(14);
    for(i=0;i<3;i++)
        cout<<"stack s1"<<s1.pop()<<"\n";
}

错误如下,,奈何看不懂。。。求指点!!
E:\123\P290 Q5.CPP(14) : error C2628: 'stack' followed by 'void' is illegal (did you forget a ';'?)
E:\123\P290 Q5.CPP(15) : error C2556: 'class stack __thiscall stack::push(int)' : overloaded function differs only by return type from 'void __thiscall stack::push(int)'
        E:\123\P290 Q5.CPP(10) : see declaration of 'push'
E:\123\P290 Q5.CPP(15) : error C2371: 'push' : redefinition; different basic types
        E:\123\P290 Q5.CPP(10) : see declaration of 'push'
E:\123\P290 Q5.CPP(18) : error C2143: syntax error : missing ';' before '['
E:\123\P290 Q5.CPP(18) : error C2143: syntax error : missing ';' before '['
E:\123\P290 Q5.CPP(32) : error C2512: 'stack' : no appropriate default constructor available
E:\123\P290 Q5.CPP(34) : error C2264: 'push' : error in function definition or declaration; function not called
E:\123\P290 Q5.CPP(35) : error C2264: 'push' : error in function definition or declaration; function not called
E:\123\P290 Q5.CPP(36) : error C2264: 'push' : error in function definition or declaration; function not called
Error executing cl.exe.
5 回复
#2
TonyDeng2015-10-20 15:38
class stack
 {
     int stck[size];
     int tos;
 public:
     stack(int tos)
     {tos=0;};
     void push(int ch);
     int pop();
 };

E:\123\P290 Q5.CPP(14) : error C2628: 'stack' followed by 'void' is illegal (did you forget a ';'?)
編譯信息已經提示得很清晰,問你是不是忘記了分號。
#3
落木啦啦啦2015-10-20 15:48
回复 2楼 TonyDeng
哦。。眼拙,,没看到。。
#4
TonyDeng2015-10-20 15:50
不是沒看到,而是你根本沒意識去看。黏貼都有的,會“沒看到”?
#5
落木啦啦啦2015-10-20 15:52
回复 4楼 TonyDeng
因为我是从下往上看的,下面的几个都看不懂上网搜也不知道什么意思,所以没有看完。
#6
落木啦啦啦2015-10-20 16:27
已经解决了,多谢二楼仁兄批评。
1