简体   繁体   中英

[C++]Variables cross .cpp files

This is a silly question with something that must be an easy answer, but after hours of searching I cannot find the answer. What I need to do is have a pair of .cpp files, say main.cpp and help.cpp that have a variable, vars1 that they share and can both change the value and detect when that value has been changed. The way that would make sense to me is that I would simply declare the variable in a class inside a header file and include that header file in both .cpp files, but that doesn't seem to work.

Here is a copy of my code:

#include <iostream>
#include <fstream>
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "variables1.h"

using namespace std;

int main(){
variables1 vars1;

do {
    cout << "Welcome\n If you need help, type 'yes' now\n";
    cin.getline(vars1.input, 1024);
    if (strcmp(vars1.input, "yes") == 0 || strcmp(vars1.input, "Yes") == 0){
        vars1.helpvar = true;
        cin.get();
    }
    else{
        cout << "Okay then, glad that you know your way around\n";
    }
    cin.clear();
    cout << "What would you like to do?\n";
    cin.getline(vars1.input, 1024);
    if (strcmp(vars1.input, "logon" ) == 0 ) {

    }
} while (0 == 0);
}

help.cpp:

#include <iostream>
#include "variables1.h"

using namespace std;

int help(){
    variables1 vars1;
    do {
        if (vars1.helpvar == true)
            cout << "detecting";
    } while (0 == 0);
}

variables1.h:

class variables1
{
public:
    bool helpvar;
    char input[1024];
};

Actually what you are doing is that for the main file and the help.cpp you are creating two different objects and are setting the helpvar variable for each of them separately. What you want is to have a single object that is used by both help.cpp and main to only modify a single instance of the helpvar variable.

Change your help function to be along the lines of

int help(const variables1& myHelpobject ){
    if (myHelpobject.helpvar == true) {
            cout << "detecting";
    }
}

and then call the function in main as:

help(vars1)

What you were doing before was creating a separate, independent, help object.

Here we are creating the object in main and then passing a reference to it to the function.

The technique to use depends on the purpose of your variable.

If it is some sort of global parameters , that you have to use throughout all your code, the simplest is to define it as a global variable:

main file:

variables1 vars1;  // outside all functions
int main(){
...
}

Either in variables1.h or in the other cpp files using the variable:

extern variables1 vars1;  //outside all functions 

However the code to initialise and maintain these variables in a should also be defined in the class. The constructor shall for example define the values by default, such as if help is enable or disabled.

If your variables are for communicating between different parts of your code, and especially if the main goal of some code is to process the content of these variables, then should better make this clear by passing the variable as parameter (by reference (&) if the communication is bidirectional, or by value).

There are 2 main issues with the code as posted:

int help() is never run

Something needs to call this function for it to run. There isn't anything doing that so regardless of the value of vars1.helpvar you are never going to see "detecting" output.

Consider adding a help.hpp with the definition of the function and call the function from main .

vars1.helpvar is not shared between main and int help()

Currently you have two instances of variables1 and helpvar is a member variable so each instance has a separate copy.

You could either:

  1. Make helpvar a static member of variables1
  2. Share once instance of variables1 between both main and help .

The use of static variables is more likely give design problem later so I'd favour option 2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM