Hello
I am trying to pass a array of object to a member function of that class but not successfull. I gone throgh lots of article and document also follow this thread in Daniweb Click Here
This thread is good to understand how to pass an array of object to a function which is not a member function.
But when take the void showAges(fish fishes[]) inside the class compiler give an error 'Undefined structure fish'.
How can I pass the array to member function.
I made some chages to check if I'm right but NO...!

    #include <iostream.h>
    #include <conio.h>
    class fish
    {
         private:
             int age;

         public:
             int getAge() 
             {
                   return age;
             }
             void setAge(int newage)
             {
                    age = newage;
             }
         void showAges(fish fishes[]);
    };
    void fish::showAges(fish fishes[])
    {
           cout << "fish 1 age is: " << fishes[0].getAge() << endl;
           cout << "fish 2 age is: " << fishes[1].getAge() << endl;
           cout << "fish 3 age is: " << fishes[2].getAge() << endl;
    }

    int main()
    {
    fish myFish[3];
    myFish[0].showAges(myFish);
    cout << endl;
    myFish[0].setAge(10);
    myFish[1].setAge(20);
    myFish[2].setAge(30);
    myFish[0].showAges(myFish);
    cin.get();
    return 0;
    }

Recommended Answers

All 11 Replies

There's nothing wrong with your code, but use of <iostream.h> suggests an old compiler that may not respect the standard language. What compiler are you using?

Your code worked when compiled with g++ compiler.

Why is showAges() a member function? It doesn't really need a current instance (this) to work. It works on an array of objects passed as a arguments, like you said. You might as well make it a non-member function. (Oh, my bad, you were just trying it this way. Sorry)

You should write a constructor function.
You might want to make showAges take an extra parameter, the number of elements in the array being passed and iterate over all the elements of the array as opposed working on just the 1st 3.

I agree. I copied your code, removed the spurious conio.h include, corrected to use <iostream> and also prefixed the cin/cout with the correct namespace names (std:cout...) and the program worked as written. The first time the showAges function was called, the array entries were undefined because the compiler created a default constructor which was invoked when each of the 'fish' were created on the stack at entry to the main block.

I am learning C++ on Turbo C++ 3.0

You can use the latest and the greatest versions of C++ for free. If you must use Microsoft products, there are free versions of most languages in Visual Studio that can be legally obtained. If you prefer real computers over the inflatable dolls sold in Redmond ... you can either install Linux on an old computer or you can run VirtualBox on your Windows machine and install Linux on a virtual machine under VirtualBox.

I am learning C++ on Turbo C++ 3.0

That's a very old compiler, and it's going to hold you back significantly. Unless you're forced to use it for some reason, I'd strongly recommend upgrading to something like Code::Blocks or Visual C++ Express.

YEs I am able to run the code in Code::Blocks 10.05 but I need to do it in turbo C++ IDE only as our school doesn't use Code::Block. IF I use it then my marks will be deducted.
I'll be thankful for support on Turbo C++. Also my I'm not able to use getline() in Turbo C++.

It is a shame schools are teaching with 20 year old compilers.

It is a shame schools are teaching with 20 year old compilers.

That's usually correlated with professors being 3 or 4 times older than that. And the last time they checked they were using the latest and greatest compiler.

I am learning C++ on Turbo C++ 3.0

Turbo C++ 3.0 is not just old, it's prehistoric. And I would not say you are "learning C++", you are learning "C++: AT&T version 2.1" which is a sort of experimental and evolving draft of C++ used by AT&T prior to making it a standard language (7 years later). Learning that version of the language has the same appeal as learning English from the turn of the first millenia: it might be interesting from a historical perspective, but not practical, except maybe for a few specialized tasks involving relics of the past (e.g., old programs, old computers, etc.).

I'll be thankful for support on Turbo C++.

It is difficult to help you in that aspect. I don't have access to this old compiler nor pre-standard reference material. So, I have no way to know what will work or not on this compiler. You'll have to start with standard code (that would work on CodeBlocks for example) and then try to resolve whatever issues come up when trying to get it to work on Turbo C++ 3.0. In this particular case, I would just try to pass by pointer:

class fish
{
     private:
         int age;

     public:
         int getAge() 
         {
               return age;
         }
         void setAge(int newage)
         {
                age = newage;
         }
         void showAges(fish* fishes);
};
void fish::showAges(fish* fishes)
{
       cout << "fish 1 age is: " << fishes[0].getAge() << endl;
       cout << "fish 2 age is: " << fishes[1].getAge() << endl;
       cout << "fish 3 age is: " << fishes[2].getAge() << endl;
}

This is because fish[] is technically not the same as fish*, although conversions are automatic, but your old compiler might not support that, or whatever. It's just a guess.

Also my I'm not able to use getline() in Turbo C++.

I believe that you should be able to use istream::getline(). Because the IOStream library and the String library evolved independently at more or less the same time, so a function like getline() which is in the middle of the two libraries might not have been supported yet at those prehistoric times. I can't tell for sure, because I don't have access to any reference material that old.

If your school requires you to use an ancient compiler and deducts marks for using something from the real world, I would render the following as my professional opinion: GET AS FAR AWAY FROM THIS PROF AS POSSIBLE. He/she has nothing to teach that you need to learn.

If you have this rule in writing, I suggest that you send it to the head of the department in confidence. You are paying for the instruction and I think you deserve to be instructed with a compiler that implements an ISO standard version of the language.

Thank you Mike 200 7 your guess work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.