0

Hello,

I think my problem is a simple one (but probably isn't) and something that I have foolishy overlooked. I have created a class (Time) with a constructor that has as its argument a 3x3 int array. It is defined in a header file as follows:

public:
Time(int [][columns]); //constructor
 
private:
int array[rows][columns];

In my program, the constructor is used to initialize the array to all 0's:

Time::Time(int array[][columns]) //time constructor
{
  for (int i=0; i<rows; i++)
    {  
      for (int j=0; j<columns; j++)
        array[i][j] = 0;
    }
}

Finally, under main() I attempt to instantiate object t of class Time and then use that object to call member functions (t.doMath(array), etc...)

Time t(array)

It is here that I run into problems. I get the following error:

`array' undeclared (first use this function)

Any help would be greatly appreciated.

3
Contributors
5
Replies
6
Views
10 Years
Discussion Span
Last Post by server_crash
0

I get the following error:

`array' undeclared (first use this function)

Any help would be greatly appreciated.

As the message says, how and where do you define array? (The one inside the class does not count.)

0

Sir,

First, thank you for your response.

Second, I was under the assumption that if an array (or variable) was declared public in the class definition that one was not required to declare it again in main(). I see now that assumption was incorrect.

Correct me if I am wrong here: only those arrays/variables explicitly passed to a class defined member function can be placed into either public or private. All other arrays/variables must be declared in main() - or such as the case may be. Is that correct?

0

Correct me if I am wrong here: only those arrays/variables explicitly passed to a class defined member function can be placed into either public or private. All other arrays/variables must be declared in main() - or such as the case may be. Is that correct?

Members of the class are members of the class, you don't pass a member into the class. Perhaps some initializer for a member is passed.

It would seem that you don't want to pass any parameters to your constructor.

const int rows = 3;
const int columns = 4;

class Time
{
   int array[rows][columns];
public:
   Time();
};

Time::Time()
{
   for ( int i = 0; i < rows; ++i )
   {
      for ( int j = 0; j < columns; ++j )
      {
         array[i][j] = 0;
      }
   }
}

int main()
{
   Time t();
   // ...
}

Variables may be declared in a variety of scopes: in a function such as main, at file scope (a "global"), dynamically allocated via a pointer, or within block scope (such as a for statement).

[edit]Gah! Let's make a little change to main, too.
http://parashift.com/c++-faq-lite/ctors.html#faq-10.2

int main()
{
   Time t;
   // ...
}
0

Passing arrays is always a pain. I like how Dave just kept it in the public member section... Works out easier that way.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.