为什么我的程序在接受用户输入后会因访问冲突而崩溃? 我在用Visual C++ 2008 Express版破坏我的程序时得到这个奇怪的错误: 'Ex2.exe': Loaded 'D:\studyMA\c++\visual studio\Ex2\Ex2\Debug\Ex2.exe', Symbols loaded. 'Ex2.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll' 'Ex2.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll' 'Ex2.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcp90d.dll' 'Ex2.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcr90d.dll' First-chance exception at 0x1049615e in Ex2.exe: 0xC0000005: Access violation reading location 0x333ee91c. Unhandled exception at 0x1049615e in Ex2.exe: 0xC0000005: Access violation reading location 0x333ee91c. The program '[948] Ex2.exe: Native' has exited with code 0 (0x0).

为什么我的程序在接受用户输入后会因访问冲突而崩溃? 我在用Visual C++ 2008 Express版破坏我的程序时得到这个奇怪的错误: 'Ex2.exe': Loaded 'D:\studyMA\c++\visual studio\Ex2\Ex2\Debug\Ex2.exe', Symbols loaded. 'Ex2.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll' 'Ex2.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll' 'Ex2.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcp90d.dll' 'Ex2.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcr90d.dll' First-chance exception at 0x1049615e in Ex2.exe: 0xC0000005: Access violation reading location 0x333ee91c. Unhandled exception at 0x1049615e in Ex2.exe: 0xC0000005: Access violation reading location 0x333ee91c. The program '[948] Ex2.exe: Native' has exited with code 0 (0x0).,c++,error-handling,C++,Error Handling,类产品如下所示: using namespace std; class product { public: string Product_code; string Product_Name; string Product_Category; string Product_Vendor; string Product_Discription; string Product_Group; void input();

产品
如下所示:

 using namespace std; 

 class product
 { 
 public: 
     string Product_code;
     string Product_Name;
     string Product_Category;
     string Product_Vendor;
     string Product_Discription;
     string Product_Group;

    void input();
    void clear_product_cell();
 };
 product::input()

     do{
        cout << "Please enter product code: ";
        getline(cin,Product_code);
        if (Product_code.empty())
            cout << "You Must Enter A Code!!!";
    } while(Product_code.empty());
    cout << endl;
我还有一个类(包含在bomba.h中),它包含:

     product ProductList[100];
我已经在bomba.cpp中编写了代码,要求用户输入:

 char ch;
 cout << "Please Select your option: ";
 cin >> ch;

 switch(ch)
   {
     case '1'://Add a product
     {
        ProductList[0].input();
             :
             :
        :
charch;
cout>ch;
开关(ch)
{
案例“1”://添加产品
{
ProductList[0]。输入();
:
:
:
product.cpp如下所示:

 using namespace std; 

 class product
 { 
 public: 
     string Product_code;
     string Product_Name;
     string Product_Category;
     string Product_Vendor;
     string Product_Discription;
     string Product_Group;

    void input();
    void clear_product_cell();
 };
 product::input()

     do{
        cout << "Please enter product code: ";
        getline(cin,Product_code);
        if (Product_code.empty())
            cout << "You Must Enter A Code!!!";
    } while(Product_code.empty());
    cout << endl;
product::input()
做{

cout0xC0000005或访问冲突表示您试图访问不属于进程的内存。这通常意味着您没有分配内存


不幸的是,您提供的代码太少,无法说明哪里出了问题,但我的猜测是Product_代码未初始化。

没有任何信息可以继续(“Product_代码是类Product中的成员变量”没有多大帮助),当您使用.empty()访问它时,我猜Product_代码为null,这导致访问冲突。(我是根据“0xC0000005”错误代码猜测的,但这只是猜测。)

cin>>Product\u code;
之后,Product\u code是什么样子的?可能是
Product\u code.empty()
正在调用一个不存在的函数,因为它是空的

我怀疑这是你的整个程序。不管怎样,如果你在调试模式下编译,调试器应该会告诉你事情的进展。你试过在调试器中运行它吗?根据给出的信息,你甚至不去你的代码。在三行代码中,我看到三个输入错误,你没有得到正确的缩进,在最重要的变量类型都不清楚的地方粘贴不完整的代码,甚至没有提到错误是发生在输入之前还是之后——这是给定代码段中最突出的一点。毕竟,你“忘记”几乎不再重要了检查输入内容是否成功。老实说,这是我这几天看到的最糟糕的帖子之一。我很少否决投票问题,但这一个确实值得。好了,伙计们。类product看起来是这样的:#包含#包含使用名称空间std;类产品{public:string Product_code;string Product_Name;string Product_Category;string Product_Vendor;string Product_description;string Product_Group;void input();void clear_Product_cell();};#endif/*Product_H_*/我还有一个类(bomba.H),其中包含:Product ProductList[100];当我在bomba.cpp ProductList[0]中写入时。input();我输入上面提到的product.cpp:void product::input()。当我在控制台中键入代码时会发生错误。Null?可能不会,除非产品代码应该是800 MB大。“800 MB大”?你从哪里得到的?我在回答中的任何地方都没有提到800 MB大小,OP在问题中也没有提到。(除非您试图使用“0xC0000005”作为大小,但事实并非如此;这是一个错误代码。谷歌可能是您的朋友。)@sbi:很好,除了这是一个内存地址,而不是任何类型的大小。仍然没有解释RaphaelSP的评论。Product_代码成员可以很容易地被分配一个内存地址0x333ee91c,但该内存地址从未初始化。访问它将导致访问空值,导致访问冲突。顺便说一句,您可以通过查看错误消息本身“location 0x333ee91c”来辨别它是内存地址,而不是大小说明符。注意“location”这个词。@Ken:我只是想指出RaphaelSP的800MB可能来自哪里,不一定是为了保护他的位置。请不要开枪射击messenger!
:)
反正我解决了问题tnx