0

I am trying to implement stack using linked list and class, and to the stack class constructor, I am giving reference variables as arguments with default value 0. But it shows an error when I do push operation with an integer literal. How can I implement it by using a default value and reference variable as well?


// ***** Stack using linked list ****

#include <iostream>
using namespace std;

class node{

    node* next;
    int data;

    public:

    node(int &d=0,node* n=NULL):data(d),next(n){}
    node(){}
    ~ node(){}

    friend class stack0;
  
};

class stack0{
   
    int size;
    node* head;

    public:
    
    stack0(){}
    stack0():size(-1),head( new node() ){}

    void push(int &t){
        if (size == -1){
            head->data=t;
            cout<<"& pushed "<<t<<" at "<<head<<" with size "<<size;
            size++;
        }
        else{
            node* temp;temp=head;
            head = new node(t,head);
            cout<<"& pushed "<<t<<" at "<<head<<" with size "<<size;
            head->next=temp;
            size++;
        }
    }
};

int  main(){
    stack0 s;
    s.push(10);
    return 0;
    
}
  • It doesn't make sense to assign a literal to a non-const reference. Imagine if you tried `t = 20;`, what would that mean to the literal you assigned to it? It would be trying to do `10 = 20;`. You can try with a `const int &t` which should work, but there isn't really a reason to use a reference at all here. Just `int t` or `const int t` are what you would typically use here. – François Andrieux Apr 27 '22 at 18:13
  • `int &d=0` is not legal. You can't bind a literal to a non-const lvalue reference. Use a const reference instead: `const int &d=0`. Or simply don't use a reference at all: `int d=0`. Same with the `push()`, use `const int &t` or `int t` instead. – Remy Lebeau Apr 27 '22 at 18:13
  • `stack0():size(-1), ...` so the empty stack has size -1? – fabian Apr 27 '22 at 18:15
  • Semantics: If you are supposed to *implement stack using linked list* you should separate the linked list and the stack. Otherwise `stack` is not using a linked list, it IS the linked list. – user4581301 Apr 27 '22 at 18:26
  • Why are you passing `t` by reference? There's no need. – Paul Sanders Apr 27 '22 at 18:26
  • but aren't we supposed to use pass by reference instead of pass by value to save memory? – Pankaj Chauhan Apr 27 '22 at 19:20
  • the alternative to passing by value is to pass by const reference. You only pass a reference when the function needs to modify the parameter – 463035818_is_not_an_ai Apr 27 '22 at 19:24
  • so we don't have to try to save memory in this case? – Pankaj Chauhan Apr 27 '22 at 19:29
  • _but aren't we supposed to use pass by reference to save memory_ As a bit of a generalisation, not for primitive types, no. These almost always fit in a register so passing them by value is the cheapest way to do it. Passing by reference costs you an additional indirection in the called function, so it's actually more expensive. But when you're passing things that you _don't_ want to be copied (a vector with 10000 elements in it, say), then yes, you should pass by reference. And by `const` reference if the function on the receiving end doesn't need to / shouldn't modify it. – Paul Sanders Apr 27 '22 at 19:31
  • So if I want to pass by reference and also want to have the default arguments, what do I do then? – Pankaj Chauhan Apr 27 '22 at 19:34
  • You can [only do it if passing by const ref](https://wandbox.org/permlink/rB0RBoieXJeRip8I), but that's usually what you should be doing anyway. And this should really be a new question, you're straying a bit here. Also, if you want to address someone specific, please tag your comment with (for example) @paulsanders, else that person probably won't see it. – Paul Sanders Apr 27 '22 at 20:33
  • @PankajChauhan No worries, glad to help out. To improve your skills, you might find [this curated list of C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) useful. – Paul Sanders Apr 28 '22 at 11:03

0 Answers0