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;
}
Related
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:
Why can I use auto on a private type?
(5 answers)
Closed 2 years ago.
Good morning everybody,
I'm trying to write a member function of a class OUTER_CLASS which returns a unique_ptr to a private class INNER_CLASS defined within the OUTER_CLASS itself. My intent is to write a factory method which returns a reference to an Interface Class which allows to interact with the Implementation Class hided from client. Apart from the design choice, I have a compilation issue: given the following snippet of code, which is a very simplified version of my architecture
#include <iostream>
#include <string>
#include <vector>
#include <memory>
using namespace std;
class OuterClass
{
class InnerClass
{
friend class OuterClass;
public:
void myPrint() { cout << "Inner Class " << a << endl;}
InnerClass(int inA) : a(inA) {};
private:
int a;
};
public:
std::unique_ptr<InnerClass> CreateInnerClass()
{
std::unique_ptr<InnerClass> innerObj;
innerObj.reset(new InnerClass(1));
return innerObj;
}
};
int main()
{
OuterClass obj;
std::unique_ptr<OuterClass::InnerClass> innerObj = obj.CreateInnerClass();
innerObj->myPrint();
}
I get following error:
main.cpp: In function 'int main()':
main.cpp:43:10: error: 'class OuterClass::InnerClass' is private within this context
43 | std::unique_ptr<OuterClass::InnerClass> innerObj = obj.CreateInnerClass();
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:20:11: note: declared private here
20 | class InnerClass
| ^~~~~~~~~~
if instead i use AUTO type deduction, writing
int main()
{
OuterClass obj;
auto innerObj = obj.CreateInnerClass();
innerObj->myPrint();
}
everything is fine...
How is it possible? What am i doing wrong?
Thanks in advance for any answer
Accessibility applies on name, not the type being referred to. So you just can't use the name OuterClass::InnerClass directly in main(), but you can still use the type like calling member functions on the returned object whose type is OuterClass::InnerClass like
obj.CreateInnerClass()->myPrint();
Given auto innerObj = obj.CreateInnerClass(); the name OuterClass::InnerClass is not used directly then it doesn't violate the accessibility check. For the same reason you can use decltype too, even it seems redundant. E.g.
decltype(obj.CreateInnerClass()) innerObj = obj.CreateInnerClass();
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;
This question already has answers here:
Is there any reason to use this->
(16 answers)
Closed 7 years ago.
I have a code working like:
class base {
....
protected:
typeA m_mem;
}
class mymodule: public base{
....
void function(){
m_mem.call();
}
}
This was working OK before. Suddenly, I see it brokes saying "m_mem was not declared...." It might be some other people changed the namespace or other parts.
And I found it was working by just adding "this" then it compiles fine
this->m_mem.call()
While I just would like to know what's the cases that I must use "this" ? I learned "this" can be used to point to distinguish between class member and argument names. While for my case, what can be the reason that I must use "this" for accessing a data member
This can occur for example when you use templates.
The compiler will issue an error that x is an undefined variable for these class definitions
template <class T>
struct A
{
int x;
};
template <class T>
struct B : A<T>
{
void set_x( int v ) { x = v; }
};
If you write
void set_x( int v ) { this->x = v; }
the code will compile.
This question already has answers here:
templates: parent class member variables not visible in inherited class
(3 answers)
Closed 8 years ago.
When I compile the following templated C++ code with GCC 4.8.3
template <typename dtype> class Base {
public:
dtype base;
dtype ceiling;
Base() { };
virtual ~Base() { };
};
template<typename dtype> class Building : public Base<dtype> {
public:
dtype wall;
Building(dtype concrete) {
Base<dtype>::base=concrete;
ceiling=concrete;
wall=concrete;
};
~Building() { };
};
int main (int argc, char* argv[]) {
Building<float>* building=new Building<float>(2.0);
std::cout << building->base << std::endl;
}
I get the error
error: ‘ceiling’ was not declared in this scope
ceiling=concrete;
So it appears that
Base<dtype>::base=concrete;
works, but
ceiling=concrete;
does not. Is there any way I can mogrify this templated code so that, in the derived class constructor I can just reference "ceiling" from the templated base class without having to clarify which class it is from?
Thanks in advance
You can use this->ceiling.