Code Crash Issue

Oct 26, 2021 at 8:32am
My below code crashes at the below highlighted line. How can I fix it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

class Animal
{
   int age;
   float height;

public:
   virtual float Eating();
   virtual void Sleeping();
};

Animal* animal = new Animal();
//This line crashed --v
animal->Eating();

//However I am able to view the instance in watch window as:
//But not able to identify error here.
-> vftbl
   ->0x00000000
   ->Animal::Sleeping (0x04b310a0)
Last edited on Oct 26, 2021 at 9:49am
Oct 26, 2021 at 9:00am
Do you mind showing the code for Animal::Eating?

You also need a virtual destructor on class Animal, but it doesn't make a different in this case.

Also Class Animal, should be class Animal.
Last edited on Oct 26, 2021 at 9:01am
Oct 26, 2021 at 9:51am
It was a type error, have corrected it as class Animal. Do you mean there should be a virtual destructor here? I am not able to figure out what caused the error here.
Oct 26, 2021 at 11:35am
Post real code showing the problem.

Not random snippets taken out of context.
Meaning, if we have to guess at all the possible things you've missed out of your post but got wrong, we're likely to just come up with something which works.

http://cpp.sh/2jh2n
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Example program
#include <iostream>
#include <string>

class Animal
{
   int age;
   float height;

public:
   virtual float Eating();
   virtual void Sleeping();
};

float Animal::Eating() {
    std::cout << "munch\n";
    return 0.0;
}
void Animal::Sleeping() {
    std::cout << "zzzzz\n";
}

int main()
{
  Animal* animal = new Animal();
  animal->Eating();
}

munch
Topic archived. No new replies allowed.