需要帮助运算符重载

时间:2014-05-30 03:01:04

标签: c++ string operator-overloading

更新了我的代码,现在在S.push_back(qElem(str))给我一个错误;

我正在尝试创建一个+ =运算符,以便将字符串压入我的堆栈,但是我在成功时遇到了最困难的时间。我一直收到错误。例如,从我下面的代码中我在main.cpp中收到错误(FIXED):

sta += "Clean";  //ERROR STATES: No viable overloaded '+='

然后在不同的时间使用不同的代码,我将收到错误(FIXED):

S.push_back(qElem(str)); //ERROR STATES: No matching conversion for functional-style cast from 'std::__1::basic_string<char>' to 'qElem'

我对操作员来说真的很难,任何有关如何完成这项工作的帮助将不胜感激。

的main.cpp

#include "queue.h"
#include "stack.h"

#include <iostream>
#include <string>
#include <vector>


using namespace std;

int main()
{
    string more;
    string task;
    int priority;
    string yes;

    int stack;

    PriQueue<string> que;
    Stack<string> sta;

    do { //Do while loop, to enter more tasks

        cout << "Would you like to add to the stack or the priority queue (1/2)?" << endl;
        cout << "1. Stack" << endl; cout << "2. Priority Queue" << endl;
        cin >> stack;

        if (stack == 1) {
            sta += "Clean";
            sta.peek();

        }

        else
        {
            //Taking in the task
            cout << "What is your task in ONE word?" << endl;
            cin >> task;

            cout << "What is the priority level of the task on a scale from 1 to 10 (10 = highest priority)?" << endl;
            cin >> priority;
            //Taking in the priority
            if (priority > 10 || priority < 1) {
                priority = 5;
                cout << "Invalid Priority Level, Automatically set to 5." << endl;
            }

            que.enqueue(task, priority); //Taking and storing the task

        }

        cout << "Would you like to RUN this again (y/n)?" << endl;
        cin >> more;

    } while (more == "y" || more == "Y" || more == "Yes" || more == "yes"); //End of loop response

    que.size(); //Returning your the number of tasks you have

    que.peek(); //Returning your first task

    cout << "Would you like us to delete your first task (y/n)?" << endl; //Using "dequeue" for example
    cin >> yes;
    if (yes == "y" || yes == "Y" || yes == "Yes" || yes == "yes") {

        que.dequeue(); //Deleting your first task

        que.peek(); //Returning your NEW first task
    }

    else {cout << "Thank You, Goodbye" << endl;}


    return 0;
}

stack.h

#ifndef Queue_stack_h
#define Queue_stack_h

#include "queue.h"
#include "error.h"

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

template<class T>
class Stack : private vector<qElem>
{
    vector<qElem> S;

public:
    Stack<T> push(T str);
    void pop();
    void peek();

    Stack<T> operator += (const T& str);

    Stack<T> operator -= (string const &str);


};


template<class T>
Stack<T> Stack<T>::push(T str)
{
    S.push_back(qElem(str));

}

template<class T>
void Stack<T>::pop()
{
    if (S.empty()) {  //First check if stack is empty, if not, print out frist in stack
        cout << "You have none in stack!" << endl;
    }

    else {
        cout << "FIRST IN STACK: " << S.front() << endl;
    }


    S.erase(S.begin()); //Now erase first in stack

    if (S.empty()) {
        cout << "You have NONE in STACK!" << endl;
    }

    else {

    }

}

template<class T>
void Stack<T>::peek()
{
    if (S.empty()) {  //First check if stack is empty, if not, print out frist in stack
        cout << "You have no stack!" << endl;
    }

    else {
        cout << "FIRST IN STACK: " << &S.front() << endl;
    }

}

template<class T>
Stack<T> Stack<T>::operator += (const T& str)
{
    this->push(str);
    return *this;

}

template<class T>
Stack<T> Stack<T>::operator -= (string const &str)
{


}

#endif

3 个答案:

答案 0 :(得分:2)

来自Scott Meyers的Effective C ++:

  

第10项

     

让赋值运算符返回对* this

的引用

按照这个惯例,你的重载应该是这样的:

Stack<T>& operator+=(const Stack<T>& rhs)
{
    ...
    return *this;
}

那就是说,看起来你正试图在你的Stack中添加一个字符串,所以你的重载就变成了这个(这似乎是非传统的):

Stack<T>& operator+=(const T& rhs)
{
    this->push(rhs);
    return *this;
}

我说这是非常规的,因为您通常希望+ =运算符将两个相似的对象添加到一起。

答案 1 :(得分:1)

为此,您必须:

  • 将T指定为+ =的参数,以便编译器找到合适的运算符
  • 返回引用,而不是Stack的副本。

示例:

Stack<T>& operator+=(const T& obj)
{
  this->push(obj);
  return *this;
}

答案 2 :(得分:-2)

这里是一个关于如何重载+=

的示例函数
// string::operator+=
#include <iostream>
#include <string>

int main ()
{
  std::string name ("John");
  std::string family ("Smith");
  name += " K. ";         // c-string
  name += family;         // string
  name += '\n';           // character

  std::cout << name;
  return 0;
}

输出:John K. Smith 这是最常见的例子,其中一切都应该有效。请注意std::string用法。