首页 > 解决方案 > 类中的 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);
}

此更新信息是否有助于您帮助我解决我的问题?

标签: c++stringclassswitch-statementbinary-tree

解决方案


我解决了我的问题,它是两行:

Client newClient = Client (first, last, iD);
transactionsTree.Insert(newClient);

我将其更改为:

Client *newClient = new Client (first, last, iD);
transactionsTree.Insert(*newClient);

这很重要,因为我在堆栈而不是堆中创建了一个新对象。


推荐阅读