所以我在上课时遇到问题。在这个类中,我应该拥有一些数据数组。我在分配和创建新数据方面没有问题,例如x2.NewAccount(“123456”,1000);这很好用,问题是当我试图用寻址到某个变量的字符串创建数据时。我对深度复制有所了解,但是在我的案例中我不知道如何编程=运算符+我认为strcpy不能正常工作。
PS:这是一个学校程序,所以请不要因为没有使用 header 和使用Im未在代码中使用的一堆包含项而判断我。它是由我的学校制作的,我不允许更改它们+添加它们(我知道使用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__ */
最佳答案
当使用database[index].accID = accID;
时,您只是在进行浅拷贝(并且依赖于调用方来保持此内存有效,因为可以访问指针)。
您已经正确地确定需要执行深拷贝,但是client::accID
只是一个指针,但是只有在初始化它以指向某些内存之前,您才能将其复制到其中。
一种实现方法是动态分配和管理client::accID
,类似于动态分配和管理client::history
的方式。
代替strcpy (database[index].accID , accID );
或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
对象,您的程序也会遇到 undefined 的行为。即使您不必进行任务分配,也应考虑尝试使用以下方法将其重写:
std::string
代替accID;
的C字符串std::vector
而不是data_history
数组仅供参考:
if (database[i].accID == input )
不会像C弦那样做...