-3

所以我得到了这个Exception,我知道这只是因为我是新来的C++和我的代码是错误的(所以,不,这不是一个已经问过的问题)。在cout上的System.AccessViolationException << line

我得到了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

任何建议将不胜感激。

+0

OT:对于任何指针“foo”,您都可以写'foo-> bla'而不是'(* foo).bla'。 (也有几个非指针)。 –

+0

你不检查的绰号是有效的,并没有将其设置为默认的构造函数的默认值。试图打印一个不指向有效c字符串的const char *是一个不同的错误计划。 – jaggedSpire

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

什么是成员初始化列表? – God

+0

@God的[C++超级FAQ对主题的几个条目(https://isocpp.org/wiki/faq/ctors#init-lists) – jaggedSpire

+1

@God我添加了一个链接到了答案。 – NathanOliver

-1

好吧我只是愚蠢的。

为@jaggedSpire建议:

我没有初始化nicknameage性能。

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

谢谢你们。

+0

他们确实是会获得一个默认值,如果没有明确的初始化。你只是使用C字符串,所以默认值是传递给'std :: ostream&operator <<(std :: ostream&,const char *)'的无效参数。 NathanOliver是给予很好的建议:用'的std :: string'和初始化列表 – jaggedSpire

+0

不是downvoter,但对此事的看法:什么将是一个指针和默认值有什么用这将是你,因为它可能不会指向任何可以安全打印的地方? – user4581301

+0

@ user4581301你绝对正确。 – God

相关问题