This question already has answers here:
Undefined reference to static class member
(9 answers)
how to initialize and use static struct [duplicate]
(2 answers)
Closed 2 years ago.
I have a class that has a static member struct
class SharedMem
{
public:
struct memory {
char buff[100];
int status, pid1, pid2;
};
static struct memory* shmptr;
}
I would like to define the static struct using
SharedMem::memory shmptr;
But I'm getting errors undefined reference to 'SharedMem::shmptr'
How do I properly define the struct in C++?
And follow up question, how can I define this struct if my class is entirely in the header file, can I define it after the class declaration at the bottom of the header file?
Thanks
class SharedMem
{
public:
struct memory {
char buff[100];
int status, pid1, pid2;
};
static memory* shmptr;
};
// must add this in the cpp file!
SharedMem::memory* SharedMem::shmptr = nullptr;
Related
This question already has answers here:
Undefined reference to a static member
(5 answers)
Closed 1 year ago.
This is a MSVP of a problem I am facing.
What is wrong with m_eventQueue being static in the code below ?
When it is not static, it compiles fine. I want it to be static because I am planning to use it in another class as well and it is a common queue between them.
This is the error I get
badri#badri-All-Series:~/progs$ g++ --std=c++11 inher3.cpp
/tmp/ccGTVi2d.o: In function `RecordingConfigJobStateSignal::RecordingConfigJobStateSignal(std::shared_ptr<EventQueue> const&)':
inher3.cpp:(.text+0x13c): undefined reference to `commonQueue::m_eventQueue'
collect2: error: ld returned 1 exit status
This is inher3.hpp
#include<iostream>
#include<memory>
#include<queue>
using namespace std;
class EventBase
{
public:
private:
int a;
};
using EventBasePtr = std::shared_ptr<EventBase>;
class SubscriptionManager
{
public:
int x;
};
class EventQueue
{
public:
explicit EventQueue( SubscriptionManager& ){};
~EventQueue();
private:
std::queue< EventBasePtr > m_queue;
};
using EventQueuePtr = std::shared_ptr<EventQueue>;
class commonQueue
{
public:
int *a;
static std::queue< EventBasePtr > m_queue;
static EventQueuePtr m_eventQueue;
};
class RecordingConfigJobStateSignal: public commonQueue
{
public:
int c;
RecordingConfigJobStateSignal( const EventQueuePtr &);
private:
int b;
};
This is inher3.cpp
#include<iostream>
#include"inher3.hpp"
RecordingConfigJobStateSignal::RecordingConfigJobStateSignal( const EventQueuePtr& eventQueue )//:m_eventQueue( eventQueue )
{
/* m_eventQueue is actually from class commonQueue */
m_eventQueue = eventQueue;
}
int main()
{
return 0;
}
When you declare a static member in a class, C++ requires you to define the member outside of the class explicitly. Put this outside of your class in the .cpp file:
/*static*/
EventQueuePtr commonQueue::m_eventQueue;
This question already has answers here:
What is object slicing?
(18 answers)
Access child members within parent class when children are inside a vector of type parent
(1 answer)
Closed 1 year ago.
I put some different structs that inherits from the same parent struct but when I try to access their members, compiler gives me an error. The code is this:
#include <bits/stdc++.h>
using namespace std;
struct A
{
int a=0;
};
struct B:A
{
int b=0;
};
struct C:A
{
int c=0;
};
int main()
{
vector<A> va;
B bb;
C cc;
va.push_back(bb);
va.push_back(cc);
cout<<va[0].b;
return 0;
}
The error is this:
'__gnu_cxx::__alloc_traits<std::allocator<A> >::value_type {aka struct A}' has no member named 'b'|
What could I do to solve this???
This question already has answers here:
How to initialize private static members in C++?
(18 answers)
Closed 2 years ago.
I cannot explain the following (using C++20 rules)
//FooTemplate.h
template<typename T>
class FooTemplate {
private:
static std::vector<T> t; //linker error.
//inline static std::vector<T> t; --> will not produce linker error
//static T t; -->this works also with the appropriate changes in the following code
public:
static void setT(T t1)
{
t.push_back(t1);
}
static T getT(int i)
{
return t.at(i);
}
};
//main.cpp
#include"FooTemplate.h"
int main() {
FooTemplate<Foo>::setT(Foo{});
FooTemplate<Foo>::getT(0);
}
I have to inline the static member t in order for it to work. If I do not use a templated container (e..g define T t instead of vector<T> t it also works.
Any idea?
Static members must be defined (initialized) outside their class.
Simply write in your implementation:
template<class T> std::vector<T> FooTemplate<T>::t = [some initializer];
EDIT: You do not actually need to explicitely initialize them (unless they are objects of a class without a default constructor), but you do need to repeat their declaration like this anyway.
This question already has answers here:
My enum is not a class or namespace
(4 answers)
Closed 4 years ago.
I declare an enum inside a class and then try to access it a different class. This works perfectly fine with vs2010, but it fails with g++ with the error "not a class or namespace name"
//Manager.h
class TestMgr
{
public:
typedef enum
{
kError,
kRun,
kFly,
kDefault = kRun
}Mode;
};
The main function
//main.cpp
#include"Manager.h"
int main()
{
TestMgr::Mode _mode = TestMgr::Mode::kDefault;
}
This gives me a compilation error: ‘TestMgr::Mode’ is not a class or namespace
Please guide.
Edit: I see that there is a similar question here, but that talks about an enum declared globally, not within a class.
That compiles to me:
class TestMgr
{
public:
typedef enum Mode
{
kError,
kRun,
kFly,
kGDefault = kRun
}Mode;
};
int main()
{
TestMgr::Mode _mode = TestMgr::Mode::kGDefault;
}
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 9 years ago.
Let's say I have a .hpp file containing a simple class with a public static method and a private static member/variable.
This is an example class:
class MyClass
{
public:
static int DoSomethingWithTheVar()
{
TheVar = 10;
return TheVar;
}
private:
static int TheVar;
}
And when I call:
int Result = MyClass::DoSomethingWithTheVar();
I would expect that "Result" is equal to 10;
Instead I get (at line 10):
undefined reference to `MyClass::TheVar'
Line 10 is "TheVar = 10;" from the method.
My question is if its possible to access a private static member (TheVar) from a static method (DoSomethingWithTheVar)?
The response to your question is yes ! You just missed to define the static member TheVar :
int MyClass::TheVar = 0;
In a cpp file.
It is to respect the One definition rule.
Example :
// Myclass.h
class MyClass
{
public:
static int DoSomethingWithTheVar()
{
TheVar = 10;
return TheVar;
}
private:
static int TheVar;
};
// Myclass.cpp
#include "Myclass.h"
int MyClass::TheVar = 0;