该调用已在另一个文件中定义

时间:2019-05-02 16:25:12

标签: c++ linker-errors

我不确定为什么会出现此错误:     LNK2005“ public:__thiscall IntStack :: IntStack(IntStack const&类)”(?? 0IntStack @@ QAE @ ABV0 @@ Z)已在IntStack.obj第19章中定义

我将在下面放置一些代码,如果有人可以告诉我我做错了什么,我将不胜感激。

constexpr

MathStack.cpp

#ifndef MATHSTACK_H
#define MATHSTACK_H
#include "IntStack.h"

class MathStack : public IntStack
{
public:

MathStack(int s) : IntStack(s) {}

void add();
void sub();
void mult();
void div();
void addAll();
void multAll();
};
#endif

IntStack

#include "MathStack.h"
#include "IntStack.cpp"
#include<iostream>


void MathStack::add()
{
int num, sum;
pop(sum);
pop(num);
sum += num;
push(sum);
}

void MathStack::sub()
{
int num, diff;

pop(diff);
pop(num);
diff -= num;
push(diff);
}
void MathStack::mult()
{
int num, mul;
pop(mul);
pop(num);
mul *= num;
push(mul);
}
void MathStack::div()
{
int num, div;
pop(div);
pop(num);
div /= num;
push(div);
}
void MathStack::addAll()
{
int num, sum;
while (!isEmpty())
{
    if (top == 0)
        break;
    add();


}

}
void MathStack::multAll()
{
int num, mul;
while (!isEmpty())
{
    if (top == 0)
        break;
    mult();
}
}

主程序

#include <iostream>
#include "IntStack.h"
using namespace std;

IntStack::IntStack(int size)
{
stackArray = new int[size];
stackSize = size;
top = -1;
}

IntStack::IntStack(const IntStack &obj)
{

if (obj.stackSize > 0)
    stackArray = new int[obj.stackSize];
else
    stackArray = nullptr;

stackSize = obj.stackSize;

for (int count = 0; count < stackSize; count++)
    stackArray[count] = obj.stackArray[count];

top = obj.top;
}

IntStack::~IntStack()
{
delete[] stackArray;
}

void IntStack::push(int num)
{
if (isFull())
{
    cout << "The stack is full.\n";
}
else
{
    top++;
    stackArray[top] = num;
}
}

void IntStack::pop(int &num)
{
if (isEmpty())
{
    cout << "The stack is empty.\n";
}
else
{
    num = stackArray[top];
    top--;
}
}

bool IntStack::isFull() const
{
bool status;

if (top == stackSize - 1)
    status = true;
else
    status = false;

return status;
}

bool IntStack::isEmpty() const
{
bool status;

if (top == -1)
    status = true;
else
    status = false;

return status;
}

0 个答案:

没有答案