c++ - exc_bad_access 错误

标签 c++

我正在为家庭作业编写程序。程序编译运行,但出现bad access错误。

这是main.cpp

#include <iostream>
#include <string>
#include "Mammal.h"
#include "Dog.h"
#include "Horse.h"
#include "Pig.h"
#include "Cat.h"

using namespace std;
//Seed for ease of grading
const int SEED=100;
const int NUM_ANIMALS=5;
const int WEIGHT_LIMIT=150;


void MammalAssignment(const Mammal * new_Mammal, int choice, string newName);
void UserChoice(const Mammal * new_Mammal);
void ListAnimal(const Mammal *new_Mammal);


int main()
{
    string newName, newWeight;

    srand(SEED);
    Mammal *new_Mammal[NUM_ANIMALS];

    UserChoice(*new_Mammal);
    for(int i=0; i<NUM_ANIMALS; i++)
        ListAnimal(new_Mammal[i]);



    //Program pauses for user input to continue
    char exit_char; 
    cout<<"\nPress any key and <enter> to exit\n";
    cin>>exit_char;

    return 0;
}

void UserChoice(const Mammal * new_Mammal)
{
    int choice;
    bool choiceGood;
    string newName;

    for(int i=0;i<NUM_ANIMALS; i++){
        choiceGood=false;
        while(choiceGood==false)
        {   
            cout<<"-Please choose a number 1-4 for the corresponding animal-\n"
                <<"1-Dog\n2-Horse\n3-Pig\n4-Cat\n";

            cin>>choice;  //User choice

            if(choice<=0 || choice >=5){
                cout<<"Your choice is invalid\n\n";
                continue;
            }

            choiceGood=true;
        } //While loop
        cout<<"\nPlease enter a name for the animal you have chosen(Ex. Fido).\n";
        cin>>newName;

        MammalAssignment(&new_Mammal[i], choice, newName);
    }  //For loop

}

void MammalAssignment(const Mammal * new_Mammal, int choice, string newName)
{
    if(choice==1){
        Dog newDog(rand()%(WEIGHT_LIMIT+1), newName);
        new_Mammal=&newDog;
    }
    else if(choice==2){
        Horse newHorse(rand()%(WEIGHT_LIMIT+1), newName);
        new_Mammal=&newHorse;
    }
    else if(choice==3){
        Pig newPig(rand()%(WEIGHT_LIMIT+1), newName);
        new_Mammal=&newPig;
    }
    else if(choice==4){
        Cat newCat(rand()%(WEIGHT_LIMIT+1), newName);
        new_Mammal=&newCat;
    }
}

void ListAnimal(const Mammal *new_Mammal)
{
    cout<<"-------------------------\nName:"
        <<new_Mammal->GetName()<<"\nWeight: "
        <<new_Mammal->GetWeight();
}

哺乳动物.h

#ifndef MAMMAL_H
#define MAMMAL_H

using namespace std;

class Mammal
{
public:
    Mammal(); //Default constructor

    Mammal( int newWeight);  //Parameterized constructor

    void SetWeight(int newWeight);

    virtual string GetName() const;

    int GetWeight() const;
    //virtual function to be  defined by derived animal classes
    virtual void Speak() const;
private:
    int weight;

};

#endif

哺乳动物.cpp

#include <iostream>
#include <string>
#include "Mammal.h"

using namespace std;

Mammal::Mammal()
{
    SetWeight(0);
    cout<<"\nInvoking default Mammal Constructor\n";
}

Mammal::Mammal( int newWeight)
{
    SetWeight(newWeight);
    cout<<"\nInvoking parameterized Mammal Constructor\n";
}

void Mammal::SetWeight(int newWeight)
{
    weight=newWeight;
}

int Mammal::GetWeight() const
{
    return weight;
}

string Mammal::GetName() const
{}

void Mammal::Speak() const
{
    cout<<"\nLadies and gentlemen, the mammal speaks...\n";
}

狗.h

#ifndef DOG_H
#define DOG_H

#include "Mammal.h"

using namespace std;

class Dog: public Mammal
{
public:
    Dog(); //Default constructor

    Dog(const int& newWeight,const string& newName);  //Parameterized constructor

    void SetName(string newName);

    string GetName() const;
    //mammal virtual function 
    virtual void Speak() const;

private:
    string name;
};

#endif

狗.cpp

#include <iostream>
#include <string>
#include "Dog.h"

using namespace std;
//Default constructor
Dog::Dog()
{
    cout<<"\nInvoking default Dog constructor\n";
}
//Parameterized constructor
Dog::Dog( const int& newWeight,const string& newName):Mammal(newWeight)  
{
    SetName(newName);
    cout<<"\nInvoking parameterized Dog constructor.\n";
}

void Dog::SetName(string newName)
{
    name=newName;
}

string Dog::GetName() const
{
    return name;
}
//mammal virtual function 
void Dog::Speak() const
{
    Mammal::Speak();
    cout<<"\nWoof!\n";
}

其他派生类(马、 pig 和猫)都与狗相同。当 ListAnimals() 到达 GetWeight() 时,我收到 Exc_Bad_Access 错误。据我所知,它正在返回正确的文件类型。任何帮助都会很棒

最佳答案

您的 MammalAssignment 函数正在返回一个指向局部变量的指针。一旦函数返回,该内存(在堆栈上)就消失了,当您将它作为相关哺乳动物类型的对象访问时,您将崩溃。

您需要返回一个指向使用 operator new 分配的内存的指针,或者可能只是一个对象而不是指针,假设在您的 Mammal 类中实现了合适的复制语义.

在您进一步学习之前,先对 C++ 中的内存管理进行修订(或初步自学?)。另见 smart pointers ,尽可能避免 new/delete 并让您的生活更轻松。

关于c++ - exc_bad_access 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5873327/

相关文章:

c++ - 如何对齐 Gtkmm 中的按钮?

c++ - 如何在C++中实现纯虚函数

android - 如何使用 cmake 和 Android NDK 在 C++ 中加载线程支持

c++ - "Not declared in this scope"解析公共(public)派生基类模板中的函数模板名称时出错

c++ - 避免在 C++ 中使用默认构造函数进行初始化

C++ NetBeans 错误 : expected unqualified-id before 'int'

c++ - 尝试多态性时出错

C++ 令人困惑的宏

用于桌面和平板电脑开发的 Java 与 C 变体

c++ - 如何检查两个任意内存范围在 c/c++ 中不重叠