It looks like you have that line in global namespace.
1 2 3 4 5 6 7 8
class QuestsAndAnswers
{
std::string FName; //Now this is a member
public:
QuestsAndAnswers();
void setFName( std::string fname);
std::string getFName();
};
But yeah, you have some really odd mixture of namespace operators. Just stick to std:: for everything. It'll make everything less confusing.
No, you need to declare inside your class, like in my example above. In that example, the f_name variable belongs to the QuestsAndAnswers class.
Your code declares a variable but it doesn't belong to the class. I guess its sort of global to that cpp. Anyway, it's not the way you want to be doing it. :-)
You should declare variables inside your class. If they are just inside the header file, then they will still be in global namespace and accessible to anything that includes that header file.
would that be considered a global variable as it's in the header file for class Example?
No, var would be member data of the class Example, and each Example object would have its own copy of var.
If you want something that is similar to a global variable but part of the class, you can define static member data.
1 2 3 4 5 6 7 8 9 10 11 12 13
#ifndef EXAMPLE_H
#define EXAMPLE_H // mistake in original
#include <string>
class Example
{
public:
static std::string var; // missing semicolon in original
};
#endif // EXAMPLE_H
1 2 3 4 5
// Example.cpp
#include <string>
std::string Example::var; // static member data must be defined externally