Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
法院的System.AccessViolationException异常<&书信电报;线 所以我得到了这个代码>异常< /代码>,我知道这只是因为我是C++的新手,我的代码是错误的(所以,不是,它不是一个已经被问过的问题)。p>_C++_Exception_Visual Studio 2013_Header Files_Access Violation - Fatal编程技术网

法院的System.AccessViolationException异常<&书信电报;线 所以我得到了这个代码>异常< /代码>,我知道这只是因为我是C++的新手,我的代码是错误的(所以,不是,它不是一个已经被问过的问题)。p>

法院的System.AccessViolationException异常<&书信电报;线 所以我得到了这个代码>异常< /代码>,我知道这只是因为我是C++的新手,我的代码是错误的(所以,不是,它不是一个已经被问过的问题)。p>,c++,exception,visual-studio-2013,header-files,access-violation,C++,Exception,Visual Studio 2013,Header Files,Access Violation,我得到了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::Fr

我得到了
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
}
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();
}
#包括
#包括
#包括“Frog.h”
使用名称空间std;
void main()
{  
青蛙青蛙=青蛙();
frog.currentState();//我在这一行得到了异常
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* )
未处理的异常:System.AccessViolationException:尝试读取或写入受保护的内存。这通常表示其他内存已损坏。
在c:\program files(x86)\microsoft visual studio 12.0\vc\include\iosfwd:line 523中的std.char\u traits.length(先是SByte*\u)
在标准状态下,操作员
青蛙青蛙=青蛙()创建默认构造的
Frog
。您的默认构造函数是

Frog::Frog()
{
    (*this).status = Free;
    (*this).color = "Green";
    (*this).weight = 200; // In grams
}
它不会初始化昵称
。当您在
currentState()
中打印它时,您正在访问垃圾指针。这是未定义的行为,导致访问冲突

我建议您使用
std::string
,这样您就不必担心这个问题。我也建议你使用。有了这些,你们班看起来就像

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所建议的:

我没有初始化
昵称
年龄
属性

尽管我在
C++
中这样做,但是如果我使用默认构造函数,属性会得到一个默认值


谢谢大家。

OT:您可以编写
foo->bla
而不是
(*foo)。bla
用于任何指针
foo
。(还有一些非指针。)您没有检查昵称是否有效,也没有在默认构造函数中将其设置为默认值。试图打印一个不指向一个有效的C字符串的conchar *是一个不同的坏计划。请注意:使用C++类中的对象初始化类列表(并且替换(*this))@ bUMITAUGEN是C++中的一个指针。@ diutl u ccw是的,但是这对于给定的示例代码是如何相关的?如果没有显式初始化,它们会得到一个默认值。您只是使用c字符串,因此默认值是传递给
std::ostream&operator的无效参数,而不是下行表决器,但请想一想:指针的默认值是什么?它可能不会指向您可以安全打印的任何地方,因此对您有什么用处?成员初始化列表是什么?@good the@good我在答案中添加了一个链接。@NathanOliver好的,我使用了成员初始化列表,得到了
错误函数'Frog::Frog(void)'已经有了一个body
@God,这很可能与头文件和cpp文件中有构造函数有关。如果您在我的示例中看到,它们就在头文件中。在cpp文件中不再需要它们。
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();
};