Open Side Menu Go to the Top

02-09-2013 , 05:00 PM
In my professor's program example, he didn't use a default constructor. I was always told it's best to provide one so that all member variables are initialized in case the overloaded constructor fails. So, I started messing around...

Code:
class foo
{
  public:
           foo(); 
           foo(int arr_in[4][4]);

  private:
            int my_array[4][4];

}
function definitions:

Code:
	foo::foo()
	{
		for(int i = 0; i < 4; i++)
		{
			for(int j = 0; j < 4; j++)
			{
				my_array[i][j] = 0;
			}
		}
	}

	foo::foo(int arr_in[4][4])
	{
		for(int i = 0; i < 4; i++)
		{
			for(int j = 0; j < 4; j++)
			{
				arr_in[i][j] = arr_in[i][j];
			}
		}
	}
What I don't understand is why I can't create a foo object with the default constructor.
C++ default constructor and 2-d array Quote
C++ default constructor and 2-d array
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
C++ default constructor and 2-d array
02-09-2013 , 07:57 PM
Can you describe what happens exactly? Compile error, runtime error?
C++ default constructor and 2-d array Quote
02-09-2013 , 08:24 PM
What's the purpose of this?
arr_in[i][j] = arr_in[i][j];
C++ default constructor and 2-d array Quote
02-09-2013 , 08:33 PM
Quote:
Originally Posted by adios
Can you describe what happens exactly? Compile error, runtime error?
Yeah, sorry, should've clarified. The default constructor and its implementation compile (VS2010 Pro, I can test later on gcc). In main:
Code:
int arr1[4][4] = { {1,2,3,4}, {1,2,3,4}, {1,2,3,4), {1,2,3,4} };
foo test_1(arr1)
test_1.print(); //foo member function
works fine. But this:
Code:
foo test_2() //default, should be filled with o's
does compile, but:
Code:
test_2.print();
does not compile. The error message:
Quote:
.print must have class/struct/union
It's not a foo object?
C++ default constructor and 2-d array Quote
02-09-2013 , 08:41 PM
Quote:
Originally Posted by jmark
What's the purpose of this?
arr_in[i][j] = arr_in[i][j];
oops, sorry, mistyped.
Code:
my_array[i][j] = arr_in[i][j]; //setting foo member var my_array
that's how it is in the program...
C++ default constructor and 2-d array Quote
02-09-2013 , 09:53 PM
Quote:
Originally Posted by Addict_x
Yeah, sorry, should've clarified. The default constructor and its implementation compile (VS2010 Pro, I can test later on gcc). In main:
Code:
int arr1[4][4] = { {1,2,3,4}, {1,2,3,4}, {1,2,3,4), {1,2,3,4} };
foo test_1(arr1)
test_1.print(); //foo member function
works fine. But this:
Code:
foo test_2() //default, should be filled with o's
does compile, but:
Code:
test_2.print();
does not compile. The error message:

It's not a foo object?
Is print a method of foo? You need a ; after your statement instantiating test_2
C++ default constructor and 2-d array Quote
02-09-2013 , 10:38 PM
Quote:
Originally Posted by adios
Is print a method of foo? You need a ; after your statement instantiating test_2
Yes, it's a foo member function and the semicolons are all accounted for in the program (I should have done a better job in my op).
C++ default constructor and 2-d array Quote
02-09-2013 , 10:55 PM
OK, I just figured it out, it was a basic syntax problem. Initially, I declared a foo object implementing the default constructor like this:
Code:
foo test();
instead of the correct syntax:
Code:
foo test; // no parens
C++ default constructor and 2-d array Quote
02-10-2013 , 01:05 AM
It's easier if you post your actual code next time.
C++ default constructor and 2-d array Quote
02-10-2013 , 05:10 AM
use the this type word.

Code:
 this.arr_in[i][j] = arr_in[i][j];
C++ default constructor and 2-d array Quote
C++ default constructor and 2-d array
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
C++ default constructor and 2-d array

      
m