CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2010
    Posts
    907

    How to return a multidimensional array?

    I have put aside C++ for a while. And I am back to it.
    I forgot how to return a 2 dimensional arrays from a method?
    Code:
    Node** GetNodeMap() const { return m_Nodes; }
    
    private:
    	Node m_Nodes[60][60];
    How can I achieve that?
    Thanks
    Jack

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to return a multidimensional array?

    Quote Originally Posted by lucky6969b View Post
    Code:
    Node** GetNodeMap() const { return m_Nodes; }
    
    private:
    	Node m_Nodes[60][60];
    Doesn't this code work?
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2010
    Posts
    907

    Re: How to return a multidimensional array?

    Error: return value type doesn't match the function type
    I don't get an error code though.
    The m_Nodes is underlined. And also I have left out the const identifier as well.
    Thanks
    Jack

  4. #4
    Join Date
    Jul 2013
    Posts
    576

    Re: How to return a multidimensional array?

    Quote Originally Posted by lucky6969b View Post
    I don't get an error code though.
    If you plan to return a pointer to the 2D array you are asking for trouble. Chances are it gets stale and there will be a much harder to find runtime bug.

    The most immediate solution is to return a copy of the array.

    Another solution is to place the array on the heap and access it by way of a smart pointer. Then you would return a smart pointer.
    Last edited by razzle; May 2nd, 2015 at 02:41 AM.

  5. #5
    Join Date
    Dec 2010
    Posts
    907

    Re: How to return a multidimensional array?

    If I return a copy of the array, this also didn't compile
    Code:
    Node[60][60] GetNodeMap()  { return m_Nodes; }
    Nor this
    Code:
    Node[][] GetNodeMap()  { return m_Nodes; }
    Oh. I've just completely forgotten the syntax, please kindly help.
    Thanks
    Jack

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to return a multidimensional array?

    In c++ functions can't return an array by value. You can use the array container in c++11 as in this example
    Code:
    class mymap {
    private:
    	struct Node{
    		int data;
    	};
    
    public:
    	typedef array < array<Node, 60>, 60 > ARRTYPE;
    
    	ARRTYPE GetNodeMap() const
    	{
    		return m_Nodes;
    	}
    
    private:
    	ARRTYPE m_Nodes;
    
    };
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured