获取munmap_chunk():在函数中使用指针和类时出现无效的指针错误

时间:2019-04-09 18:33:12

标签: c++ pointers project

我编写了一个函数,该函数接受用户输入的字符串并在数组中搜索该字符串,然后使用该数组的索引执行一些操作。我不断收到错误,我不知道为什么。我尝试使用调试器,但是我不断收到错误消息,指出它没有使用调试符号进行编译。我的教授对我毫无帮助,当我习惯了CodeBlocks时,大学强迫我们在虚拟机上使用Atom。

#include <iostream>
#include <iomanip>
#include <fstream>
#include "Shareholder.h"
#include "Share.h"

using namespace std;

int numberOfLines();
Share *loadData(int size);
void shareholderData(Shareholder &sh);
void displayData(Share *myShare, int num);
void purchaseShares(Share *myShare, Shareholder &sh, int num);
void displayShareholder(Shareholder &sh);
void balanceAdjust(Shareholder &sh);

int main(){

  int amount = numberOfLines();
  Share *shares = loadData(amount);
  displayData(shares, amount);
  Shareholder shareholder1;
  shareholderData(shareholder1);
  purchaseShares(shares, shareholder1, amount);
  delete shares;
  shares = 0;
  displayShareholder(shareholder1);
  balanceAdjust(shareholder1);

  exit(0);
}

//The fucntion i think is causing the issue

void purchaseShares(Share *myShare, Shareholder &sh, int num){

  bool valid = false;
  string comBought;
  int sharesBought;
  int match;
  double invested, balance;

  do{
    cout << endl;
    cout << setw(5) << "" << "Which company would you like to buy shares from? ";
    cin.ignore();
    getline(cin, comBought);
    cout << setw(5) << "" << "How many shares would you like to buy? ";
    cin >> sharesBought;
    cout << endl;

    for(int i = 0; i < num; i++){
    string target = myShare[i].getCompanyName();
    if(comBought == target){
        match = i;
        valid = true;
    }
    }
    if (valid == false){
    cout << setw(5) << "" << "Error! Invalid company! Please enter compnay from list.";
    }
  }while(valid == false);

  invested = myShare[match].getPricePerShare() * sharesBought;
  balance = sh.getAvailableBalance() - invested;

  sh.setAmmountInvested(invested);
  sh.setAvailableBalance(balance);
  sh.setNumberofShare(sharesBought);
  sh.setInvestmentCompany(myShare[match].getCompanyName());

}

0 个答案:

没有答案