How to get access to a public static member variable - c++

I have my header file in which i have the "selection" as you can see it's public static member .
#ifndef SHAREDDATA_H_
#define SHAREDDATA_H_
#include "cocos2d.h"
#include "GameTrinkets.h"
using namespace std;
class SharedData{
private:
//...
//Instance of the singleton
static SharedData* m_mySingleton;
public:
//Get instance of singleton
static SharedData* sharedGameManager();
static int selection;
};
#endif /* SHAREDDATA_H_ */
Where i try to get access is:
I tried setting the selection trough just the namespace as it seemed the correct way to do it
I tried by going trough the singleton instance but got unlucky
So the code where i try it
#include "GameScene.h"
#include "GameTrinkets.h"
#include "SharedData.h"
#include <time.h>
void GameScene::mainSpriteSelection(int selection){
//1
SharedData::selection=3;
//2
SharedData::sharedGameManager()->selection=selection;
}
The error i get is :
[armeabi] SharedLibrary : libcocos2dcpp.so
jni/../../Classes/GameScene.cpp:41: error: undefined reference to 'SharedData::selection'

In C++, when you declare a variable to be static you have to redefine it in the implementation file before you can use the variable. For example,
//in the header file.
class foo{
public:
static int bar;
};
//in the implementation file
int foo::bar // optional initialisation.
//in the main program
//header files
int main(){
foo::bar = desired_initialisation_value; //no problem
}

The error messages tells you that you need to define static variable selection
define it in GameScene.cpp file
int GameScene::selection = 0;

You have to define the static variable outside your class (in the .cpp) as
int SharedData::selection;
Minimal example:
#include <iostream>
using namespace std;
struct Foo
{
static int member;
};
int Foo::member; // definition here
int main()
{
cout << Foo::member << endl;
}
Otherwise you get a linker error.

Related

(solved) field has incomplete type of class

I´m building a program with several classes (data structures like stacks, lists,etc).
There is some class (Concesionario) that i need to use in another (ListaE). The class ListaE uses another class called NodoListaE, which uses two pointers, one to the value of the object (Concesionario) and another to the next position of the list (siguiente).
#ifndef NODOLISTAE_HPP
#define NODOLISTAE_HPP
#include "Concesionario.hpp"
class Concesionario;
class ListaE;
class NodoListaE
{
public:
NodoListaE(Concesionario* conc, NodoListaE* sig = NULL);
private:
Concesionario* conc;
NodoListaE* siguiente;
friend class ListaE;
};
typedef NodoListaE* pnodoListaE;
#endif // NODOLISTAE_HPP
#ifndef LISTAE_HPP
#define LISTAE_HPP
#include "NodoListaE.hpp"
#include "Automovil.hpp"
class Automovil;
class NodoListaE;
class ListaE
{
private:
NodoListaE* primero;
public:
ListaE();
void enlistarOrden(Automovil* automovil);
};
#endif // LISTAE_HPP
#ifndef CONCESIONARIO_HPP
#define CONCESIONARIO_HPP
#include <string>
#include "ListaE.hpp"
class ListaE;
class Concesionario
{
public:
Concesionario();
~Concesionario();
std::string mostrar();
void setZona(std::string letra);
void setNum();
int getNum();
private:
int nc=2;
int num_conc;
std::string zona;
int generadorNumsIntervalo(int min, int max);
ListaE automoviles;//ERROR HERE
};
#endif // CONCESIONARIO_HPP
All the cpp files are not implemented (empty constructor and destructor).
The compiler I´m currently using is MINGWx64.
I´ve tried using forward declarations and it worked for the rest of the classes but not for this one.
The program throws the following error in the **Concesionario ** hpp file: include\Concesionario.hpp|22|error: field 'automoviles' has incomplete type 'ListaE'|
Concesionario is implemented in other classes and the program runs perfectly.
Example of another class implementing Concesionario
#ifndef ARBOL_HPP
#define ARBOL_HPP
#include <iostream>
#include "NodoArbol.hpp"
#include "Concesionario.hpp"
using namespace std;
class Arbol {
public:
Arbol();
void Insertar(Concesionario* concesionario);
private:
pnodoArbol raiz;
pnodoArbol actual;
int contador;
int altura;
bool Vacio(pnodoArbol nodo);
};
#endif // ARBOL_HPP
I`ve also tried deleting this class and creating another one from 0 but the error remains.
Any solution to this problem? Thank you very much.

Can't find find out why it doesn't compile [duplicate]

I have a very simple program that doesn't compile due to multiple definition error. It is here:
main.cpp
#include <iostream>
#include "read_p.h"
using namespace std;
int main()
{
return 0;
}
read_p.cpp
#include "read_p.h"
using namespace std;
void read_p()
{
/*
some code here
*/
}
read_p.h
#ifndef READ_P_H
#define READ_P_H
#include "buildings.h"
void read_p();
#endif
buildings.h
#ifndef BUILDINGS_H
#define BUILDINGS_H
#include "flag.h"
using namespace std;
/*
some class here
*/
#endif
flag.h
#ifndef FLAG_H
#define FLAG_H
using namespace std;
class
Test
{
private:
public:
int test_var;
Test(int);
};
Test::Test(int a)
{
test_var = a;
}
#endif
The compiler gives me the error that the constructor Test::Test is defined multiple times. Unlike questions I find online, this error is not due to including the cpp-file instead of the h-file.
Question: Where does the multiple definition of the constructor occur? And is the proper way to circumvent the issue by making the constructur inline?
Change
Test(int);
to
inline Test(int);
Even better, fix your class definition to define member functions inline, which makes them implicitly inline:
class Test
{
public:
int test_var;
Test(int a) : test_var(a) {}
};
Otherwise, as always, defining a function in a header means that it gets defined in every translation unit that includes that header, which leads to multiple definitions.

Member function definition outside the class

Firstly, I am giving the codes. Then I am explaining the problem I am facing.
main.cpp
#include <iostream>
#include "acc.h"
using namespace std;
class mem;
int main()
{
show();
return 0;
}
acc.h
#ifndef ACC_H
#define ACC_H
#include "acc.cpp"
void show();
class mem{
int a;
public:
void showa();
void seta(int A);
};
#endif
acc.cpp
#include <iostream>
using namespace std;
void mem::showa(){cout<<a<<endl;}
void mem::seta(int A){a = A;}
void show()
{
mem m;
m.seta(22);
string ss;
cin>>ss;
cout<<"MY name is "<<ss<<" ";
m.showa();
}
"mem" class I declared in "acc.h" file already and added that "acc.h" into acc.cpp file also. But when I am calling that class from a function. It can't response. Showing "a" and "mem" not declared. How can I perfectly link that class definition and member functions of that class so that calling member functions of that class from another function can't create any problem?
If you remove the #include "acc.cpp" from the acc.h file it should compile without any errors. I tried and it compiles for me. I am using Visual Studio 2010 for the same.
Other than this, few more comments:
You can use #pragma once in you header file instead of #ifndef/#define macros. The former is more cleaner.
You dont need to forward declare class mem before main() as you are already including acc.h.
the show() can be moved to where main() is defined making the acc.h/acc.cppfiles dedicated for the mem class.
A header file should always be named after the class it is holding i.e. mem.h/mem.cpp in your case. This informs which file contains which class even without opening the file.

Nonstatic Member Reference with "std::cout" in Header

I'm sort of new to C++, and I've been making my way through a bit in my own project. I ran into an error with this header and .cpp file
// main.cpp
#include <iostream>
#include "Header.h"
int main() {
MyClass::TestFunction(); //'MyClass::TestFunction': illegal call of non-static member function
}
// header.h
#ifndef HEADER_H
#define HEADER_H
#include <iostream>
class MyClass {
public:
void TestFunction() {
std::cout << "Hello World\n"; //Where I beleive the issue is
}
};
#endif
Now I think the issue comes from std::cout not being static and the declaration in main.cpp needs it to be static, but I'm not sure how to make it static so that main.cpp works correctly. If anyone could give me a tip as to how I can make things like this work later on down the road, that would be awesome :)
the issue comes from std::cout not being static and the declaration in main.cpp needs it to be static
You either have to make your function static OR to intanciate an object of your class and hen call its function :
main.cpp
int main() {
MyClass pony;
pony.TestFunction();
}
OR
header.h
class MyClass {
public:
static void TestFunction() {
std::cout << "Hello World\n";
}
};
whenever a member function is written inside a class, it can be called only using object. You have to create a object in main function.
// main.cpp
#include <iostream>
#include "header.h"
int main() {
MyClass myObject;
myObject.TestFunction(); //'MyClass::TestFunction': illegal call of non-static member function }
OR
If you dont want to use object, then make the member function as static.
// header.h
#ifndef HEADER_H
#define HEADER_H
#include <iostream>
class MyClass { public:
void static TestFunction() {
std::cout << "Hello World\n"; //Where I beleive the issue is
} };
#endif

static Class doesn't show same values between other classes

I hope that I am providing enough information and that I've titled this correctly.
In general, I want to have a class that stores data in my application, and I need several other classes to access the same data. essentially sharing the data between multiple classes.
Short/Concise Code follows:
example.cpp (main application)
// example.cpp : Defines the entry point for the console application.
//
#include "AnotherClass.h"
#include "ObjectClass.h"
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
//Prototype
static void do_example ( );
int main()
{
do_example ( );
}
static void do_example ( ) {
MyObject.a = 5;
cout <<"MyObject.a value set in main application is "<<MyObject.a<<"\n";
AnotherClass m_AnotherClass;
m_AnotherClass.PrintValue();
}
ObjectClass.h
class ObjectClass {
public:
ObjectClass(); // Constructor
int a; // Public variable
} static MyObject;
ObjecClass.cpp
#include "ObjectClass.h"
ObjectClass::ObjectClass() {
a = 0;
}
AnotherClass.h
class AnotherClass {
public:
AnotherClass(); // Constructor
void PrintValue(); // Public function
int value; // Public variable
};
AnotherClass.cpp
#include "AnotherClass.h"
#include "ObjectClass.h"
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
AnotherClass::AnotherClass() {
value = MyObject.a;
}
void AnotherClass::PrintValue() {
cout <<"value in AnotherClass is "<<value<<"\n";
cout <<"it should be the same."<<"\n";
}
But the value is the default value of 0, as if it is a new instance of MyObject. But it should be pulling the value of 5 from the static MyObject.
What am I missing?
A static class instance is a static variable itself. What you expect to happen also makes sense, however, your code does not show how the static instance is handled. In fact, if both MyClassInstances refer to the same object, then you don't even need static declaration.
Also, static variables are defined in cpp files. If you define it in a header, the cpp file (compilation unit) that includes it will define a separate static variable. So, define the static object in the main cpp file and use extern MyStaticClass in the header file. The linker will then link the uses to the same variable.