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
Related
I have a main.cpp file, with #include "engine.h" in the beginning.
Inside engine.h there's
#include "myObject.h"
namespace nmspc{
MyObject obj1;
//some code here
}
and myObject.h has:
#include <iostream>
class MyObject{
public:
void helloWorld(){
std::cout << "Hello, World!" << std::endl;
}
}
Why can't I refer have in my main.cpp something like this?:
int main{
nmspc::obj1::helloWorld();
return 0;
}
instead I have to type either nmspc::obj1.helloWorld(), or MyObject::helloWorld(). But I'd like to use the one above.
You can do something like this using static member functions, by adding the keyword static before the method declaration. if you do this, the function will not be associated with an actual instantiation of the class, so cannot access any member variables or non-static member functions.
In general, you wouldn't access static member functions through the created object, but through the class name (although it is possible to call it through an instance).
class MyObject{
public:
static void helloWorld() {
std::cout << "Hello, World!" << std::endl;
}
}
int main{
MyObject::helloWorld();
return 0;
}
Whether or not you should do this is dependant on the context. Your program is simple enough that's its not immediately clear why you would make it a static member function, rather than a free function, but I'm not sure I have enough context to give advice.
1st of all
incorrect header content
#include "myObject.h"
namespace nmspc{
MyObject obj1;
//some code here
}
this should be in a cpp module i.e: a translation unit
as it is an instantiation i.e. a dynamic related thing (run time affect code)
whereas header is the place for all of the static related content
if like to write MyObject::helloWorld()
may have it here
myObject.h :
#include <iostream>
namespace nmspc{
class MyObject{
public:
void helloWorld();
};
MyObject::helloWorld(){
std::cout << "Hello, World!" << std::endl;
}
}
because :: (scope resolution op.) is what name space or class/struct referred to
e.g. nmspc::MyObject::helloWorld
might be helloWorld class inside MyObject class belong to nmspc name space
might be helloWorld class belong to MyObject name space belong to nmspc name space. etc
int main{
MyObject obj1;
//some code here
nmspc::obj1.helloWorld();
return 0;
}
This might me a silly problem. But I am not getting the conceptual problem in the program.
I want to pass the pointer of class Child in a global function and access that pointer object from different parts of the project just by including Child.h
Problem: the following problem does not work when I do get_Child()->number_1 in the main.
Solution: if I include the Child.cpp in the main, and inline the Constructor or
if I declare the constructor in the Child.h instead of Child.cpp
Query request. What is the reason for this behavior? A way how I can declare the Constructor in Child.cpp and does not have to include Child.cpp in the main.cpp
main.cpp
#include <iostream>
#include "Child.h"
//#include "Child.cpp"
using namespace std;
int main(){
Child *pC = new Child();
cout << "direct = " << pC->number_1 << endl;
cout << "with function = " << get_Child()->number_1 << endl;
}
Child.h
#ifndef CHILD_H_
#define CHILD_H_
class Child;
static Child * pointer_Child;
inline void save_Child(Child * p_C){
pointer_Child = p_C;
}
inline Child * get_Child(){
return pointer_Child;
}
class Child {
public:
Child();
//Child(){
// this ->set_Child();
//}
void set_Child(){
save_Child(this);
}
int number_1 = 10;
};
#endif /* CHILD_H_ */
Child.cpp
#include "Child.h"
//inline Child::Child(){
// this ->set_Child();
//}
Child::Child(){
this->set_Child();
}
Do not declare static variables in a header (outside of classes) like this:
static Child * pointer_Child;
It will create a variable in every compilation unit that includes the header. And they are not accessible from outside the translation unit.
Instead, make pointer_Child extern and provide an implementation in the CPP as follows:
Header:
extern Child* pointer_Child;
CPP:
Child* pointer_Child;
And never include a CPP file.
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.
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.
Let's say I have a header file a.h and a source file a.cpp. When I try to compile this and call what() from a different file (e.g. main.cpp) which includes a.h:
a.h:
class A {
friend void what();
public:
int index;
};
a.cpp:
#include "a.h"
#include <iostream>
void what() {
std::cout << "what" << std::endl;
}
It fails, as expected (error: 'what' was not declared in this scope). However, when I do the same thing with this:
a.h:
class A {
friend void what(A *thing);
public:
int index;
};
a.cpp:
#include "a.h"
#include <iostream>
void what(A *thing) {
std::cout << thing->index << std::endl;
}
It compiles and runs just fine on g++ 4.4.1 (assuming "index" has been initialized, of course). I don't know much about C++, but I would assume that, by passing the object pointer to the function, the function somehow became available to the global scope (i.e. gets "promoted" in order to be able to "see" an object which previously existed on the same scope as it). I haven't tried this with other kinds of functions yet, but, when using g++ and friend functions, I get this behavior. Is this supposed to happen in C++?
Thanks and sorry if this is a noobish question, my friend Google was failing me.
I compiled your code with g++ 4.5.1 and no error was reported.
I think you have forgotten ; at the end of your class declaration.
Following code works perfectly fine for me (gcc 4.4.5)
a.h
class A {
friend void what();
private:
int index;
};
a.cpp
#include <iostream>
#include "a.h"
using namespace std;
void what()
{
cout << "what" << endl;
}
int main()
{
what();
return 0;
}
UPDATE
You should declare what function in the a.h header file before A class declaration.