0

I am declaring a global variable, first_clauses:

vector<vector<int>> first_clauses; //clauses for first iteartion

Now, cnf_transformation is one of the functions of the program:

    void cnf_transformation(vector<vector<string>> gates, vector<vector<int>> first_clauses)
    {
        for (int i = 0; i < gates.size(); i++)
        {
            if (gates[i][1] == "AND" || gates[i][1] == "and")
            {
                vector<int> cl;
                cl.push_back(-1 * umap_toInt[gates[i][2] + "_a_1"]);
                cl.push_back(-1 * umap_toInt[gates[i][3] + "_a_1"]);
                cl.push_back(umap_toInt[gates[i][0] + "_a_1"]);
                first_clauses.push_back(cl);
                cl.clear();
                cl.push_back(umap_toInt[gates[i][2] + "_a_1"]);
                cl.push_back(-1 * umap_toInt[gates[i][0] + "_a_1"]);
                first_clauses.push_back(cl);
                cl.clear();
//many more lines are there
}

I want to push some required data into the global variable first_clauses. So, I am calling cnf_transformation and my second argument is first_clauses:

cnf_transformation(gates, first_clauses);

Now, when I print the first_clauses data structure inside my function, it is actually storing data but, after coming out of the function, my global variable first_clauses is not updated. I am still learning C++. Please guide me.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Aadil Hoda
  • 592
  • 7
  • 17
  • Did you mean to pass `first_clauses` by reference? – François Andrieux Feb 19 '20 at 18:24
  • 3
    Please provide [mcve] as text, not image. https://stackoverflow.com/help/how-to-ask – Slava Feb 19 '20 at 18:27
  • Do you mean some mystical global variable `first_clause` or the parameter `first_clauses` that you pass by value, thus you only push to a copy of it and the original remains unchanged? One of your edits showed it as being passed by reference, please clarify. – Lukas-T Feb 19 '20 at 18:39
  • @churill the global variable I declared is not updating, I tried passing by reference also, by reading this post: https://stackoverflow.com/questions/4061128/how-to-pass-2-d-vector-to-a-function-in-c. But passing by reference also not working. – Aadil Hoda Feb 19 '20 at 18:42
  • @FrançoisAndrieux please see my last comment. – Aadil Hoda Feb 19 '20 at 18:44
  • @Slava I replaced the image with text. – Aadil Hoda Feb 19 '20 at 18:45
  • Your code shows neither any global variables, nor any variable named "first_clause". Maybe read it again and check if those are just typos. And maybe clarify on your understanding of global variable, it may not mean what you think it means. – Lukas-T Feb 19 '20 at 18:49
  • @churill I have declared the global variable first_clauses. – Aadil Hoda Feb 19 '20 at 18:56
  • @churill inside the function, the pushing is happening,but its not adding in the actual global variable first_clauses. – Aadil Hoda Feb 19 '20 at 18:57
  • @churill I have printed the first_clause inside the function, and it actually storing the datas, but global variable first_clauses remain unchanged. – Aadil Hoda Feb 19 '20 at 18:58
  • 1
    No, you have _no_ global variable. `first_clauses` (with s at the end, in contrast to your question) is a function parameter and no global variable. And you need to pass it as reference to modify it. – Lukas-T Feb 19 '20 at 19:01
  • Its working now, modified first_clauses.push_back(cl); with ::first_clauses.push_back(cl); – Aadil Hoda Feb 19 '20 at 19:08
  • @churill please see my last comment. Thanks for your concern. – Aadil Hoda Feb 19 '20 at 19:11
  • Good you solved it, but it exposes a design weakness you should adress (global variable are considered bad practice). – Lukas-T Feb 19 '20 at 19:12
  • @Slava Please open this question. I solved it. I will share the answer. It will help others. – Aadil Hoda Feb 19 '20 at 19:21
  • You still did not provide [mcve] so your question is still useless for others. – Slava Feb 19 '20 at 19:25
  • I highly recommend `transform` your text to all upper or all lower case before comparing. As is, you are missing some possible combinations: "And", "ANd", "aNd", etc. – Thomas Matthews Feb 19 '20 at 19:35
  • @Slava I have modified it to be more comprehensible, please review it, and suggest some changes if possible – Aadil Hoda Feb 20 '20 at 12:08

0 Answers0