c++ - 类中的 C++ 字符串在构造后变为空
问题描述
我正在创建一个程序,其部分工作是将标记为 Client() 的类对象存储在二叉树中。我在 switch 语句中创建对象
Client newClient = Client (first, last, iD);
transactionsTree.Insert(newClient);
switch 语句处于读取数据的循环中,因此在执行此案例并且程序继续执行后,该类中的字符串将设置为空的“”字符串。我在逐步调试时发现了这一点,并且一旦该案例块执行,字符串就会变为空。放入该类的任何其他数据都会保留,但这些字符串不会。即使我在 Client.h 文件中声明了这些字符串名称,它们在创建它的 switch case 之后仍然是空的。我创建上面显示的 newClient 的代码在 Transactions.cpp 中,transactionsTree 是 BSTree.cpp 的类对象,还有 Client.cpp,所有这些类共享一个连接,但我假设我的问题必须做我如何将对象插入二叉树。
这是带有 switch 语句案例的代码:
case 'O': // open an account
{
string first = list.front();
list.pop();
string last = list.front();
list.pop();
stringstream getiD(list.front()); // transfer string to int
list.pop();
int iD = 0;
getiD >> iD; // transferring string to int
if (transactionsTree.Retrieve(iD)) // if Client is already in tree, prints error message
{
cout << "ERROR: Account " << iD << " is already open. Transaction refused." << endl;
}
else // else creates new Client
{
Client newClient = Client (first, last, iD);
transactionsTree.Insert(newClient);
}
break;
}
这是我的二叉树插入方法:
void BSTree::Insert(Client &newClient)
{
if (isEmpty())
{
Node *newNode = new Node(newClient);
this->root = newNode;
}
else
add(this->root, newClient);
}
BSTree::Node* BSTree::add(Node *node, Client &newClient) // helper function for Insert()
{
if (node == nullptr)
{
Node *newNode = new Node(newClient);
return newNode;
}
if (newClient.clientID < node->pClient->clientID)
node->left = add(node->left, newClient);
else
node->right = add(node->right, newClient);
}
编辑1:经过进一步检查,在标头或构造函数中声明的类中的字符串都不成立,尽管这里是字符串向量。我还有一个字符串数组,整个数组在 Client.cpp 的标头中声明,但是当我尝试从任何 Client 对象中打印出任何字符串时,它只会打印出一个地址。
edit2:我已将我的问题隔离到两个区域,一个我尝试使用以下方法访问树中的客户端:
Client *ptrClient; // create pointer to access the Client once found
ptrClient = &transactionsTree.getClient(iD);
还有两个在我的二叉树类中的 getClient 方法中:
Client& BSTree::getClient(int id) // returns a Client object from the tree to process() in Transactions.cpp
{
return getTheClient(this->root, id);
}
Client& BSTree::getTheClient(Node * node, int iD) // helper function for getClient that returns a Client object in the tree
{
// no need for the if condition of iD not being found because I check if the iD is in the tree before this function is executed
if (node->pClient->clientID == iD)
{
cout << node->pClient->firstName << " PRINTED HERE~~~~~~~~~~~~~" << endl;
return *node->pClient; // return client if found
}
if (iD < node->pClient->clientID)
return getTheClient(node->left, iD);
else
return getTheClient(node->right, iD);
}
此更新信息是否有助于您帮助我解决我的问题?
解决方案
我解决了我的问题,它是两行:
Client newClient = Client (first, last, iD);
transactionsTree.Insert(newClient);
我将其更改为:
Client *newClient = new Client (first, last, iD);
transactionsTree.Insert(*newClient);
这很重要,因为我在堆栈而不是堆中创建了一个新对象。
推荐阅读
- docker - 如何将我的代码安装到容器中,然后运行它?
- jsf - 禁用/启用primefaces keyFilter
- postgresql - Postgres 查询有时需要很长时间
- json - 使用 Helm 创建包含引用元素列表的 json 可解析 k8s 配置映射
- javascript - Node JS 很新,我正在尝试发布特定请求,但它让我出错
- python - Django REST API 身份验证
- sql-server - SQL Server 2012 程序集执行失败 - System.NullReferenceException:对象引用未设置为对象的实例
- python - 边界框选择整个图像而不是多边形
- java - Spring jpa 禁用合并
- javascript - 反应表列顺序