Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8751813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:07:07+00:00 2026-06-13T13:07:07+00:00

So in my header file for a class Foo I declare a 2D array

  • 0

So in my header file for a class Foo I declare a 2D array of ints like so:

int board[][];

I am intentionally leaving out a size because I want to set that in the constructor. board[][] will not change in size once initialized. One of my constructors looks like this:

Foo(int _board[][]);

In this I want to set board[][] to the same size as _board[][] and to also copy the contents. I have tried using this in the .cpp implementation of Foo:

Foo::Foo(int _board[][]){
board[][] = _board[][]; //Does not work as intended
}

However, this does not work as intended. How can I make board[][] be the same size and have the same contents as _board[][] in the constructor?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:07:08+00:00Added an answer on June 13, 2026 at 1:07 pm

    C++ is different than Java. int a[][]; is not allowed as variable type. Some misleading C++ feature is that first size is allowed to be left empty:

    int foo(int a[]);
    int foo(int a[][3]); 
    int foo(int a[][3][4]); 
    

    Another misleading C++ feature is that this is allowed in initialization of an array (compiler will count the size):

    int a[][] = {{1,2}, {1,2,3,4}, {1}};
    

    which is equivalent to:

    int a[3][4] = {{1,2}, {1,2,3,4}, {1}};
    

    For your very case – use std::vector:

    std::vector<std::vector<int>> board;
    Foo(std::vector<std::vector<int>> board) : board(board) {}
    

    If you can’t use std::vector for whatever reason – then only solution is to use int** with both sizes:

    int** board;
    size_t s1;
    size_t s2;
    Foo(int** board = NULL, size_t s1 = 0, size_t s2 = 0) : board(board), s1(s1), s2(s2) {}
    

    But be aware that you cannot use this way:

    int board[][] = {{1,1,2},{1,2,2}};
    Foo foo((int**)board,2,3);
    

    because you must provide a dynamic array:

    int** board = new int*[2] { new int[3]{1,1,2}, new int[3]{1,2,2}};
    

    And since that – you have to implement copy constructor, assignment operator and destructor:

    Foo(const Foo& other) : TODO { TODO }  
    ~Foo() { TODO }  
    Foo& operator = (Foo other) { TODO }
    

    So, just use std::vector.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I have code which looks like this inside a header file: class Bar;
//header file class Foo { public: struct FooBar { bool ok; string msg; };
I'm using boost::scoped_ptr and a forward declaration in a header file: //Bar.h class Foo;
For example: Base class header file has: enum FOO { FOO_A, FOO_B, FOO_C, FOO_USERSTART
Header file: // pe10-8arr.h -- header file for a simple list class #ifndef SIMPLEST_
Initializing in the header file i get the following error: invalid in-class initialization of
I have all the class definitions in a header file: ModelModule.h. I have provided
I've got a class called Task. The header file goes as such: class Task
Assume I've declared a function (or class, doesn't matter) in a header file, which
header file: private: vector<int*>* nums; public slots: void buttonClicked(); cpp file: NewWindow(){ int one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.