This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Making a template parameter a friend?
C++ Faq 23.11 How can I set up my class so it won't be inherited from?
lists the following code:
class Fred;
class FredBase {
private:
friend class Fred;
FredBase() { }
};
class Fred : private virtual FredBase {
public:
...
};
I tried to make a generic template for the same.
#include <stdio.h>
template<typename MAKE_ME_NONINHERITABLE >
class NonInheritable{
private:
NonInheritable(){
}
friend MAKE_ME_NONINHERITABLE; //<--- error here
};
This give me an error:
xxx.cpp:11: error: a class-key must be used when declaring a friend
So I tried:
template<typename MAKE_ME_NONINHERITABLE >
class NonInheritable{
private:
NonInheritable(){
}
friend class MAKE_ME_NONINHERITABLE; //<--- error here
};
class A : virtual public NonInheritable<A>{
};
And I get this error:
xxx.cpp:11: error: using typedef-name `MAKE_ME_NONINHERITABLE' after `class'
Is there a way to make this work?
You can use final from c++11 or sealed from microsoft extensions for c++.
Related
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:
Derived template-class access to base-class member-data
(3 answers)
Closed 5 years ago.
I have created a variable in base class that is a vector of template but I'm not able to access this member from the derived class , can someone explain?
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
/*
*
*/
template <typename T>
class Apple{
protected:
public:
vector<T> vec;
virtual void foo(T node) =0;
};
template <typename T>
class Ball:public Apple<T>{
public:
void foo(T node){
cout<<"hahaha\n";
vec.push_back(node);/* I GET COMPILATION ERROR HERE"vec was not declared in this scope"*/
}
};
int main(int argc, char** argv) {
Ball<int> b;
b.foo(10);
return 0;
}
The member vec is of a template parameter dependent type so the compiler needs some help like:
this->vec.push_back(node);
or use qualified name:
Apple<T>::vec.push_back(node)
Thanks to #Avran Borborah - Why am I getting errors when my template-derived-class uses a member it inherits from its template-base-class?
Here’s the rule: the compiler does not look in dependent base classes (like B<T>) when looking up nondependent names (like f).
This doesn’t mean that inheritance doesn’t work. ...
You have not created any private members in the base class.
To resolve the error, you can replace the line which produces compilation error with :
Apple::vec.push_back(node);
or
this->vec.push_back(node)
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.
This question already has answers here:
When can I use a forward declaration?
(13 answers)
Closed 8 years ago.
Can anyone help me?
I want to create 2 classes which need each other like:
class CBullet{
public:
bool DetectCollision(CEnemy enemy){
//some code here
}
};
and
class CEnemy{
public:
CBullet *shootedBullet;
};
this creating an error. I know the reason for this error is because CBullet class want to use CEnemy class which is not created yet. So, i try to create a prototype of CEnemy class before CBullet class like:
class CEnemy;
class CBullet{
...
but it create another error enemy has incomplete type.
Did I make a wrong prototype class?
Do it the other way, as in your example CBullet needs CEnemy definition (note that it wouldn't if Cenemy would have been passed by pointer/reference to DetectCollision) :
#include "CEnemy.h"
class CBullet{
public:
bool DetectCollision(CEnemy enemy){
//some code here
}
};
and
class CBullet;
class CEnemy{
public:
CBullet *shootedBullet;
};