c++ - Need help operator overloading -


updated code gives me error @ s.push_back(qelem(str));

i trying create += operator in order push string stack having difficult time @ succeeding. keep receiving errors. example, code below receiving error within main.cpp @ (fixed):

sta += "clean";  //error states: no viable overloaded '+=' 

then different code @ different times receive error @ (fixed):

s.push_back(qelem(str)); //error states: no matching conversion functional-style cast 'std::__1::basic_string<char>' 'qelem' 

i having hard time operator, on how make work appreciated.

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 while loop, enter more tasks          cout << "would add stack or 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 task             cout << "what task in 1 word?" << endl;             cin >> task;              cout << "what priority level of task on scale 1 10 (10 = highest priority)?" << endl;             cin >> priority;             //taking in priority             if (priority > 10 || priority < 1) {                 priority = 5;                 cout << "invalid priority level, automatically set 5." << endl;             }              que.enqueue(task, priority); //taking , storing task          }          cout << "would run again (y/n)?" << endl;         cin >> more;      } while (more == "y" || more == "y" || more == "yes" || more == "yes"); //end of loop response      que.size(); //returning number of tasks have      que.peek(); //returning first task      cout << "would delete first task (y/n)?" << endl; //using "dequeue" example     cin >> yes;     if (yes == "y" || yes == "y" || yes == "yes" || yes == "yes") {          que.dequeue(); //deleting first task          que.peek(); //returning 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 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 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 

from effective c++ scott meyers:

item 10

have assignment operators return reference *this

in keeping convention, overload should this:

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

that said, looks you're trying add string stack, overload becomes (which seems non-conventional):

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

i unconventional because you'd expect += operator add 2 objects together.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -