QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1078|回复: 4

c++ program for stack

[复制链接]
发表于 2003-11-5 23:10:13 | 显示全部楼层 |阅读模式
the first file:
class duizhan
{
        public:
    int stack[10];
        int top;
        int MaxSize;
       
        duizhan();
        ~duizhan();
        void push(int);
        int pop();
};

the second file:
#include <iostream>
#include &lt;stdio&gt;
#include "duizhan.h"

using namespace std;

duizhan::duizhan()
{
        int MaxSize = 10;
        int top = -1;
}

void duizhan::push(int valve)
{
        int i;
       
        if (top&gt;=MaxSize-1)
                cout &lt;&lt; "The stack is full!!!" &lt;&lt; endl;
        else
        {
                printf("\nThe stack content before(top -&gt; bottom):");
                for (i=top;i&gt;=0;i--)
                        printf("[%d]",stack);
               
                top++;
                stack[top] = value;
               
                printf("\nThe stack content after push(top -&gt; bottom):");
                for (i=top;i&gt;=0;i--)
                        printf("[%d]",stack);
                printf("\n");
        }
}

int duizhan::pop()
{
        int temp;
        int i;
       
        if (top&lt;0){
                printf("\nThe stack is empty!!!\n");
                return -1;
        }
        printf("\nThe stack content before(top -&gt; bottom):");
        for (i=top;i&gt;=0;i--)
                printf("[%d]",stack);
       
        temp = stack[top];
        top--;
        printf("\nThe pop value is [%d]",temp);
        printf("\nThe stack content after pop(top -&gt; bottom):");
        for (i=top;i&gt;=0;i--)
                printf("[%d]",stack);
        printf("\n");
        return temp;
}

the third file:
#include "duizhan.h"
#include <iostream>

using namespace std;

int main()
{
        duizhan dzh;
        int select;
        int stack[5];
        int i,value;
       
        printf("\n(1)Input a stack data");
        printf("\n(2)Output a stack data");
        printf("=n(3)Exit");
        printf("\nPlease select one =&gt;");
        scanf("%d",&amp;select);
        do{
                switch(select){
                        case 1:printf("\nPlease input the data=&gt;");
                                scanf("%d",&amp;value);
                            dzh.push(value);
                            break;
                        case 2:value=dzh.pop();
                                break;
                }
                printf("\n(1)Input a stack data");
                printf("\n(2)Output a stack data");
                printf("\n(3)Exit");
                printf("\nPlease select one=&gt;");
                scanf("%d",&amp;select);
                printf("\n");
        }while(select != 3);
}

the error view:
/tmp/cc72ZmlF.o(.text+0x19): In function `main':
: undefined reference to `duizhan::duizhan[in-charge]()'
/tmp/cc72ZmlF.o(.text+0xb7): In function `main':
: undefined reference to `duizhan::push(int)'
/tmp/cc72ZmlF.o(.text+0xc: In function `main':
: undefined reference to `duizhan::pop()'
/tmp/cc72ZmlF.o(.text+0x150): In function `main':
: undefined reference to `duizhan::~duizhan [in-charge]()'
/tmp/cc72ZmlF.o(.text+0x16d): In function `main':
: undefined reference to `duizhan::~duizhan [in-charge]()'
collect2: ld returned 1 exit status

how to do it now!!! :-(
发表于 2003-11-17 14:19:46 | 显示全部楼层
you need compile the second file.
such as
    gcc -c -o duizhan.o duizhan.o
then
   gcc -o main main.c duizhan.o
回复

使用道具 举报

发表于 2003-11-18 14:24:17 | 显示全部楼层
try:

g++ duizhan.h duizhan.cpp -o duizhan
回复

使用道具 举报

 楼主| 发表于 2003-11-18 23:56:29 | 显示全部楼层
谢谢!!!这个问题解决了!!!
回复

使用道具 举报

 楼主| 发表于 2003-11-18 23:57:54 | 显示全部楼层
但是,有人告诉我,*.o *.so 动态链接库的后缀名和*.o有什么区别!!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-9 10:30 , Processed in 0.058238 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表