Thread: print array member function

  1. #1
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    print array member function

    I am trying to write a simple function that will print my array of clocks. I have a function called display_time() for the clocks:

    Code:
    void clock::display_time()
    {
    	cout << "Current time is: " << _hours << ":" << setfill ('0') << setw(2) << _mins << " ";
    	if (_morn)
    		cout << "AM" << endl;
    	else
    		cout << "PM" << endl;
    }
    And my print function is:

    Code:
    void clock::print_clocks(clock& array)
    {
    	for (int i = 0; i < array._used; i ++)
    	{
    		cout << array[i].display_time() << endl;
    	}
    }
    The error I'm getting at compile is:

    "no match for clock&[int&]" at the "cout << array[i].... line."

    What does this mean?

    Brian
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    void clock::print_clocks(clock* array)
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66
    That made things much worse. Should I be passing a pointer to the function?
    "In theory, there is no difference between theory and practice. But, in practice, there is."
    - Jan L.A. van de Snepscheut

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    161
    You haven't shown us the entire declaration of your clock class, so we have no idea what your intent is there. Does the clock class store an array of all your clocks with _used counting the number and operator[] providing access? If not, then you'll have to pass an array (which decays to a pointer) to the function.

    By the way, if print_clocks() is receiving an array of clocks and not using or modifying any member data, then there is really no reason for it to be a member function.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    You are trying to pass an array? Arrays are always passed as pointers to their first member.

    You will also need to pass in the array size, as in this example:

    Code:
    #include <iostream>
    
    void PrintArray(int * array, int count){
    	for (int i = 0; i < count; i++)
    		std::cout << "[" << array[i] << "]";
    	std::cout << std::endl;
    }
    
    int main(){
    	int myArray[5] = {6,2,1,3,4};
    	PrintArray(myArray,5);
    	int * myArray2 = new int[6];
    	for (int i = 0 ;i < 6; i++)
    		myArray2[i] = i * 2 + 1;
    	PrintArray(myArray2,6);
    	delete[] myArray2;
    
    }
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM