//this is main.cpp
#include <iostream>
#include "Cook.hpp"
using namespace std;
int main(void){
int l = Cook.get_life();
}
//this is cook.hpp
#ifndef HUNTER_H
#define HUNTER_H
class Cook
{
public:
int get_life(void);
private:
int life;
};
#endif
//this is cook.cpp
#include "Cook.hpp"
int Cook::get_life(void)
{
life=0;
return life;
}
They are all in same folder. And I get compile error when I run main.cpp. And Xcode recommended to use Cook().get_life() instead of Cook.get_life(). Can you explain why? I thought I should use Cook.get_life.
I use Xcode.
First you have to declare variable with type of you class (instance) and then you can use it, But classes has static functions too, that mean you can use function without declare instance of it first but in that you can't use member variable of class, Reading more about concept of classes and more ...
get_life is not a static function, you have to call it on an instance of your class Cook, and that's exactly what Cook() does. If you want to call get_life without an instance of Cook, you should declare your function this way :
static int get_life(void);
And then call it like that :
Cook::get_life();
The thing is you can't use class attributes from static member functions, so instead you need to instantiate your class Cook before calling your member function.
Cook c = Cook(); // Cook().get_life() works to, but you don't keep your newly created object
c.get_life();
Related
I am trying to make functions repository. I have created four files:
Function.hpp, Function.cpp, FunctionsRepository.hpp, FunctionsRepository.cpp
I want to keep pointers to functions in vector of pointers.
//FunctionsRepository.hpp
#ifndef FUNCTIONSREPOSITORY_HPP
#define FUNCTIONSREPOSITORY_HPP
#include <vector>
using namespace std;
class FunctionsRepository {
private:
static vector<double *> pointerToFunctions;
public:
static void addFunction(double * wsk);
};
#endif
//FunctionRepository.cpp
#include "FunctionsRepository.hpp"
void FunctionsRepository::addFunction(double * wsk) {
pointerToFunctions.push_back(wsk);
}
//Functions.hpp
#ifndef FUNCTIONS_HPP
#define FUNCTOINS_HPP
#include "FunctionsRepository.hpp"
int constFunction(int numberOfVehicles);
void linearFunction();
void stepFunction();
#endif
//Funcctions.cpp
#include "Functions.hpp"
double constFunction(double numberOfVehicles){
return numberOfVehicles/2;
}
double (*funcConstant)(double) = constFunction;
//ERROR HERE
FunctionsRepository::addFunction(funcConstant);
I want to add new functions to program as easily as its possible and use it leater in other parts of program.
But I dont get it. Why i am getting this error. The addFunction() method is static, that means I can use it in other classes or parts of program. Vector is static to make sure that is the only one copy for whole program.
Use function wrapper. std::function can stores callable objects. So, your code will contain something like this:
class FunctionsRepository {
private:
// void() - function prototype
static std::vector<std::function<void()>> pointerToFunctions;
public:
static void addFunction(std::function<void()> wsk)
{
pointerToFunctions.push_back(wsk);
}
};
for more information consult official documentation: http://en.cppreference.com/w/cpp/utility/functional/function
I solved It. I received an error because I was calling the FunctionsRepository::addFunction(funcConstant); expression out of any scope. I just created new function to execute this command and thats all.
I have multiple classes in my program.
A) When I create an object of a class in another class I am getting no error but when I use the object to call a function I get the above error.
B)Also if I create an object of another class and call a function using that in the constructor of my class then I get no error like this.
C) Cout function does not work in the body of the class except when I put it any function
D) The main class is able to do all of these and I am not getting any error.
It would be great to hear back soon. Thank you in advance.
Following is the code : These are two classes in my cpp. I am facing no problems except using object after creating it. the code is too huge too be posted. Everything can be done in main but not in other classes why?
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <vector>
#include <map>
using namespace std;
class Message
{
public:
void check(string side)
{
if(side!="B"&&side!="S")
{
cout<<"Side should be either Buy (B) or Sell (S)"<<endl;;
}
}
};
class Orderbook
{
public:
string side;
Orderbook() //No Error if I define inside constructor
Message m; //No Error while declaring
m.check(side); //Error when I write m. or m->
};
This is a mistake:
m.check(side);
That code has to go inside a function.
Your class definition can only contain declarations and functions.
Classes don't "run", they provide a blueprint for how to make an object.
The line Message m; means that an Orderbook will contain Message called m, if you later create an Orderbook.
Calling m.check(side), meaning you are running actual code, but you can't run code outside main() - you can only define variables.
In C++, code can only appear inside function bodies or in variable initializes.
You can declare an object of a class in another Class,that's possible but you cant initialize that object. For that you need to do something like this :-->
(inside main)
Orderbook o1;
o1.m.check(side)
but that would be unnecessary. Keeping things short :-
You can't call functions inside a Class
I have multiple classes in my program.
A) When I create an object of a class in another class I am getting no error but when I use the object to call a function I get the above error.
B)Also if I create an object of another class and call a function using that in the constructor of my class then I get no error like this.
C) Cout function does not work in the body of the class except when I put it any function
D) The main class is able to do all of these and I am not getting any error.
It would be great to hear back soon. Thank you in advance.
Following is the code : These are two classes in my cpp. I am facing no problems except using object after creating it. the code is too huge too be posted. Everything can be done in main but not in other classes why?
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <vector>
#include <map>
using namespace std;
class Message
{
public:
void check(string side)
{
if(side!="B"&&side!="S")
{
cout<<"Side should be either Buy (B) or Sell (S)"<<endl;;
}
}
};
class Orderbook
{
public:
string side;
Orderbook() //No Error if I define inside constructor
Message m; //No Error while declaring
m.check(side); //Error when I write m. or m->
};
This is a mistake:
m.check(side);
That code has to go inside a function.
Your class definition can only contain declarations and functions.
Classes don't "run", they provide a blueprint for how to make an object.
The line Message m; means that an Orderbook will contain Message called m, if you later create an Orderbook.
Calling m.check(side), meaning you are running actual code, but you can't run code outside main() - you can only define variables.
In C++, code can only appear inside function bodies or in variable initializes.
You can declare an object of a class in another Class,that's possible but you cant initialize that object. For that you need to do something like this :-->
(inside main)
Orderbook o1;
o1.m.check(side)
but that would be unnecessary. Keeping things short :-
You can't call functions inside a Class
I would like to know if there was a way to declare a class before a function, and then initialize it inside of a function, something like this:
Application.h:
class Application
{
Application(HWND hwnd);
~Application() {}
};
Main.cpp:
#include "Application.h"
#include <Windows.h>
Application App;
int main()
{
App(hwnd);
return 0;
}
Application *pApp;
int main()
{
pApp = new Application(hwnd);
//use pApp
delete pApp;
return 0;
}
Using a pointer is pretty much the only way to do what you want to do.
You cannot initialize a global object inside a function, the constructor of the object will be called some time before the main function of the program is called. This is very bad, it involves the thing called static initialization fiasco and you want to avoid it. Try to search for singleton pattern implementation in C++, that's what you need probably.
In C++ the constructor for an object is called when the storage for it is allocated, you cannot call the constructor later like you tried to do.
You might consider not defining a constructor, and using a separate member function, for example init, to initialize your App object.
Application.h:
class Application
{
public:
void init(HWND hwnd);
};
Main.cpp:
#include "Application.h"
#include <Windows.h>
Application App;
int main()
{
App.init(hwnd);
return 0;
}
Can someone explain why the following code does not work? I cannot find any resources explaining the how namespaces, classes and identifiers fit together. When you do my_class::my_member, the my_class:: part is not a namespace? What is it?
#include <iostream>
class my_class {
public:
static void my_member() {
std::cout << "worked" << std::endl;
}
};
int main() {
using namespace my_class; // error: 'my_class' is not a namespace-name
my_member(); // error: 'my_member' was not declared in this scope
my_class::my_member(); // works
}
As a more general question: is there a way I can reference static class members without doing the my_class:: namespace/ identifier/ whatever each time?
Instead of
my_class::my_member_1
my_class::my_member_2
I just want
my_member_1
my_member_2
Is this possible? Thank you.
Is this possible?
Yes, indirectly. If you create a method that operates in my_class's scope, then you can get the behavior you want.
#include <iostream>
class my_class {
public:
static void my_member() {
std::cout << "worked" << std::endl;
}
static int my_main();
};
int my_class::my_main() {
my_member(); // no error
my_class::my_member(); // works too
return EXIT_SUCCESS;
}
int main() {
my_class::my_main();
}
my_class is not a namespace, it is a class name (a type). Therefore, you cannot use using namespace with my_class.
If you want to use my_member_1 without prefixing the class name, create a global wrapper function.
void my_member_1() {
my_class::my_member_1();
}
When you call a static function like this :
my_class::my_member();
You are refering to the class definition to find the static function. A static function will be the same for every instance of your class. You cannot access the static function either by simply calling the function name, or by creating an instance of the class to call the static function.
If you want to call directly the static function without writing down the class definition, you could do something like :
#define my_member_1 my_class::my_member_1()
#define my_member_2 my_class::my_member_2()
...
After that you could simply call my_member_1 to execute your static function, but that could get confusing on a large scale program. My advice is to keep using the class definition so you know exactly what function you are calling.