1

所以我在上课时遇到问题。在这个类中,我应该有一些数据数组。我对分配和创建新数据没有问题,例如 x2.NewAccount("123456" , 1000); 这很好用,问题是当我尝试使用针对某个变量的字符串创建数据时。我对深度复制有所了解,但我不知道如何在我的情况下编程 = 运算符 + 我认为 strcpy 但这也不起作用。

PS:它是一个学校项目,所以请不要因为我没有使用标题和使用一堆我没有在代码中使用的包含而评判我。它由我的学校制作,我不允许更改它们+添加它们(我知道使用 c++ 中的字符串会容易得多。)。谢谢你的帮助。

#ifndef __PROGTEST__
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cassert>
#include <cctype>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
#endif /* __PROGTEST__ */

struct data_history{
    int money = 0;
    bool Income;
    const char * UnStr;
    const char * to_from;
};

struct client{
    const char * accID;
    int Balance;
    int def_bal;
    data_history * history;
    int in_index = 0;
    int in_cap = 10;

    friend ostream &operator << (ostream &output , client p){
        output << p.accID << ":" << endl << "   " << p.def_bal << endl;
        for (int i = 0 ; i < p.in_index ; i++){
            if (p.history[i].Income == false)
                output << " - " << abs(p.history[i].money) << ", to: " << p.history[i].to_from << ", sign: " << p.history[i].UnStr << endl;
            else
                output << " + " <<abs(p.history[i].money) << ", from: " << p.history[i].to_from << ", sign: " << p.history[i].UnStr << endl;
        }
        output << " = " << p.Balance << endl;
        return output;
    }
};

class CBank
{
public:
    int cap = 10;
    int index = 0;
    client * database;
    ~CBank(){
        for (int i = 0 ; i < index ; i++)
                delete[] database[i].history;
        delete[]database;
    }
    CBank(){
        database = new client[cap];
    }
    bool NewAccount ( const char * accID, int initialBalance ){
        for(int i = 0 ; i < index ; i ++)
            if (accID == database[i].accID) {
                return false;
            }
        //strcpy (database[index].accID , accID );  // Im getting errors while compileing (cuz I was using const char * for database.accID when i chenged it i got program crash. 
        database[index].accID = accID;
        database[index].Balance = initialBalance;
        database[index].def_bal = initialBalance;
        database[index].in_cap = 10;
        database[index].history = new data_history[database[index].in_cap];
        index ++;
        return true;
    }
    client Account (const char * accID ){
        const char * input  =accID;
        for (int i = 0 ; i < index ; i++){
            if (database[i].accID == input )
                return database[i];
        }
        throw "error";
    }
    void print (){
        for (int i = 0 ; i  < index ; i ++) {
            cout << endl;
            cout << i << " = "<< " ID = " << database[i].accID << " | Balance = " << database[i].Balance << endl;
            cout << "===Account history ===\n\n";
            for (int y = 0 ; y < database[i].in_index; y++) {
                cout << "Was it for him? : " << boolalpha << database[i].history[y].Income
                    << "\nHow much : " << database[i].history[y].money << "\nUnique string : "
                    << database[i].history[y].UnStr << "\nfrom/to: " << database[i].history[y].to_from << endl << endl;
            }
        }
    }

private:

};


#ifndef __PROGTEST__
int main ( void )
{
    char accCpy[100], debCpy[100], credCpy[100], signCpy[100];
    CBank x2;
    strncpy ( accCpy, "123456", sizeof ( accCpy ) );
    assert ( x2 . NewAccount ( accCpy, 1000 ) );
    x2 . print();
    cout << "\n\n\n\n";
    strncpy ( accCpy, "987654", sizeof ( accCpy ) );
    assert ( x2 . NewAccount ( "987654", -500 ) );
    x2 . print();
}
#endif /* __PROGTEST__ */
4

1 回答 1

1

使用时,database[index].accID = accID;您只是在做一个浅拷贝(并且依靠调用者来保持这个内存有效,因为指针可能被访问)。

您已经正确地确定您需要执行深层复制,但这client::accID只是一个指针,但您可能不会复制到它,直到您将它初始化为指向一些内存。

一种方法是动态分配和管理,client::accID类似于您动态分配和管理的方式client::history

而不是strcpy (database[index].accID , accID );or database[index].accID = accID;,请尝试:

size_t bufsize = strlen(accID) + 1;
char *buf = new char[bufsize];
memcpy(buf, accID, bufsize);
database[index].accID = buf;

并在析构函数中添加:

delete[] database[i].accID

正如其他人所指出的,这种 C++ 编程风格非常容易出错,并且社区并不看好。使用标准库类可以轻松避免这种手动内存管理。CBank即使进行了上述更改,如果您开始复制对象,您的程序也会遇到未定义的行为。

即使您不必为您的作业这样做,您也应该考虑尝试将其重写为练习:

  • std::string而不是 C 字符串accID;
  • std::vector而不是data_history数组

也仅供参考:

if (database[i].accID == input )

不会用 c-strings 做期望的事情......

于 2020-04-09T19:04:00.877 回答