How can I share variable values between different classes in C++? - c++

I've created a minimal example to share a variable between classes.
In C# normally I do this by creating a public static class with a public static variable... then I can just access it from everywhere.
Main.cpp
#include <iostream>
#include "TestClass.h"
#include "Shared.h"
int main(int argc, char* argv[])
{
std::cout << "This should print 'Hello'" << std::endl;
std::cout << "Print 1 from main: " << MyNameSpace::MESSAGE << std::endl;
std::cout << "This should print 'Test message 1'" << std::endl;
TestClass test("Test message 1");
std::cout << "Print 2 from main: " << MyNameSpace::MESSAGE << std::endl;
std::cout << "This should print 'Test message 2'" << std::endl;
MyNameSpace::MESSAGE = "Test message 2";
std::cout << "Print 3 from main: " << MyNameSpace::MESSAGE << std::endl;
return 0;
}
Shared.h
#pragma once
#include <string>
namespace MyNameSpace
{
static std::string MESSAGE = "Hello";
}
TestClass.h
#pragma once
#include <string>
#include "Shared.h"
#include <iostream>
class TestClass {
public:
TestClass(const std::string& message);
};
TestClass.cpp
#include "TestClass.h"
TestClass::TestClass(const std::string& message)
{
MyNameSpace::MESSAGE = message;
std::cout << "Print 1 from TestClass: " << MyNameSpace::MESSAGE << std::endl;
}
So, if I set any value, for example MyNameSpace::MESSAGE = "Potato" from any file/class, I want to see Potato in MyNameSpace::MESSAGE from all classes
Here is the output so you can see better:
C:\Users\Adrian\RiderProjects\Test\x64\Debug\Test.exe
Hello:
Print 1 from main: Hello
This should print Test message 1:
Print 1 from TestClass: Test message 1
Print 2 from main: Hello
Test message 2:
Print 3 from main: Test message 2
Process finished with exit code 0.

Note that in C++ you can define public static variables in classes, and they will pretty much do what you want.
That said, use of namespaces here is almost irrelevant. It essentially means that there's a :: in the name of your variable (MyNamespace::MESSAGE), but you could alternatively call your variable MyNamespace_MESSAGE, for example.
So the question, independent of namespaces, is how can you have a global variable in C++. One way do to this is to declare the variable in a header file, and define it in a single C++ file. For example:
// message.h
extern std::string message;
// message.cc
std::string message {"Hello"};
(Wrap these in a namespace if you want.) Alternatively, if you just want to put it in a header file, use:
// header.h
inline std::string message{"Hello"};
Outside of a class, static just means static linkage, meaning that you can't access the variable from other translation units. That's not what you want.

Related

C++ endl not printing new line when called from a method

New to C++
My understanding is endl will add a new line. So with the following piece of code:
#include <iostream>
using namespace std;
void printf(string message);
int main()
{
cout << "Hello" << endl;
cout << "World" << endl;
printf("Hello");
printf("World");
return 0;
}
void printf(string message) {
cout << message << endl;
}
I expect the output to be:
Hello
World
Hello
World
But, strangely, the output is:
Hello
World
HelloWorld
Looks like, when called from the user-defined method, endl is not adding new line..??
What is wrong with my understanding here. Please advise.
The problem is that due to overload resolution the built in printf function is selected over your custom defined printf function. This is because the string literal "Hello" and "World" decays to const char* due to type decay and the built in printf function is a better match than your custom defined printf.
To solve this, replace the printf calls with :
printf(std::string("Hello"));
printf(std::string("World"));
In the above statements, we're explicitly using std::string's constructor to create std::string objects from the string literal "Hello" and "World" and then passing those std::string objects by value to your printf function.
Another alternative is to put your custom printf inside a custom namespace. Or you can name your function other than printf itself.
It's using the inbuilt printf method.
Try to explicitly use std::string so that it'll call custom printf method.
printf(std::string("Hello"));
printf(std::string("World"));
Or you can put your method in a different namespace:
#include <iostream>
namespace test
{
extern void printf(const std::string& message);
}
int main()
{
std::cout << "Hello" << std::endl;
std::cout << "World" << std::endl;
test::printf("Hello");
test::printf("World");
return 0;
}
void test::printf(const std::string& message) {
std::cout << message << std::endl;
}
try renaming the "printf" function to "print" it works fine-
#include <iostream>
using namespace std;
void print(string message);
int main()
{
cout << "Hello" << endl;
cout << "World" << endl;
print("Hello");
print("World");
cout <<endl;
return 0;
}
void print(std::string message) {
cout << message << endl;
}
You should pick function name other than printf(); like Print().

Calling a string getter function from a header file

I'm learning C++, and I'm just messing around with putting classes in separate files for practice. I have a getter function, which returns a string (because the variable is saved as a string). However, from my main() function, I am not sure how to call it. I know the problem is probably that I need to include string somewhere when I call the object, but I have no idea how to format it.
I know this is a pretty newbie questions, but I couldn't find the answer anywhere. Could someone help me out?
(p.s. I'm not trying to get this specific code to work, since it's useless. I'm just trying to learn how to apply it for future reference).
I've tried throwing in string in a couple of places when calling or creating the object, but I always get an error. I know I could get around it by not encapsulating the variable or not having a separate class file, but that's not what I want.
main.cpp
#include <iostream>
#include "usernameclass.h"
#include <string>
using namespace std;
int main()
{
usernameclass usernameobject;
usernameobject.getUsername();
return 0;
}
usernameclass.h
#ifndef USERNAMECLASS_H
#define USERNAMECLASS_H
#include <string>
class usernameclass
{
public:
usernameclass();
std::string getUsername();
void setUsername(std::string name);
askUsername();
private:
std::string usernameVar = "test";
};
#endif
usernameclass.cpp
#include "usernameclass.h"
#include <iostream>
#include "username.h"
#include <string>
using namespace std;
string usernameclass::getUsername(){
return usernameVar;
cout << "test cout" << endl;
}
usernameclass::askUsername(){
string name;
cout << "What is your name?" << endl;
cin >> name;
setUsername(name);
cout << "Ah, so your name is "+usernameVar+", great name I guess!" << endl;
cin.get();
cin.get();
cout << "You're about to do some stuff, so get ready!" << endl;
}
usernameclass::usernameclass(){}
void usernameclass::setUsername(string name){
string* nameptr = &usernameVar;
*nameptr = name;
}
Expected result: runs getUsername() function and returns usernameVar
Actual result: doesn't run the getUsername() function
The current code would not compile, because you have not specified return type of 'askUsername()' routine, which is 'void', I believe.
Other things are good, apart from an output in 'getUsername()', which happens after returning from the function and about which you should have received a warning, I guess.
To the question: you can call that 'get' method in 'main()' as:
cout << usernameobject.getUsername();
Your code should be structured more like this instead:
main.cpp
#include <iostream>
#include "usernameclass.h"
int main()
{
usernameclass usernameobject;
// optional:
// usernameobject.askUsername();
// do something with usernameobject.getUsername() as needed...
return 0;
}
usernameclass.h
#ifndef USERNAMECLASS_H
#define USERNAMECLASS_H
#include <string>
class usernameclass
{
public:
std::string getUsername() const;
void setUsername(std::string name);
void askUsername();
private:
std::string usernameVar = "test";
};
#endif
usernameclass.cpp
#include <iostream>
#include "usernameclass.h"
std::string usernameclass::getUsername() const {
return usernameVar;
}
void usernameclass::setUsername(std::string name) {
usernameVar = name;
}
void usernameclass::askUsername() {
std::string name;
std::cout << "What is your name?" << std::endl;
std::getline(std::cin, std::name);
setUsername(name);
std::cout << "Ah, so your name is " << getUsername() << ", great name I guess!" << std::endl;
std::cout << "You're about to do some stuff, so get ready!" << std::endl;
}

Does the stream operator exist for class member functions?

I included the logger from boost. I'm pretty pleased how it works. Just for simplicity and the reason I don't want to use makros to often in my code, I wrap it in a class.
I now wonder if I could use the streaming operator << to write on a member function.
code
class LogWrapper{
...
//debug function
//info function
...
}
void main() {
LogWrapper log;
log.debug() << "some debug msg"; // does this exist?
log.info() << "some info msg";
}
output
[some_timestamp][debug] some debug msg
[some_timestamp][info] some info msg
Is this possible in a good practice, or is it entirely bad style?
It can be done easily like this:
#include <iostream>
class A {
public:
std::ostream &debug() const {
std::cerr << "[timestamp]" << "[DEBUG]";
return std::cerr;
}
};
int main()
{
A a;
a.debug() << "Test";
}
But the important question here is: Should we implement it in this way? In my opinion, NO!
Because you are thinking that the User of the class will print the logs like this:
int main()
{
A a;
a.debug() << "Test" << std::endl;
a.debug() << "Test2" << std::endl;
}
Output:
[timestamp][DEBUG]Test
[timestamp][DEBUG]Test2
But what if User chooses this way:
int main()
{
A a;
auto &out = a.debug();
out << "Test" << std::endl;
out << "Test2" << std::endl;
}
Output:
[timestamp][DEBUG]Test
Test2
I would highly recommend not to return stream object. You should use member functions for this purpose.
#include <iostream>
class A {
public:
static void debug(const std::string &log) {
std::cerr << "[timestamp]" << "[DEBUG]" << log << std::endl;
}
};
int main()
{
A::debug("Test 1");
A::debug("Test 2");
}
Output:
[timestamp][DEBUG]Test 1
[timestamp][DEBUG]Test 2

Requesting a function from another file in a class not working

I have 2 .cpp files called "FactoringProgram.cpp" and "FactoringProgram2nd.cpp", also 1 header file called "FactoringProgram.h". I've already tried searching for this problem on StackOverflow and a couple other sites and haven't found a solution that worked for me. When I try to run this command: "g++ FactoringProgram.cpp FactoringProgram2nd.cpp" I get this error:
FactoringProgram.cpp: In function ‘int main()’: FactoringProgram.cpp:8:11: error: request for member ‘InitialMessage’
in ‘Problem1’, which is of non-class type ‘Factoring()’
Problem1.InitialMessage();
The code for "FactoringProgram.h" is:
#ifndef FactoringProgram_h
#define FactoringProgram_h
#include <stdio.h>
#include <iostream>
class Factoring
{
private:
int m_FirstCoefficent;
char m_FirstOperator;
int m_SecondCoefficent;
char m_SecondOperator;
int m_3rdExpression;
public:
Factoring();
int InitialMessage();
};
#endif
FactoringProgram.cpp code:
#include <stdio.h>
#include <iostream>
#include "FactoringProgramH.h"
int main()
{
Factoring Problem1();
Problem1.InitialMessage();
return 0;
}
FactoringProgram2nd.cpp code:
#include "FactoringProgramH.h"
#include <stdio.h>
#include <iostream>
Factoring::Factoring(int FirstCoefficent=0, char FirstOperator='+',
int SecondCoefficent=1, char SecondOperator='+', int 3rdExpression=1)
: m_FirstCoefficent(FirstCoefficen), m_FirstOperator(FirstOperator),
m_SecondCoefficent(SecondCoefficent), m_SecondOperator(SecondOperator),
m_3rdExpression(3rdExpression);
{
}
int Factoring::InitialMessage()
{
std::cout << "Ok right now your expression is looking like: "
<< FirstCoefficent << "x^2 " << FirstOperator << " " << SecondCoefficent
<< " x" << SecondOperator << " " << 3rdExpression;
}
Your code has several small errors. Here is a version that works (you can compare and see what I had to change). A summary of the changes are:
default parameter values should be in the header file (*.h);
you cannot start variable/argument names with numbers;
int InitialMessage() doesn't return anything, so I changed it to void InitialMessage();
when you initialize fields in a constructor, the last field cannot end with a semi-colon;
fixed the most vexing parsing (per #RSahu comment) in main.cpp.
FactoringProgram.h
#ifndef FactoringProgram_h
#define FactoringProgram_h
#include <stdio.h>
#include <iostream>
class Factoring
{
private:
int m_FirstCoefficent;
char m_FirstOperator;
int m_SecondCoefficent;
char m_SecondOperator;
int m_3rdExpression;
public:
Factoring(int FirstCoefficent = 0, char FirstOperator = '+',
int SecondCoefficent = 1, char SecondOperator = '+', int thirdExpression = 1);
void InitialMessage();
};
#endif
FactoringProgram.cpp
#include "FactoringProgram.h"
Factoring::Factoring(int firstCoefficent, char firstOperator, int SecondCoefficent, char SecondOperator, int thirdExpression) :
m_FirstCoefficent(firstCoefficent),
m_FirstOperator(firstOperator),
m_SecondCoefficent(SecondCoefficent),
m_SecondOperator(SecondOperator),
m_3rdExpression(thirdExpression)
{}
void Factoring::InitialMessage()
{
std::cout << "Ok right now your expression is looking like: "
<< m_FirstCoefficent << "x^2 " << m_FirstOperator << " " << m_SecondCoefficent
<< " x" << m_SecondOperator << " " << m_3rdExpression;
}
main.cpp
#include <stdio.h>
#include <iostream>
#include "FactoringProgram.h"
int main()
{
Factoring Problem1;
Problem1.InitialMessage();
return 0;
}
To this error substitute:
Factoring Problem1();
by:
Factoring Problem1 = Factoring();
The problem is that the compiler is interpreting this line as a function declaration rather than a variable declaration. AS mentioned in the comments, this is known as the most vexing parser problem.
Obs: The code you posted contain many more minor errors.
You should define Factoring() without params and I used headers in FactoringProgram.h
FactoringProgram.h
#include <stdio.h>
#include <iostream>
class Factoring
{
private:
int m_FirstCoefficent;
char m_FirstOperator;
int m_SecondCoefficent;
char m_SecondOperator;
int m_3rdExpression;
public:
Factoring();
Factoring(int,char,int,char,int);
int InitialMessage();
};
#endif
FactoringProgram.cpp
#include "FactoringProgram.h"
int main()
{
Factoring Problem1 = Factoring();
Problem1.InitialMessage();
system("pause");
return 0;
}
FactoringProgram2nd.cpp
#include "FactoringProgram.h"
Factoring::Factoring()
{
*this = Factoring(0, '+', 1, '+', 1);
}
Factoring::Factoring(int FirstCoefficent = 0, char FirstOperator = '+',int SecondCoefficent = 1, char SecondOperator = '+', int _3rdExpression = 1) : m_FirstCoefficent(FirstCoefficent), m_FirstOperator(FirstOperator),m_SecondCoefficent(SecondCoefficent), m_SecondOperator(SecondOperator),m_3rdExpression(_3rdExpression)
{
}
int Factoring::InitialMessage()
{
std::cout << "Ok right now your expression is looking like: "
<< m_FirstCoefficent << "x^2 " << m_FirstOperator << " " << m_SecondCoefficent
<< " x" << m_SecondOperator << " " << m_3rdExpression;
return 0;
}

CLang prog segmentation fault in ((CXXMethodDecl*)(VarDecl->getParentFunctionOrMethod()))

I am compiling my program which uses Clang 3.5 on Ubuntu 13.04 x86 machine. I wanted to get all the variables declared in a cpp project. Here is what I wrote:-
// Declares clang::SyntaxOnlyAction.
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
// Declares llvm::cl::extrahelp.
#include "llvm/Support/CommandLine.h"
#include "clang/AST/DeclCXX.h"
#include <iostream>
#include <vector>
using namespace clang::tooling;
using namespace llvm;
using namespace clang::ast_matchers;
using namespace clang;
using namespace std;
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
static cl::extrahelp MoreHelp("\nMore help text...");
static cl::OptionCategory MyToolCategory("My Tool Category");
internal::Matcher<Decl> fields = fieldDecl().bind("field");
internal::Matcher<Decl> params = parmVarDecl().bind("param");
internal::Matcher<Decl> vars = varDecl().bind("var");
class VarDeclPrinter : public MatchFinder::MatchCallback {
public:
int counter;
virtual void run(const MatchFinder::MatchResult &Result) {
const Decl* dec;
if(const FieldDecl* field = Result.Nodes.getNodeAs<FieldDecl>("field"))
llvm::outs() << "Member Variable: " << field->getName() << "\t\t"
<< field->getParent()->getQualifiedNameAsString() << "\n";
if(const ParmVarDecl* param = Result.Nodes.getNodeAs<ParmVarDecl>("param"))
llvm::outs() << "Argument Variable: " << param->getName() << "\t\t"
<< ((CXXMethodDecl*)(param->getParentFunctionOrMethod()))->getQualifiedNameAsString() << "\n";
if(const VarDecl* var = Result.Nodes.getNodeAs<VarDecl>("var"))
llvm::outs() << "Local Variable: " << var->getName() << "\t\t"
<< ((CXXMethodDecl*)(var->getParentFunctionOrMethod()))->getQualifiedNameAsString() << "\n";
counter++;
//llvm::outs() << "Variable count: " << counter << "\n";
//ASTContext* Context = Result.Context;
}
};
int main(int argc, const char **argv) {
CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
VarDeclPrinter VarDeclCallback;
MatchFinder Finder;
Finder.addMatcher(fields, &VarDeclCallback);
Finder.addMatcher(params, &VarDeclCallback);
Finder.addMatcher(vars, &VarDeclCallback);
return Tool.run(newFrontendActionFactory(&Finder));
}
When I try running this program, I get segmentation fault (possibly when I am casting var->getParentFunctionOrMethod() to CXXMethodDecl*). I wanted the method name where this variable is declared if it is a local variable. Can anybody tell me what may be the reasons for the error? Or what could be a better way to get method name of the variable?
I got the solution. It was because I was trying to fetch method name even if var is not inside method. Here is the solution:-
if(const VarDecl* var = Result.Nodes.getNodeAs<VarDecl>("var")) {
llvm::outs() << "Local Variable: " << var->getName() << "\t\t";
if(var->isFunctionOrMethodVarDecl())
llvm::outs() << ((CXXMethodDecl*)(var->getParentFunctionOrMethod()))->getQualifiedNameAsString();
llvm::outs() << "\n";
}