2016-11-11 72 views
-4

我得到一个:“异常在ConsoleApplication18.exe在0x00E8316C抛出:0000005:访问冲突写入位置0xCDCDCDD1。”当我运行我的代码时,这就是它的全部,并且错误会打破我标注评论的地方。感谢您的时间!访问冲突C++的Visual Studio类

#include "iostream" 
using namespace std; 


class Transaction 
{ 
public: 
    int USERID; 
    int Amount; 
    virtual void Report() //modify for each transaction type 
    { 

    } 

    virtual void Perform() //modify for each transaction type 
    { 

    } 

}; 


class Transfer : public Transaction 
{ 
public: 
    int transID; // id of the user to tranfer with 
    int transType; // type of transition; to or fro 

    void Perform() 
    { 
     cout << "\n" << "Transfer" << "\n\n"; 
     cout << "Enter the id of the user to tranfer with: "; 
     cin >> transID; 
     cout << "\n" << "Transfer type:" << "\n" << "1: To 2: Fro" << "\n"; 
     cin >> transType; 
     if (transType == 2) 
     { 
      int temp = transID; 
      transID = USERID; 
      USERID = temp; 
     } 
     cout << "\n" << "Enter the amount you wish to withdraw: "; 
     cin >> Amount; 

    } 

    void Report() 
    { 
     cout << "\n\n" << "Transfer of " << Amount << "$ from user: #" << USERID << " to the user: #" << transID; 
    } 

}; 


class Withdraw : public Transaction 
{ 
public: 

    void Perform() 
    { 
     cout << "\n" << "Withdraw" << "\n\n"; 
     cout << "Enter the amount you wish to withdraw: "; 
     cin >> Amount; 

    } 
    void Report() 
    { 
     cout << "\n\n" << "Withdrawl of: " << Amount << "$"; 
    } 

}; 

class Deposit : public Transaction 
{ 
public: 

    void Perform() 
    { 
     cout << "\n" << "Deposit" << "\n"; 
     cout << "Enter the amount you wish to deposit: "; 
     cin >> Amount; 
    } 
    void Report() 
    { 
     cout << "\n\n" << "Deposit of: " << Amount << "$"; 
    } 

}; 




class User 
{ 
public: 
    int ID; 
    int numTrans; 
    Transaction* t[100]; 
    int TYPE; 

    User() 
    { 
     numTrans = 0; 
    } 

    void AddWithdraw() 
    { 
     t[numTrans] = new Withdraw; 
     numTrans++; 
    } 

    void AddDeposit() 
    { 
     t[numTrans] = new Deposit; 
     numTrans++; 
    } 

    void AddTransfer() 
    { 
     t[numTrans] = new Transfer; 
     numTrans++; 
    } 

}; 


void main() 
{ 
    User*u[100]; 
    int id; 
    int type; 
    int counter; 

    counter = 0; 

    for (int i = 0; i <= 100; i++) 
    { 
     u[i] = new User; 
    } 

    while (true) 
    { 
     type = 0; 

     cout << "\nPlease enter your ID: "; 
     cin >> id; 

     cout << "\n" << "Which transaction would you like to perform" << "\n" << "1: Withdraw" << "\n" << "2: Deposit" << "\n" << "3: Transfer" << "\n"; 
     cin >> type; 

     if (type != 1 && type != 2 && type != 3) 
     { 
      type = 1; 
     } 

     for (int i = 0; i <= counter; i++) 
     { 
      if (u[i]->ID == id || i == counter) 
      { 

       if (i == counter)// Add the type of transaction to the transaction array 
       { 
        if (type == 1) { u[i]->AddWithdraw(); } 
        if (type == 2) { u[i]->AddDeposit(); } 
        if (type == 3) { u[i]->AddTransfer(); } 

       } 

       u[i]->t[u[i]->numTrans]->USERID = id; //BREAKS HERE 
       u[i]->t[u[i]->numTrans]->Perform(); 

       for (int b = 0; b <= u[i]->numTrans; b++) 
       { 

        u[i]->t[b]->Report(); 
       } 
       break; 
      } 
     } 

     // Report all of the previous transactions 
     counter++; 

    } 



} 
+5

在'main'的'u'阵列具有100个元素。你写101个元素。 –

+2

请[编辑]你的问题以提供[mcve]。 –

+1

另外,你的基类'Transaction'缺少一个虚拟析构函数。 – PaulMcKenzie

回答

1

代码中有很多错误。

  • Transaction缺少虚拟析构函数。

  • 你没有做任何边界填写您的数组时检查。

  • 当您的循环需要使用<代替时,您的循环正在使用<=。访问u[i]->t[]阵列时

  • 主循环会出界。

  • 您并未分配ID字段中的每个已分配User,因此您无法通过其id找到用户。

  • 您正在泄漏内存,因为您从未释放任何TransactionUser对象。

尝试一些更喜欢这个:

#include <iostream> 

using namespace std; 

class Transaction 
{ 
public: 
    int USERID; 
    int Amount; 

    virtual Transaction() {} 

    virtual void Report() = 0; 
    virtual void Perform() = 0; 
}; 

class Transfer : public Transaction 
{ 
public: 
    int transID; // id of the user to tranfer with 
    int transType; // type of transition; to or fro 

    void Perform() 
    { 
     cout << "\n" << "Transfer" << "\n\n"; 
     cout << "Enter the id of the user to tranfer with: "; 
     cin >> transID; 
     cout << "\n" << "Transfer type:" << "\n" << "1: To 2: Fro" << "\n"; 
     cin >> transType; 
     if (transType == 2) 
     { 
      int temp = transID; 
      transID = USERID; 
      USERID = temp; 
     } 
     cout << "\n" << "Enter the amount you wish to withdraw: "; 
     cin >> Amount;  
    } 

    void Report() 
    { 
     cout << "\n\n" << "Transfer of " << Amount << "$ from user: #" << USERID << " to the user: #" << transID; 
    }  
}; 

class Withdraw : public Transaction 
{ 
public:  
    void Perform() 
    { 
     cout << "\n" << "Withdraw" << "\n\n"; 
     cout << "Enter the amount you wish to withdraw: "; 
     cin >> Amount; 

    } 

    void Report() 
    { 
     cout << "\n\n" << "Withdrawl of: " << Amount << "$"; 
    }  
}; 

class Deposit : public Transaction 
{ 
public:  
    void Perform() 
    { 
     cout << "\n" << "Deposit" << "\n"; 
     cout << "Enter the amount you wish to deposit: "; 
     cin >> Amount; 
    } 

    void Report() 
    { 
     cout << "\n\n" << "Deposit of: " << Amount << "$"; 
    }  
}; 

class User 
{ 
public: 
    int ID; 
    int numTrans; 
    Transaction* trans[100]; 
    int TYPE; 

    User() 
    { 
     numTrans = 0; 
    } 

    ~User() 
    { 
     for (int i = 0; i < numTrans; ++i) 
      delete trans[i]; 
    } 

    Transaction* AddTransaction(int type) 
    { 
     if (numTrans == 100) 
      return NULL; 

     Transaction *t; 

     switch (type) 
     { 
      case 1: 
       t = new Withdraw; 
       break; 

      case 2: 
       t = new Deposit; 
       break; 

      case 3: 
       t = new Transfer; 
       break; 

      default: 
       return NULL; 
     } 

     trans[numTrans] = t; 
     numTrans++; 

     return t; 
    } 

    Transaction* AddWithdraw() 
    { 
     return AddTransaction(1); 
    } 

    Transaction* AddDeposit() 
    { 
     return AddTransaction(2); 
    } 

    Transaction *AddTransfer() 
    { 
     return AddTransaction(3); 
    } 
}; 

void main() 
{ 
    User* users[100]; 
    int numUsers = 0; 
    int id; 
    int type; 

    for (int i = 0; i < 100; ++i) 
    { 
     users[i] = new User; 
     users[i]->ID = i+1; 
     ++numUsers; 
    } 

    while (true) 
    { 
     cout << "\nPlease enter your ID: "; 
     cin >> id; 

     User *user = NULL; 
     for (int i = 0; i < numUsers; ++i) 
     { 
      if (users[i]->ID == id) 
      { 
       user = users[i]; 
       break; 
      } 
     } 

     if (!user) 
     { 
      cout << "Invalid id!" << "\n"; 
      continue; 
     } 

     type = 0; 

     do 
     { 
      cout << "\n" << "Which transaction would you like to perform" << "\n" << "1: Withdraw" << "\n" << "2: Deposit" << "\n" << "3: Transfer" << "\n" << "4: Quit" << "\n"; 
      cin >> type; 

      if ((type >= 1) && (type <= 4)) 
       break; 

      cout << "Invalid selection!" << "\n"; 
     } 
     while (true); 

     if (type == 4) 
      break; 

     // Add the type of transaction to the transaction array 
     Transaction *trans = user->AddTransaction(type); 
     if (!trans) 
     { 
      cout << "Too many transactions for this id!" << "\n"; 
      continue; 
     } 

     trans->USERID = id; 
     trans->Perform(); 

     for (int b = 0; b < user->numTrans; ++b) 
      user->trans[b]->Report(); 
    } 

    for (int i = 0; i < numUsers; ++i) 
     delete users[i]; 

    return 0; 
} 
+0

@drescherjm:固定 –

+0

谢谢雷米!我会仔细研究一下,有一个简单的原因,为什么:u [i] - > t []离开债券? – korbs

+0

@korbs:数组索引是基于0的,所以't []'的有效索引是'0 ..(numTrans-1)',包括端点在内。直接使用'numTrans'会将数组的末尾+1移到周围的内存中。当他们使用'<='而不是'<'时,你的循环显示相同的错误。 –