Static object of nested class [duplicate] - c++

This question already has answers here:
What is the purpose of the Most Vexing Parse?
(6 answers)
Closed 2 years ago.
I have a Request_manager.h file declaring an object Tickets_Queue as static in Request_manager, which is a nested class from itself.
class Request_manager {
public:
class Tickets_Queue{
private:
pthread_mutex_t m_mutex;
public:
Tickets_Queue(){};
~Tickets_Queue(){};
};
static Tickets_Queue ticket_queue;
private:
static int m_connected;
};
To initialize it, in Request_manager.cpp I write:
int Request_manager::m_connected(0);
Request_manager::Tickets_Queue Request_manager::ticket_queue();
The initialization of m_connected works but for ticket_queue it says:
gcc.archive core/bin/gcc-5.4.0/debug/link-static/threading-multi/libcore.a
gcc.compile.c++ data_interfaces/bin/gcc-5.4.0/debug/link-static/threading-multi/Request_manager.o
data_interfaces/Request_manager.cpp:17:62: error: no ‘dataserver::Request_manager::Tickets_Queue dataserver::Request_manager::ticket_queue()’ member function declared in class ‘dataserver::Request_manager’
Request_manager::Tickets_Queue Request_manager::ticket_queue();

You are calling a function.
Yeah. My mistake. As #john says it is prototyping a function.
You have to declare the type and the variable:
Request_manager::Tickets_Queue Request_manager::ticket_queue;

Related

How to call a class function in c++ without object declaration? [duplicate]

This question already has answers here:
Invoke a c++ class method without a class instance?
(5 answers)
Closed 1 year ago.
I have created a function within a class. How do I call that function in int main() without declaring any object for the class?
You can do it by using a static member function as shown below:
#include <iostream>
class NAME
{
public:
//define a static member function
static void print_st()
{
std::cout<<"static print_st callled"<<std::endl;
}
};
int main()
{
//call static member function without using an object
NAME::print_st();
return 0;
}
What you can do is to write your function inside the class as static. This lets you call the function without declaring an Object

C++ struct member is a reference to class [duplicate]

This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
How to initialize the reference member variable of a class?
(2 answers)
Closed 2 years ago.
can a struct member be a reference to another class?
I have this struct in header file:
struct stop_object {
timetable& tt;
std::string stop_name;
std::set<std::string> platforms;
};
and this class in header file:
using stop_container = std::vector<stop_object>;
class timetable {
public:
stop_container timetable_stops;
platform_container timetable_platforms;
platform_route_container timetable_platform_routes;
route_departure_container timetable_route_departures;
trip_container timetable_trips;
trip_departure_container timetable_trip_departures;
};
I can not build the program. It says: missing type specifier. for timetable& tt;
How can I achieve that I will have the reference to the parent object in which the struct is?
Thanks for help!

Why I can't define a static field in the declaration? [duplicate]

This question already has answers here:
Static constant string (class member)
(11 answers)
How to initialize private static members in C++?
(18 answers)
Closed 2 years ago.
So it looks like I can't do this for some reason...
class MyClass
{
static std::string name = "Whatever";
};
I don't get it, for me it would make sense to define something in the declaration, even if is static, but instead, I have to do this... ¿?¿?¿?
class MyClass
{
static std::string name;
};
std::string MyClass::name = "Whatever";
And by the way... Isn't name private by default? Then why can I change its value from outside the class?
Why I can't define a static field in the declaration?
You can!
#include <string>
class MyClass
{
static inline std::string name = "Whatever";
// ^^^^^^
};
Isn't name private by default? Then why can I change its value from outside the class?
You can't!
You're defining/initialising it, not changing its value.

Scope inside class of C++ [duplicate]

This question already has answers here:
Do class functions/variables have to be declared before being used?
(5 answers)
Closed 5 years ago.
class cl {
public:
cl(int i) { val=i; }
int val;
int double_val() { return val+val; }
};
Variable val is declared after the constructor, which assigns it. But still this code works. Isn't 'val' out of scope for constructor?
The full definition of the class is available to its members. So val is actually declared before the constructor's implementation.

Smart unique pointer as a member variable [duplicate]

This question already has an answer here:
How to initialize a shared_ptr that is a member of a class?
(1 answer)
Closed 8 years ago.
I have a class as:
class LargeObject
{
public:
LargeObject();
void DoSomething();
private:
std::unique_ptr<Thing> pThing;
};
Then when I want to create the pointer in the constructor
LargeObject()
{
pThing(new Thing()); //This does not work.
}
I want to use the member variable throughout the code. How to do that?
I think initialization should be in constructor's initialization list, that's the place where constructors should be invoked from another constructor:
LargeObject()
:pThing(new Thing){}