cout上的System.AccessViolationException<<线

时间:2016-02-11 19:11:26

标签: c++ exception visual-studio-2013 header-files access-violation

所以我得到了这个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

非常感谢任何建议。

2 个答案:

答案 0 :(得分:4)

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 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();
};

答案 1 :(得分:-1)

好的,我只是傻了。

@jaggedSpire建议:

我没有初始化nicknameage属性。

虽然我在C++中,但如果使用默认构造函数,则属性会获得默认值。

谢谢你们。