This question already has answers here:
Accessing inherited variable from templated parent class [duplicate]
(2 answers)
Closed 7 years ago.
I want to access protected variable in parent class, I have the following code and it compiles fine:
class Base
{
protected:
int a;
};
class Child : protected Base
{
public:
int b;
void foo(){
b = a;
}
};
int main() {
Child c;
c.foo();
}
Ok, now I want to make everything templated. I changed code to the following
template<typename T>
class Base
{
protected:
int a;
};
template <typename T>
class Child : protected Base<T>
{
public:
int b;
void foo(){
b = a;
}
};
int main() {
Child<int> c;
c.foo();
}
And got error:
test.cpp: In member function ‘void Child<T>::foo()’:
test.cpp:14:17: error: ‘a’ was not declared in this scope
b = a;
^
Is it correct behavior? What's the difference?
I use g++ 4.9.1
Hehe, my favourite C++ oddity!
This will work:
void foo()
{
b = this->a;
// ^^^^^^
}
Unqualified lookup doesn't work here because the base is a template. That's just the way it is, and comes down to highly technical details about how C++ programs are translated.
Related
This question already has answers here:
When should you use 'friend' in C++?
(31 answers)
Closed 1 year ago.
From the below code snipped, need to access member function of class A::B. Since class B is private unable to create object for it from non-member function. Kindly help me in getting resolve out of this without moving Class B into public
Code below for reference
A.h
#include <iostream>
class A
{
public:
A();
void Func();
private:
class B;
B *b;
};
A.cpp
#include "A.h"
#include "AB.h"
A::A()
{
b = new B(this);
}
void A::Func()
{
b->Func();
}
AB.h
#include "A.h"
class A::B
{
public:
B(A* a);
void Func();
private:
A* ptr;
};
AB.cpp
#include "AB.h"
A::B::B(A* a):ptr(a)
{
}
void A::B::Func()
{
std::cout<<"Do nothing ";
}
static void call_sample()
{
A::B* tmp;
//access member function of class A::B
}
Error below for reference:
In file included from AB.cpp:2:0:
AB.h: In function ‘void call_sample()’:
AB.h:3:10: error: ‘class A::B’ is private
class A::B
^
AB.cpp:15:4: error: within this context
A::B* tmp;
B is a nested class from A, and it is private, so, as a definition, it is not accessible from the outside. If you need to use B you can decleare B in the public part of A, and then you can use A::B* type.
This question already has answers here:
What's the difference between inline member function and normal member function?
(4 answers)
Closed 2 years ago.
i mean, this code:
class A
{
public:
void fun();
};
void A::fun()
{
cout << "fun() called";
}
is basically doing the same job as this:
class A
{
public:
void fun()
{
cout << "fun() called";
}
};
right? are there any differences between these codes, any particular reason to choose the one over the other?
Imagine two classes:
class A {
public:
A() { B b; }
};
class B {
public:
B() { A a; }
};
That code as written cannot compile, because no matter how you order those classes, one of them refers to something not defined. You can't forward-declare one class either, because it gets instantiated.
This code, however, compiles:
class A {
public:
A();
};
class B {
public:
B();
};
A::A() { B b; }
B::B() { A a; }
This question already has answers here:
error: member access into incomplete type : forward declaration of
(2 answers)
Closed 4 years ago.
I'm trying to figure out how to access member variable of forward declared class though pointer.
My code:
#include <iostream>
class Boo;
class Foo{
public:
int getNum() {
return booPtr->num; //Error: Member access into incomplete type 'Boo'
}
Boo *booPtr;
};
class Boo : public Foo {
public:
Boo() {
booPtr = this;
}
int num = 45;
};
int main() {
Boo boo;
int num = boo.getNum();
std::cout << num << '\n';
}
The error I get :
"return booPtr->num" : Member access into incomplete type 'Boo'
The result I expect :
45
How can I fix this error and what would be more common and safer way to achieve the same thing?
When you access members of a class, the class has to be complete type; only forward declaration is not enough.
You can move the definition of the member function out of the class definition and after the definition of Boo.
class Boo;
class Foo{
public:
int getNum();
Boo *booPtr;
};
class Boo : public Foo {
...
};
int Foo::getNum() {
return booPtr->num;
}
You need to define the member function getNum() where the type Boo is known to the compiler. Hence change the class definition of Foo to
class Boo;
class Foo{
public:
int getNum();
Boo *booPtr;
};
Then comes the definition of Boo, and finally
int Foo::getNum()
{
return booPtr->num;
}
This question already has answers here:
C++ inheritance - inaccessible base?
(2 answers)
Closed 7 years ago.
I've tried making a basic C++ program with some classes and ran into a problem. The program looks like:
#include<iostream>
using namespace std;
class A {
public:
int i;
A(int ai) {this->i = ai;}
A() {}
};
class B : A {
public:
A aa;
B(A &a) : A(a.i) {
aa = a;
}
};
int main()
{
A a(5);
B b(a);
cout << "Hello World!" << b.i;
return 0;
}
The program fails to compile with:
In function 'int main()':
Line 6: error: 'int A::i' is inaccessible
compilation terminated due to -Wfatal-errors.
But the variable i is public in the class A. What am I doing wrong?
You're inheriting A privately:
class B : A {
^^^^^^
You need to inherit A publicly:
class B : public A {
^^^^^^^^^^^^^
This question already has answers here:
Why does an overridden function in the derived class hide other overloads of the base class?
(4 answers)
Closed 8 years ago.
How can I use final overloaded function from derived class?
Compiler says 'no matching function for call to 'B::foo()''.
class A
{
public:
virtual void foo() final
{
std::cout << "foo";
}
virtual void foo(int b) = 0;
};
class B : public A
{
public:
void foo(int b) override
{
std::cout << b;
}
};
//Somewhere
B* b = new B;
b->foo(); //Error
But it works without overloading.
class A
{
public:
virtual void foo() final
{
std::cout << "foo";
}
};
class B : public A
{
};
//Somewhere
B* b = new B;
b->foo(); //Works!
This declaration in class B
void foo(int b) override
{
std::cout << b;
}
hides all other functions with the same name declared in class A (excluding of course the overriden function).
You can either call the function explicitly like this
b->A::foo();
Or include using declaration
using A::foo;
in the definition of class B.
Or you can cast the pointer to the base class pointer
static_cast<A *>( b )->foo();