-2

I have a Problem like this: C++: Storing structs in a stack

My Code:

#include <stdio.h>
#include <stack>
#include <iostream>
#include <string>
using namespace std;

struct adresse{

    string info;
};

int main(){

    string eingabe;
    stack<adresse> Stack1;

    cout << "Bitte Ausdruck eingeben: " << endl;
    getline( cin, eingabe);

    adresse* temp;
    temp = new adresse;
    temp->info = eingabe[0];
    Stack1.push(temp);

    return 0;  
}

The error is:

reference to type 'const value_type'(aka 'const adresse') could not bind to an 
lvalue of type 'adresse *'Stack1.push(temp);

What is wrong?

Thanks

Tommy

Community
  • 1
  • 1
tsid
  • 15
  • 3
  • 1
    You're trying to push a pointer onto your stack and it doesn't accept pointers. – Galik Apr 15 '15 at 18:49
  • 1
    Some hints: Always pair a new with a delete (or delegate to a smart pointer), never store pointers in a container (unless you need polymorphism ), do not fool around and get informed. (BTW: your German is awful) –  Apr 15 '15 at 19:00

5 Answers5

5
adresse temp;
temp.info = eingabe;
Stack1.push(temp); // maybe `std::move(temp)` instead of `temp`

instead of the new and stuff you did with temp. new is not required here. [0] is not required. Pointers are not required.

In general, new means "create a copy of this whose lifetime I will manage manually, and whose access is slower". It returns a pointer to the thing you newed. If you don't need fine-grained control over the lifetime of what you are creating, create it on the stack instead.

In some modern C++ coding styles, all calls to new outside of specific low level resource management functions are discouraged (like make_shared or make_unique or boost::variant or the like). But you still need to understand pointers.

You created a pointer to some data, and you tried to store it in a container that expects values of some data. The container wanted a reference to an existing chunk of data which it would copy: you gave it a pointer to some data instead. These are not the same type, so the compiler gave you an error saying there was no way to automatically convert from one type to the other.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
0

You declared your stack to contain adresses, but are trying to push the address of an adresse.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

First of all to do what you are doing you would need a stack like so,

stack<adresse*> Stack1;

If you want to use your own stack then push it like so

Stack1.push(*temp);
Abhishek Vasisht
  • 406
  • 6
  • 18
0

When writing Stack1.push(temp), temp is a adresse *, while Stack1.push expects an adresse const & argument.

This is what the compiler is trying to tell you.

To fix this issue, just do not store temp as a pointer to the object but rather directly as the object.

Ekleog
  • 1,054
  • 7
  • 19
0

You have a few problems.

First your header <stdio.h> is a C header, in C++ use <cstdio>.

Next you are creating a pointer to an object and trying to push that pointer onto your stack. No need to make a pointer, just define the object directly.

Finally using eingabe[0] only gives you the first letter of the word stored in eingabe.

Try the following:

//#include <stdio.h> // not this in C++
#include <cstdio> // use this instead
#include <stack>
#include <iostream>
#include <string>
using namespace std;

struct adresse
{

    string info;
};

int main()
{

    string eingabe;
    stack<adresse> Stack1;

    cout << "Bitte Ausdruck eingeben: " << endl;
    getline(cin, eingabe);

    // no need to make pointers just use
    // adresse as if it were built in
    adresse temp;
    temp.info = eingabe; // [0] will only give you the first letter
    Stack1.push(temp);

    return 0;
}
Galik
  • 47,303
  • 4
  • 80
  • 117