Having some issues with function/const

Okay, so for an assignment I need to write a function called find() that returns a reference to a vector. So I have vector <int> & find(string & key); If I do this, I get the obvious warning warning: reference to local variable 'lineNum' returned [enabled by default].

If I do vector<int> & find(string & key) const; I get a huge error that starts out like
1
2
In member function 'std::vector<int>& index_table::find(std::string&) const':
indextable.cpp:74:30: error: no match for 'operator='


Am I using the const identifier incorrectly? If not, is there anything else that I should change to alleviate this headache?
So from the warning, I guess your code looks like:

1
2
3
4
5
6
vector<int>& find(string &key)
{
    vector<int> lineNum;
    // other things
    return lineNum;
}


You shouldn't return a reference to a local variable. I don't why you have to return a reference to a vector. To my understanding, you could declare the function like this:

vector<int> find(const string &key)

or this

1
2
3
4
5
vector<int>& find(vector<int> &lineNum, const string &key)
{
    // operate on passed lineNum directly
    return lineNum;
}

For the assignment it requires us to return a reference to a vector (for some reason).
Did your assignment provide the function prototype? Maybe you can try my 2nd option.
That seemed to make it work. The assignment said to function should take a string reference, but didn't specify anything else. That's the only way I can seem to get it though.
Can you post exact wording of your assigment?
Topic archived. No new replies allowed.