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

各位大佬帮忙看一下实在找不出哪错 栈的存储与操作

七月与安生鉴 发布于 2021-12-11 15:24, 984 次点击

SeqStack.h
#ifndef SeqStack_H
#define SeqStack_H
const int StackSize = 10;
class SeqStack
{
public:
     SeqStack();
    ~SeqStack(){}
    void Push(int x);
    int Pop();
    int GetTop();
    int Empty();
private:
    int data[StackSize];
    int top;
};
#endif
SeqStack.cpp
#include"SeqStack.h"
//初始化顺序栈
SeqStack::SeqStack(){
    top = -1;
}
//数据元素入栈
void SeqStack:: Push(int x)
{
    top++;
    data[top] = x;
}
//数据元素出栈
int SeqStack:: Pop(){
    int x = data[top--];
    return x;
}
//读栈顶元素
int SeqStack::GetTop()
{
    if (top != -1)
        return data[top];
}
//判定栈空/满操作
int SeqStack::Empty()
{
    if (top == -1)return 1;
    else return 0;
}

main.cpp
#include"SeqStack.cpp"
#include<iostream>
using namespace std;
void main()
{
    SeqStack S;
    if (S.Empty())
        cout << "栈为空" << endl;
    else
        cout << "栈非空" << endl;
    cout << "对15和10执行入栈操作" << endl;
    S.Push(15);
    S.Push(10);
    cout << "栈顶元素为;" << endl;
    cout << S.GetTop() << endl;
    cout << "执行一次出栈操作" << endl;
    S.Pop();
    cout << "栈顶元素为:" << endl;
    cout << S.GetTop() << endl;
    system("pause");
}

这是报的错误
错误    6    error LNK1169: 找到一个或多个多重定义的符号    G:\新建文件夹\Project2\x64\Debug\栈的储存于操作.exe    1    1    栈的储存于操作
错误    1    error LNK2005: "public: __cdecl SeqStack::SeqStack(void)" (??0SeqStack@@QEAA@XZ) 已经在 main.obj 中定义    G:\新建文件夹\Project2\Project2\SeqStack.obj    栈的储存于操作
错误    5    error LNK2005: "public: int __cdecl SeqStack::Empty(void)" (?Empty@SeqStack@@QEAAHXZ) 已经在 main.obj 中定义    G:\新建文件夹\Project2\Project2\SeqStack.obj    栈的储存于操作
错误    4    error LNK2005: "public: int __cdecl SeqStack::GetTop(void)" (?GetTop@SeqStack@@QEAAHXZ) 已经在 main.obj 中定义    G:\新建文件夹\Project2\Project2\SeqStack.obj    栈的储存于操作
错误    3    error LNK2005: "public: int __cdecl SeqStack::Pop(void)" (?Pop@SeqStack@@QEAAHXZ) 已经在 main.obj 中定义    G:\新建文件夹\Project2\Project2\SeqStack.obj    栈的储存于操作
错误    2    error LNK2005: "public: void __cdecl SeqStack::Push(int)" (?Push@SeqStack@@QEAAXH@Z) 已经在 main.obj 中定义    G:\新建文件夹\Project2\Project2\SeqStack.obj    栈的储存于操作


但我把这三个文件放在一个cpp里就不会报错



[此贴子已经被作者于2021-12-11 16:39编辑过]

2 回复
#2
rjsp2021-12-11 16:39
#include"SeqStack.cpp"

你把这处改掉试试
#3
七月与安生鉴2021-12-11 16:50
回复 2楼 rjsp
对了十分感谢
这错误过于傻了
1