0

Update for anyone having trouble returning multidimensional arrays

Further reading: Returning multidimensional array from function

I've declared a static int variable in my header. I've defined it in the .cpp file (Implementation file?), as shown with the relevant code below...

Card.h

#ifndef CARD_H
#define CARD_H

class Card {
private:
    static int  _palette[][3];
public:
    static int  palette();
};

#endif /* CARD_H */

Card.cpp

int Card::_palette[][3]=    {
    {168, 0,   32},
    {228, 92,  16},
    {248, 216, 120},
    {88,  216, 84},
    {0,   120, 248},
    {104, 68,  252},
    {216, 0,   204},
    {248, 120, 248}
};

static int palette(){
    return _palette;
}

But when I compile, I get this error:

..\source\src\Card.cpp: In function 'int palette()':
..\source\src\Card.cpp:42:9: error: '_palette' was not declared in this scope
  return _palette;

Shouldn't my accessor function palette() be able to get the value of private member _palette?

Community
  • 1
  • 1
Username
  • 3,463
  • 11
  • 68
  • 111

2 Answers2

2

You forgot the Card::

int (*Card::palette())[3]{
    return _palette;
}

You shouldn't have static in the method definition. Also, you're trying to return an int[][] when you should return an int.

Change your class to this:

class Card {
private:
    static int  _palette[][3];
public:
    static int  (*palette())[3];
};
IsaacH
  • 240
  • 2
  • 5
  • I now have this error: `error: invalid conversion from 'int (*)[3]' to 'int' [-fpermissive]`. Changing function type to `static int*` gives this error: `error: cannot declare member function 'static int* Card::palette()' to have static linkage [-fpermissive]` – Username Feb 10 '16 at 05:15
  • @Username You probably want to remove the `static` keyword from the method definition. Keep it in your class definition, but only if you actually need it. – IsaacH Feb 10 '16 at 05:20
  • @IsaacH Even after changing class declaration of the public member function to `int**`, along with it's definition, I still get the `invalid conversion` error. I want to access the array so I can loop thru it. That means I want to return the array, no? – Username Feb 10 '16 at 05:30
  • @Username see code above.Don't forget to update the type in your class too. – IsaacH Feb 10 '16 at 05:39
  • Your latest edit gives me no errors. Thought I don't understand what's happening. I'm new to C++. Can you help me understand what your code does? – Username Feb 10 '16 at 05:48
  • 1
    @Username Because you are returning `_palette`, you have to make the function type match that. `_palette` has the type `int[][3]`, so `(*palette())[3]` is compatible with that. `static` isn't allowed in the method definition, nothing to understand there, just the rule (same goes for `virtual`). And adding the `Card::` was required so we know it's a method and not just some random free function. – IsaacH Feb 10 '16 at 05:52
  • 1
    Sorry I suggested `**int` the first time. That would work in C, which I'm more used to, but not C++. – IsaacH Feb 10 '16 at 05:56
1

Firstly, the method name is Card::palette, not just palette. And Card::palette is what you should use in the method definition.

Secondly, static method definition is not supposed to include the keyword static.

Thirdly, how are you expecting to be able to return an array as an int value??? Given the declaration of your _palette array, to return it from a function you'd have to use either int (*)[3] return type or int (&)[][3] return type

int (*Card::palette())[3] {
    return _palette;
}

or

int (&Card::palette())[][3] {
    return _palette;
}

And a typedef can make it more readable.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765