5

I'm a c++ student and could use some help with understanding and doing this part of my assignment.

I have a vector of SalesItem objects:

class Invoice
{
public:
    //blabla
    vector<SalesItem> *getSalesItems(); //code provided by the assignment.
private:
{
    //blabla
    vector<SalesItem> salesItems;
};

and I need to return that vector as a reference:

void Invoice::getSalesItems() //error on this line. Code provided by assignment.
{
    return salesItems; //error on this line.
}

Now, I know that the things that are giving me errors are obviously wrong, I don't even have any pointers or references in there. I'm posting those few lines of code just as an example of what I would like to see (or more realistically, what makes sense to me.)

I want a get function that works like other get functions for types like int or string, except this one has to return by reference (as required by the assignment.)

Unfortunately, my understanding of vectors and references is not up to merit for this problem, and I do not have any educational resources that can help me on this. If someone could please help me understand what to do, I would greatly appreciate it.

Any extra information will be gladly given.

jww
  • 97,681
  • 90
  • 411
  • 885
Crazierinzane
  • 71
  • 1
  • 1
  • 8
  • 3
    `vector *getSalesItems();` returns a pointer and does not return a reference. `void Invoice::getSalesItems()` doesn't return anything. The function definition in the class and the declaration must be the same: `vector & getSalesItems();` returns a vector by refernece. – Jerry Jeremiah Apr 23 '14 at 01:24

2 Answers2

13

You need to specify the return type. Moreover, it is better to provide both const and non-const versions. Code as follows:

class Invoice
{
public:
          vector<SalesItem> &getSalesItems()       { return salesItems; }
    const vector<SalesItem> &getSalesItems() const { return salesItems; }
private:
    vector<SalesItem> salesItems;
};

Sample usage:

Invoice invoice;
vector<SalesItem> &saleItems = invoice.getSalesItems(); // call the first (non-const) version
const Invoice &const_invoice = invoice;
const vector<SalesItem> &saleItems = const_invoice.getSalesItems(); // call the second (const) version
Danqi Wang
  • 1,597
  • 1
  • 10
  • 28
  • slightly off-topic, but I've always thought this technique (a "getter" getting a reference) was pointless obfuscation compared to just having the member public. – M.M Apr 23 '14 at 04:35
  • 1
    @MattMcNabb I don't advocate `getter` in this case, but having the member public is an even worse choice. A good discussion [here](http://stackoverflow.com/a/2977045/930095) – Danqi Wang Apr 23 '14 at 05:54
  • That discussion is mostly about "getters" that return by value, and one post even says that the reference version is basically the same as having a public member. (the only difference is you can't form a pointer-to-member on the reference). – M.M Apr 23 '14 at 06:01
  • 1
    I agree that a `getter` returns a reference is not good, but public member is worse. Quoted from the link: "Because once you start making getters/setters, people stop designing objects with a critical eye toward what data should be visible and what data should not. With public members it is even worse because the tendency becomes to make everything public." – Danqi Wang Apr 23 '14 at 07:37
11

You are returning void in the function implementation. And you also don't have correct declaration to get return by reference.

The code should be like this:

Header:

class Invoice
{
public:
    vector<SalesItem> &getSalesItems();
private:
    vector<SalesItem> salesItems;
};

Implementation:

vector<SalesItem> &Invoice::getSalesItems() {
    return salesItems;
}
Krypton
  • 3,337
  • 5
  • 32
  • 52