分段错误(内核转储) C++面向对象编程

Segmentation fault (core dumped) C++ Object Oriented Programming

本文关键字:C++ 面向对象编程 转储 内核 错误 分段      更新时间:2023-10-16

所以,我正在为大学编写一个 c++ 迷你项目。该项目包括制作面向买卖加密货币的软件草案,但是,从昨天开始,我一直遇到问题(专门转储的分段错误核心(......因此,由于此页面对我以前的程序很有帮助,而这次我没有找到可以帮助我的东西,因此我决定注册并询问是否有人愿意帮助我。

#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
class usuario {
private :
string username[10], password[10];
int aux;
public :
usuario();
void setUnamep(string, string, int);
string getUnamep();
void setPass(string);
string getPass();
int DataAcc(string, string);
~usuario();
};
class moneda {  
protected :
float cantidad;
public :
moneda(float);
void setCant(float);
void getCant();
~moneda(); 
};
class bitcoin : public moneda {
private :
float btc[20];
public :
bitcoin (float);
void setBuy(float, float[]);
void getBuy();
void mostrarc(float);
~bitcoin();
}; 

usuario::usuario () {
}
void usuario::setUnamep(string username_, string password_, int aux_) {
string PreUser[20], aux_2;
aux = aux_;
for (int i= 1; i <= aux; i++) {  
username[i] = username_[i];  
password[i] = password_[i];
cout<<"nEnter an username: "; 
cin>>username[i];               
cout<<"Enter a password: ";  
cin>>password[i];       
username[0] = ".";      //pass 1 leer
for (int v = 0 ; v < i; v++) {     
if (username[v] == username[i]) {  
cout<<"nUsername already in use. Choose another"<<endl;
username[i] = "null";
password[i] = "null";
i--;
v = 20000;
}
}
}
}

int usuario::DataAcc(string InUs, string InPass) {
bool ing = false, ret = false;
int u = 0;
do  {
if (InUs==username[u] and InPass==password[u]) {
ing = true;
u = 10;
ret = true;
}
else  //////
u++;
}
while (ing == false and u<5);
if (u == 5)
cout<<"nIncorrect user or password. Try again."<<endl;
if (ing == true) {
cout<<"nAccount data..."<<endl;      
}
return ret;
}
usuario::~usuario() {
}
moneda::moneda(float cantidad_) {
cantidad = cantidad_;
}
moneda::~moneda() {
}                                            
bitcoin::bitcoin(float cantidad_) : moneda(cantidad_) {
}
void bitcoin::setBuy(float cantidad_, float btc_[]) {
int aux;
for (int i = 0; i < 20 ; i++) {
btc[i] = btc_[i];
}
cout<<"How many BTC do you wish to buy?: ";
cin>>cantidad;
btc[aux] = btc[aux] + cantidad;
}
bitcoin::~bitcoin() {   
} 
int main() {
int opc = 0, aux1;
string InUs, InPass;
int aux2 = 0;
bitcoin b1(0);
cout<<"Welcome to BitZuela 2018, down there you have several options for you to choice which one do you want to run. ";
cout<<"nn1. Sign Up."<<endl;
cout<<"2. Log in."<<endl;
cout<<"3. Finish program."<<endl;   
usuario u1; 
while (opc >=0 and opc <=2) {
cout<<"nPress the button of the option you want to run: "; 
cin>>opc;
if (opc==1) {
cout<<"nHow many accounts do you want to register?: ";
cin>>aux1;
u1.setUnamep("null", "null", aux1);
} 
if (opc==2) {
cout<<"nUsername: ";
cin>>InUs;
cout<<"Password: ";
cin>>InPass; 
aux2 = u1.DataAcc(InUs, InPass);
if (aux2 == 1) {
b1.setBuy(0,0);  //The problem is when this object is created
}
}
if (opc == 3)
cout<<"nProgram finished."<<endl;
}       
return 0;
}

就是这样,如果有人能帮助我解决这个问题,我将不胜感激。另外,如果您对另一件事有建议,阅读它将是一种乐趣!

这种方法似乎有一些问题

void bitcoin::setBuy(float cantidad_, float btc_[]) {
int aux;
for (int i = 0; i < 20 ; i++) {
btc[i] = btc_[i];
}
cout<<"How many BTC do you wish to buy?: ";
cin>>cantidad;
btc[aux] = btc[aux] + cantidad;
}

在设置"aux"变量之前使用它,导致未定义的行为。

此外,调用传递的是 0 而不是 float[]。 编译器将 0 解释为 nullptr,导致 ::setBuy 崩溃

if (aux2 == 1) {
b1.setBuy(0,0); 

可能还有其他一些问题,但解决这些问题将是朝着正确方向迈出的一步

你的核心转储是在setBuy函数中。你请求一个浮点数组,但是当你在代码中调用它时,你传递了一个"0",但你应该传递一个包含 20 个元素的数组。

aux 变量是在函数内部设置的,但我认为你应该从函数的签名中传递它。

此外,您在该函数中使用的 cantidad 变量不是签名中的变量(您应该将其从签名中删除,或向 cantidad 添加一个 _(。

我还研究了您的setUnamep函数,您应该使用std::map进行用户名和密码管理(您可以在log(n(中搜索已经存在的键(。