User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,905 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,597 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1669 | Replies: 5
Reply
Join Date: May 2006
Posts: 17
Reputation: jaden403 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
jaden403 jaden403 is offline Offline
Newbie Poster

Question constructor with an array as an argument

  #1  
May 29th, 2006
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,449
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: constructor with an array as an argument

  #2  
May 29th, 2006
Originally Posted by jaden403
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.)
Reply With Quote  
Join Date: May 2006
Posts: 17
Reputation: jaden403 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
jaden403 jaden403 is offline Offline
Newbie Poster

Re: constructor with an array as an argument

  #3  
May 29th, 2006
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?
Reply With Quote  
Join Date: Apr 2004
Posts: 3,449
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: constructor with an array as an argument

  #4  
May 29th, 2006
Originally Posted by jaden403
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;
   // ...
}
Last edited by Dave Sinkula : May 30th, 2006 at 10:43 am.
Reply With Quote  
Join Date: May 2006
Posts: 17
Reputation: jaden403 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
jaden403 jaden403 is offline Offline
Newbie Poster

Re: constructor with an array as an argument

  #5  
Jun 4th, 2006
This helps...thanks!
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

Re: constructor with an array as an argument

  #6  
Jun 4th, 2006
Passing arrays is always a pain. I like how Dave just kept it in the public member section... Works out easier that way.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 7:37 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC