2

Ok, that is my struct:

struct sudurjanie {
    string stoka_ime;
    string proizvoditel;
    double cena;
    int kolichestvo;
};

Next I create queue:

queue<sudurjanie> q;

But when I write this:

cin >> q.push(sudurjanie.stoka_ime);

In error list write this:

IntelliSense: a nonstatic member reference must be relative to a specific object

Ok, when I try this:

cout << q.back();

, why write this:

no operator "<<" matches these operands

?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Nikolai Cekov
  • 177
  • 3
  • 3
  • 11

4 Answers4

6

It sounds like you may have wanted to do this instead:

queue<sudurjanie> q;

sudurjanie item;
cin >> item.stoka_ime;

q.push(item);

The line cin>>q.push(sudurjanie.stoka_ime); doesn't make any sense. Literally, it means:

  1. Pass sudurjanie.stoka_ime to q's push() method. This will fail, because push() takes an argument of type sudurjanie while you have supplied an argument of type string.
  2. Read from cin into the result of the push() call, which is void. This will fail because it makes no sense to read into void.
cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • Ok, when I try this: cout< – Nikolai Cekov Jun 12 '12 at 20:58
  • 2
    Because you are trying to write an object of type `sudurjanie` to `cout`, and `cout` has no idea how it's supposed to represent this type. You would need to overload the `operator<<()` function yourself for this to work. Consider trying `cout << q.back().stoka_ime << endl;` instead. – cdhowie Jun 12 '12 at 20:59
  • Ok but I need to get all data from q? – Nikolai Cekov Jun 12 '12 at 21:11
  • Then either overload `operator<<()` or stream out all elements: `cout << q.back().stoka_ime << q.back().proizvoditel << ...` and so on. – cdhowie Jun 12 '12 at 21:24
  • 4
    @Nikolai : Maybe it's time to consider starting over with [a good book](http://stackoverflow.com/q/388242/636019)... – ildjarn Jun 12 '12 at 21:32
  • 2
    If you need general guidance programming, this is probably not the place to ask. SO deals with specific questions, not "make my whole program work" questions, and we generally assume that you have at least a basic grasp of the programming language you are asking about. – cdhowie Jun 12 '12 at 22:14
3

Your reference to sudurjanie.stoka_ime is invalid as you are naming a member of the type, not an instance of it.

Try:

sudurjanie tmp;
cin >> tmp.stoka_ime;
q.push(tmp);

This will create an instance of sudurjanie, named tmp, read the field, then push the instance onto the queue

Attila
  • 28,265
  • 3
  • 46
  • 55
0

Read the item in first and then add the struct to the queue.

Anon Mail
  • 4,660
  • 1
  • 18
  • 21
0

Your queue is a queue of sudurjanie structs. What you are trying to push into the queue is

a) the name of your struct and not an instance

b) a member of the struct (a string).

mathematician1975
  • 21,161
  • 6
  • 59
  • 101