•
•
•
•
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
![]() |
•
•
Join Date: May 2006
Posts: 17
Reputation:
Rep Power: 3
Solved Threads: 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:
In my program, the constructor is used to initialize the array to all 0's:
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...)
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.
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.
•
•
•
•
Originally Posted by jaden403
I get the following error:
`array' undeclared (first use this function)
Any help would be greatly appreciated.
•
•
Join Date: May 2006
Posts: 17
Reputation:
Rep Power: 3
Solved Threads: 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?
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?
•
•
•
•
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?
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();
// ...
}[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.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
- Return Array from C++ (C++)
- Query On Passing a datarow array as an argument (VB.NET)
- Class with dynamic array how? (C++)
- template issues - need expert debugger! (C++)
Other Threads in the C++ Forum
- Previous Thread: cout is too slow
- Next Thread: cin vs. getline inquiry



Linear Mode