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!
Related
This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 2 years ago.
Is there a compiler option that will allow the following code to compile, without moving the function definition to the bottom?
class first{
int a;
void func(second b){}
};
class second{
int a;
void func(first b){}
};
Nope, not when you pass it by value. If you passed it by pointer or by reference you could.
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;
This question already has answers here:
What are the differences between struct and class in C++?
(30 answers)
Closed 2 years ago.
When writing C++ code, what is the difference between using class and struct?
class Foo final { };
vs.
struct Bar final { };
When should I prefer one over the other? Is this merely a choice of programming/coding style?
A struct simply defines the layout of a piece of storage.
A class is a dynamically-allocated "thing" which can have methods ... it can do things.
This question already has answers here:
Undefined Reference to class static member in static member
(1 answer)
How to initialize private static members in C++?
(18 answers)
Closed 8 years ago.
Class base{
public :
static vector<int> _elems;
...
How can I use that static one.Must I define it out of the class body again?
Or I meet a trouble about a error"Undefine reference to 'base::_elems'"
You've only declared the static member, never defined it. In your cpp file you need to do this:
vector<int> base::_elems;
You can use it like any other variable. You only need to remember that the static variable is the same for all instances.
Edit: I forgot the defenition. You must define the variable, this can be done from any cpp file, but i recommend to define the variable in the file base.cpp.
This question already has answers here:
What does it mean to have an undefined reference to a static member?
(2 answers)
Closed 9 years ago.
I have a .cpp file that looks something like this:
//other code
namespace {
class C1;
class C2;
class C2{
public: static int counter;
//member functions here
};
class C1{
//other code
C2::counter = 10;
};
}
When I run 'make' I get the following error:
relocation R_386_GOTOFF against undefined symbol '(anonymous namespace)::C2::counter' can not be used when making a shared object...
Am I missing something simple here? Shouldn't the static int be available for class C1 to change it? Also, I am developing this as a part of the Clang library's. Also, I can share the Makefile if that helps.
You have missed out on providing the definition of you static variable. This definition must occur outside the class and only one definition is allowed. Usual way to do this is to provide the definition in the implementation file.
Because you are directly using it, without providing any definition for it, you are getting the error.