1

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?

Merceus
  • 61
  • 1
  • 6

3 Answers3

4

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.

Community
  • 1
  • 1
Ryan Bemrose
  • 9,018
  • 1
  • 41
  • 54
  • avoiding `using namespace std;` in your code all together I don't normally enforce it as being 100% proper code, it is still ill advised to use it, however there are some cases that it is okay if it is within a function definition; but I would suggest to never use it at global scope. – Francis Cugler May 11 '17 at 00:19
1

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

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

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

Paul Hashmi
  • 456
  • 5
  • 18
1

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.
}
Francis Cugler
  • 7,788
  • 2
  • 28
  • 59