System.AccessViolationException 在 cout << 行

System.AccessViolationException at cout << line

所以我得到了这个 Exception 我知道这只是因为我是 C++ 的新手而且我的代码是错误的(所以,不,这不是一个已经问过的问题)。

我得到了 Frog.cpp 个文件和 program.cpp 个文件。

Frog.cpp:

#include <iostream>
#include <conio.h>
#include "Frog.h"

using namespace std;

Frog::Frog()
{
    (*this).status = Free;
    (*this).color = "Green";
    (*this).weight = 200; // In grams
}
Frog::Frog(float weight, int age, char* color, char* nickname, Status status)
{
    (*this).weight = weight;
    (*this).age = age;
    (*this).color = color;
    (*this).nickname = nickname;
    (*this).status = status;
}
Frog::Frog(float weight, int age)
{
    (*this).weight = weight;
    (*this).age = age;
}
void Frog::currentState()
{
    cout << "Weight:" << (*this).weight << " ,Age:" << (*this).age << " ,Color:" << (*this).color << " , Nickname:" << (*this).nickname << " , Status:" << (*this).status << endl; // The ling that causeing the mayhem
}

Frog.h:

#ifndef FROG_H
#define FROG_H
typedef enum Status { Free, Urban, Plate, Dead };

class Frog
{
    private:
        float weight;
        int age;
        char* color;
        char* nickname;
        Status status;
    public:
        Frog();
        Frog(float weight, int age, char* color, char* nickname, Status status);
        Frog(float weight, int age);
        void currentState();
};

#endif

Program.cpp:

#include <iostream>
#include <conio.h>
#include "Frog.h"

using namespace std;

void main()
{  
    Frog frog = Frog();

    frog.currentState(); // I get the Exception on this line


    getch();
}

异常:

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
       at std.char_traits<char>.length(SByte* _First) in c:\program files (x86)\microsoft visual studio 12.0\vc\include\iosfwd:line 523
       at std.operator<<<struct std::char_traits<char> >(basic_ostream<char\,std::char_traits<char> >* _Ostr, SByte* _Val) in c:\program files (x86)\microsoft visual studio 12.0\vc\include\ostream:line 791
       at Frog.currentState(Frog* )

错误的线路是 cout << "Weight:" << (*this).weight << " ,Age:" << (*this).age << " ,Color:" << (*this).color << " , Nickname:" << (*this).nickname << " , Status:" << (*this).status << endl;Frog.cpp

如有任何建议,我们将不胜感激。

Frog frog = Frog(); 创建一个默认构造的 Frog。您的默认构造函数是

Frog::Frog()
{
    (*this).status = Free;
    (*this).color = "Green";
    (*this).weight = 200; // In grams
}

不初始化 nickname。当您在 currentState() 中打印它时,您正在访问垃圾指针。这是未定义的行为,会导致访问冲突。

我建议你使用 std::string 这样你就不用担心这个了。我还建议您使用 member initialization list。这样你 class 就会看起来像

class Frog
{
    private:
        float weight;
        int age;
        std::string color;
        std::string nickname;
        Status status;
    public:
        Frog() : status(Free), color("Green"), weight(200), age(0), nickname("") {}
        Frog(float weight, int age, std::string color, std::string nickname, Status status) : 
            status(status), color(color), weight(weight), age(age), nickname(nickname) {}
        Frog(float weight, int age);
        void currentState();
};

好吧我就是傻了

正如@jaggedSpire 建议的那样:

我没有初始化 nicknameage 属性。

尽管我在 C++ 中使用默认构造函数时属性会获得默认值。

谢谢大家。