Hello I need to create an array of pointers for class dog is one class and showDog is the derived class. I need to input from a data file and store the variables to these classes. In the file there is a D for dog or S for showDog between each set of data. I am having trouble with the pointer part of the problem and am not quite sure what I am doing wrong. Here is the part of my code that I believe the problem is. Any help I could get I would greatly appreciate.

-thanks

const int NUM_DOGS = 10;
dog *dogList[NUM_DOGS];
showDog *showList[NUM_DOGS];

    ifstream numdata;
    numdata.open("pgm8.dat");

    if ( !numdata )                                                       // Check to make sure file was opened
    {
        cout << "Error opening file" << endl;
    }
int i;
for(i=0;i<NUM_DOGS;i++)
{
    string test, nn, cc;
    double ww;
    int se, sw;

    numdata>>test;
    cout <<" test= " << test << endl;
    if (test == "D")
    {   i++;
        numdata>>nn;
        i++;
        numdata>>cc;
        i++;
        numdata>>ww;
        dogList[NUM_DOGS] = new dog(nn, nn, ww);
    }
    else
    {   i++;
        numdata>>nn;
        i++;
        numdata>>cc;
        i++;
        numdata>>ww;
        showList[i] = new showDog(nn, cc, ww, se, sw);
    }
}
    numdata.close();

                                                                        //array output
for(i=0;i<NUM_DOGS;i++)
{
   cout <<"dog items: " << dogList[i]->displayDog() << endl;
   cout <<"showdog items: " << showList[i]->displayDog() << endl;
}

Also here is the data file

D
Fido
50
Black
S
Rover
25
Brown
3
1
D
Shep
10
White
S
Shaggy
80
Black
10
3

Recommended Answers

All 9 Replies

A couple of things to look out for:

1) In your loop, you are iterating 10 times. However, your data file may not have 10 "dog" items in it. You might be better off reading until EOF.
2) Your "for" loop increments your variable ("i"), but then inside the loop you are incrementing it as well. This will cause trouble.
3) when you assign a new item to "dogList" - do you really want to be assigning it to a spot in the array specified by "NUM_DOGS"? Your assignment to "showDogs" looks a bit better (of course, after you fix the incrementing issue with "i").

You're pretty close - just a bit of cleanup and you should be in good shape.

ok I see what you are saying, I think I have fixed those problems not sure why I thought I had to increment i. here is my new code, but I am still getting a lot of syntax errors which i think are connected to my output displayDogs function. My guidelines say that I need to make the function virtual to handle the derived class. I'm not quite sure how to do this though.

const int NUM_DOGS = 10;
dog *dogList[NUM_DOGS];
showDog *showList[NUM_DOGS];

    ifstream numdata;
    numdata.open("pgm8.dat");

    if ( !numdata )                                                       // Check to make sure file was opened
    {
        cout << "Error opening file" << endl;
    }
int i;
while (!numdata().eof)
{
    string test, nn, cc;
    double ww;
    int se, sw;

    numdata>>test;
    cout <<" test= " << test << endl;
    if (test == "D")
    {   numdata>>nn;
        numdata>>cc;
        numdata>>ww;
        dogList[i] = new dog(nn, nn, ww);
    }
    else
    {
        numdata>>nn;
        numdata>>cc;
        numdata>>ww;
        showList[i] = new showDog(nn, cc, ww, se, sw);
    }
}
    numdata.close();

                                                                        //array output
for(i=0;i<NUM_DOGS;i++)
{
   cout <<"dog items: " << dogList[i]->displayDog() << endl;
   cout <<"showdog items: " << showList[i]->displayDog() << endl;
}

Code looks better, but there are still a couple of trouble spots - first, now, in your while loop, you are not incrementing "i" at all! In your assignments to the array, you will overwrite the same item (actually, since "i" is declared, but never initialized, there's some uncertainty there - always initialize your variables!) Second, in your display loop, you are still iterating over 10 elements, when you may not have that many. As you enter each item into the array, increment a counter so you know how many you loaded.

Now, to tackle the virtual stuff. In your code, you are using 2 arrays. The idea is that you can use one array of type "Dog", and put both "Dog" elements and "ShowDog" elements in it, since "ShowDog" is derived from "Dog".

Then, you iterate through the array, calling the "displayDog" method on each, and the correct method gets called for each element, due to declaring the "displayDog" method as virtual in the base class.

Thanks so much, its almost working correctly now. The only thing that is still having problems is my output. I'm not sure what it is, this looks right to me. My compiler is also saying that with a virtual function I need a virtual destructor, how do I right that?

for(i=0;i<count;i++)
{
   cout <<"dog items: " << dogList[i]->displayDog() << endl;
}

A stream object detects eof condition AFTER (but not before) read operation. Now let's suppose that pmd8.dat file is empty (yes, it's unusual but possible case):

ifstream numdata("pgm8.dat");
...
while (!numdata,eof()) { // eof() returns false now
   // Oops! Now you are trying to get NOTHING!
   ...
   numdata >> nn; // No data in nn!
   ...
   dogList[i] = new ... // with unitialized data...
   // You forgot to increment i - yet another error...
   // If it's a global variable - it's a bad practice...
}

Virtual destructor: a destructor is a member function. Didn't you know how to declare virtual member functions?

Yes, I fixed the increment of i after mriscolo's help but my problem now is that this line
cout <<"dog items: " << dogList->displayDog() << endl;

is giving me errors. And yes I need to make my display function virtual however i need a virtual destructor and I believe what I have is correct but I keep getting errors saying that I need a destructor for a virtual function

What sort of syntax error? Also, the class listings for "dog" and "showdog" may help.

>I keep getting errors saying that I need a destructor for a virtual function.
Flat nonsense.
Declare your class destructor as virtual - that's all. It seems you don't understand a concept of polymorphic classes.
http://www.codersource.net/cpp_virtual_destructors.html
There are lots of links on the topic. Search Google. It's annoying to reprint well-known info in response to misunderstanding of obvious compiler message...

>>My compiler is also saying that with a virtual function I need a virtual destructor,
>>how do I right that?
LOL, I just wished my compiler was that smart.

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.