1

I'm trying to return a pointer to an array for my function prototype.

class NAV                  
{
    string date;
    float nav;
public:
    NAV(const string&);  
};

const int HistorySize = 300;

class MutualFund
{
    string ticker;
    NAV* history[HistorySize];
public:
    MutualFund(const string& ticker_, const string& historyFile);
    ~MutualFund();
    NAV** getArray() const{return history;}
    void report() const;

};

For NAV** getArray() const{return history;}, I'm getting a compiler error:

error: invalid conversion from 'NAV* const*' to 'NAV**' [-fpermissive]

Any ideas?

6
  • Do you actually need the pointer to the structure array? Maybe you need just the array and return its reference in getArray() Commented Mar 6, 2016 at 4:58
  • How would you return the reference of the history array? Commented Mar 6, 2016 at 5:13
  • FYI the error means that return history returns NAV* const * but you are trying to return it as NAV**. Commented Mar 6, 2016 at 5:23
  • Did you intend for history to be 300 pointers to NAV? Or is history supposed to be an array of 300 NAVs? Commented Mar 6, 2016 at 5:27
  • 1
    In Visual Studio the error is error C2440: 'return': cannot convert from 'NAV *const [300]' to 'NAV **' Commented Mar 6, 2016 at 5:33

2 Answers 2

4

In NAV** getArray() const{return history;} const means the programmer promises that calling this function will not cause changes to the state of the MutualFund. By returning a non-const pointer, NAV**, you are opening up the possibility for the state to be changed through use of the returned pointer. The compiler will not allow this and is telling you that it can only return a pointer to constant data: NAV* const*.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm having trouble implementing your solution. When I use const on NAV**, the compiler says invalid conversion from NAV* const* to const NAV**. Is there any way to return the member "history"? If so, what would the history return type be?
The return type is NAV* const*, just like the error message says.
0

Your getter is a const method, so during its execution, all data members are considered const as well. That's why the conversion error says it is converting from const to non-const, since your return value is non-const.

2 Comments

Nope; you get this casting error unless you do "NAV* const*".
@kfsone Ah, quite so, I missed that there was a second * after the const in the error line. I edited that note out. Nevertheless, it's still true that the const method is turning the member const, so taking its address gives a pointer to const data, which is why it's incompatible with the non-const return type. Either the method shouldn't be const, or the return type should be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.