错误访问冲突写入位置0x00229C20.尝试在控制台中输入字符串时

error access violation writing location 0x00229C20. when trying to input a string in console

本文关键字:控制台 输入 字符串 访问冲突 位置 0x00229C20 错误      更新时间:2023-10-16

我试图在c++代码中输入字符串,当我运行时,我总是得到以下错误:在assignment-1.exe中的0x0F5023F5 (msvcp140 .dll)抛出异常:0xC0000005:访问违规写入位置0x00229C20。我会张贴我的代码下面,如果有人能帮助我,那将是伟大的。请注意,我已经知道,这是一个问题,我试图访问的内存位置上,你没有访问权限,我只是不知道如何解决它。

HEADER FILE:
#ifndef item_H
#define item_h
class item
{
private:
    //attributes
    int itemID;
    char itemName[20];
    float itemcost;
    float itemprice;
    //utility function
    float calcPrice();
public:
    //constructor
    item(int = 000, char[] = "itemUnknown", float = 0,float = 0);
    //destructor
    ~item();
    //set functions
    void setAll(int, char[], float, float);
    void setID(int);
    void setName(char[]);
    void setCost(float);
    //get function
    int getID();
    float getcost();
    float getprice();
    void getname();
    //print function
    void print();
};
#endif
CPP:
#include "Dariush.h"
#include <iostream>
#include <iomanip>
#include<string>
using namespace std;
//constructor will set attributes
item::item(int ID, char n[] , float c,float p)
{

        setID(ID);
        setName(n);
        setCost(c);
        setAll(ID, n, c, p);
}
//destructor will print destroing two objects
item::~item()
{
    cout << "destroing two objects : " << " " << itemName << " "
        << " & " << itemName << endl;
}
//set functions :
void item::setID(int ID)
{
    cout << "please enter the item's ID  :  " << endl;
    cin >> ID;
}
void item::setName(char n[])
{
    cout << "please enter the item's name" << endl;
    cin.ignore();
    cin.getline(n, 20);

}
void item::setCost(float c)
{
    cout << "please enter the item's cost : " << endl;
    cin >> c;
}
void item::setAll(int ID, char n[], float c, float p)
{
    itemID = (ID > 0 && ID < 999) ? ID : 0;
    strcpy_s(itemName, n);
    itemcost = (c > 0) ? c : 0;
    calcPrice();
}
//get functions : 
int item::getID()
{
    return itemID;
}
float item::getcost()
{
    return itemcost;
}
float item::getprice()
{
    return itemprice;
}
void item::getname()
{
    cout << itemName << endl;
}
//print function :
void item::print()
{
    cout << "ID : " << itemID << endl
        << "Name : " << itemName << endl
        << "cost : " << itemcost << endl
        << "price : " << itemprice << endl;
}
// utility function for price callculation : 
float item::calcPrice()
{
    if (itemcost < 1000)
    {
        itemprice = itemcost + (itemcost*0.1);
    }
    else
        itemprice = itemcost + (itemcost*0.2);
    return itemprice;
}
MAIN.CPP:
#include "Dariush.h"
#include <iostream>
#include<string>
using namespace std ;
void main()
{
    item i1;
    item i2;
    i1.print();
    i2.print();
}

感谢您的协助

让我们仔细看看这三个函数声明:

item(int = 000, char[] = "itemUnknown", float = 0,float = 0);
void setAll(int, char[], float, float);
void setName(char[]);

这里的问题是,您声明的字符"array"参数根本不是真正的数组。它们是指针。当声明参数时,例如char n[]实际上被编译器翻译为char *n

构造函数声明使指针指向常量字符串字面值""。常量字符串字面值的重要之处在于它们确实是常量。试图修改字符串字面值会导致未定义行为。并且改变这个文字是您试图在setName函数中使用cin.getline(n, 20)调用所做的。不仅如此,您还告诉cin.getline函数读取超出字符串字面量的内容。

简单的解决方案是将setName读入成员变量itemName

这段代码有很多问题,但是导致访问冲突的一个是:

void item::setName(char n[])
{
    cout << "please enter the item's name" << endl;
    cin.ignore();
    cin.getline(n, 20); //here
}

你应该用cin.getline(itemName, 20);代替。

同样,为了防止将来出现这样的错误,将参数声明为char const n[]而不是char n[]——当您使用带有非const指针的字符串字面量作为参数时,好的编译器应该显示警告。