使用类的对象作为方法中的参数

时间:2016-03-12 15:36:17

标签: c++ oop

我有类Stack的方法,它比较了这个类的2个对象:

bool comparison(T &stack) {
    if (size == stack.size)
        for (int i = 0; i < size; i++) {
            if (!this->stackPr[i].comparison(stack.stackPr[i]))
                return false;
        }
    else
        return false;
    return true;
}

并使用类Time的方法:

bool comparison(Time &time) {
    if ((this->hours == time.hours) && (this->minutes == time.minutes) && (this->seconds == time.seconds))
        return true;
    return false;

当我尝试在main中使用这个comman时:

bool temp = stack3.comparison(stack4);

MVS强调| stack4 |并告诉我错误:

a reference of type "Time &"(non-const qualified) cannot be initialized with a value of type Stack<Time>

我怎么能处理这个问题? 谢谢你的回答:)

有课程堆栈:

class Stack {
private:
T *stackPr;
int size;
int top;
public:
//----------------CONSTRUCTORS-----------------
Stack(int n) {
    if (n > 0)
        size = n;
    else
        size = 10;
    stackPr = new T[size];
    top = -1;
}
Stack() {
    size = 10;
    stackPr = new T[size];
    top = -1;
}
Stack(Stack &stack) {
    stackPr = new T[stack.size];
    size = stack.size;
    top = stack.top;
    for (int i = 0; i < size; i++)
        stackPr[i] = stack.stackPr[i];
}
Stack(T *objs, int sizeMass) {
    size = sizeMass;
    stackPr = new T[size];
    for (int i = 0; i < sizeMass; i++) {
        this->push(objs[i]);
    }
}

//----------------DESTRUCTOR-------------------
~Stack() {
    delete[] stackPr;
}

//-----------------METHODS---------------------

//Add element to stack
void push(T &element) {
    if (top == size - 1)
        cout << "\nThere's no more place!!!\n";
    else {
        top++;
        stackPr[top] = element;
        cout << "\nElement was succesfully pushed\n";
    }
}

//Read + Delete
T pop() {
    if (top == -1)
        cout << "\nStack is empty\n";
    else {
        T temp = stackPr[top];
        stackPr[top] = 0;
        top--;
        cout << "\nElement was succesfully poped and deleted\n";
        return temp;
    }
}

//Read
T popup() {
    if (top == -1)
        cout << "\nStack is empty\n";
    else {
        cout << "\nElement was succesfully popped\n";
        return stackPr[top];
    }
}

//Comparison of 2 stacks
bool comparison(T &stack) {
    if (size == stack.size)
        for (int i = 0; i < size; i++) {
            if (!this->stackPr[i].comparison(stack.stackPr[i]))
                return false;
        }
    else
        return false;
    return true;
}
};

2 个答案:

答案 0 :(得分:1)

在Stack类

中试试这个

变化:

bool comparison(T &stack) {

为此:

bool comparison(Stack<T> &stack) {

答案 1 :(得分:0)

首先,放弃此comparison功能,它会阻碍您的代码,而是使用==

其次,在比较函数中使用const Stack<T>

最后,使用auto来推断变量的类型。

这是一个示例,显示了我刚写的内容的基础知识:

#include <iostream>

using namespace std;

struct Time 
{
    bool operator==(const Time& time)
    {
        return true;// adjust it with your own needs.
    }
};

template<typename T>
struct Stack
{
    T val;
    Stack(T& val_): val(val_) {}
    bool operator==(const Stack<T>& stack)
    {
        return this->val == stack.val; // here is your business logic of comparison
    }
};

int main()
{
    Time t1;
    Time t2;
    Stack<Time> myStack1(t1);
    Stack<Time> myStack2(t2);
    auto temp = myStack1 == myStack2;
    cout << temp << endl;
    return 0;
}