简体   繁体   中英

How to use a member variable in a member function which is defined in a separate file

userInput.hpp

#include <string>

class UserInput{
public:

    std::string rawInput;
    std::string parseUserInput;

}; 

userInput.cpp

#include <iostream>

#include "userInput.hpp"
#include "stringManipulation.hpp"

using namespace std;

string branchCommand;

string parseUserInput(){

    removeWhiteSpaces(rawInput);

    return branchCommand;
}

I have created a class in userInput.hpp with the member function parseUserInput and I mean to define that function in userInput.cpp. However, when I try to use rawInput inside the definition, I cannot without making it so that rawInput is declared as static .

Is it possible to use the rawInput string in the function's definition which is in another file without making the rawInput variable static?

First of all, you've declared parseUserInput as a string field in your hpp, not as a function. Declare it as a member function by using paretheses.

std::string parseUserInput();

Second, in userInput.cpp you are defining a global function named parseUserInput() , not a member function. To define the member function on the UserInput class, use the scope resolution operator ::

std::string UserInput::parseUserInput() {
    ...
}

Finally, you should avoid using namespace std; in your code.

Hi have you thought about making a get function...

std::string returnRawInput() {return rawInput;}

you can then call this function to get the input...

Your current class looks like this:

userInput.hpp

// Missing Header Guards Or #Pragma Once
#include <string>

class UserInput{
public:    
    std::string rawInput;        // Member Variable of UserInput      
    std::string parseUserInput;  // Member Variable of UserInput   
};

userInput.cpp

#include <iostream>                // Okay    
#include "userInput.hpp"           // Okay
#include "stringManipulation.hpp"  // Okay

using namespace std;   // Bad Practice

string branchCommand;  // non const local Global Variable - can be bad practice 

// Looks like a global function that is not a part of your class
string parseUserInput(){

    removeWhiteSpaces(rawInput);

    return branchCommand;   // returning global variable that isn't used?
}

Did you mean?

userInput.hpp

#ifndef USER_INPUT_HPP
#define USER_INPUT_HPP

#include <string>

class UserInput {
public:
    std::string rawInput; // Member Variable
    std::string parseUserInput(); // Now a Member Function that Returns a std::string
};

#endif // USER_INPUT_HPP

userInput.cpp

#include <iostream>
#include "userInput.hpp"
#include "stringManipulation.hpp"

std::string branchCommand; // Still a bad idea if not const or static

std::string UserInput::parseUserInput( /* should this take a string from the user? */ ) {
    removeWhiteSpaces( rawInput );

    return branchCommand; // ? ... Still not sure what you are returning.
}

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