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???
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
#include <iostream>
using namespace std ;
class maths{
int a;
int b;
void function(int b) {
a=1;
while (a<=10) {
cout<<a*b;
a++;
}
}
};
int main() {
maths product;
product.function(4);
// the 'product.function is showing some error saying the declaration is not accessible
return 0;
}
In C++ class members default to private. Private members can only be accessed from within the class itself. You need to set the members to be public, like this:
class maths {
public:
// Everything after this label is now public
int a;
int b;
// etc.
// If you want to define some members as private later, do this
private:
int a_private_variable;
};
One way to solve this would be to just replace the keyword class with struct as shown below.The use of struct makes all the member of the class public by default which means the user of the class can access them directly.
#include <iostream>
using namespace std ;
struct maths{//NOTE THE KEYWORD STRUCT HERE
int a;
int b;
void function(int b) {
a=1;
while (a<=10) {
cout<<a*b;
a++;
}
}
};
int main() {
maths product;
product.function(4);
// the 'product.function is showing some error saying the declaration is not accessible
return 0;
}
This question already has an answer here:
Why is there an injected class name?
(1 answer)
Closed 2 years ago.
I don't know how to explain this:
namespace A
{
struct B
{
static void f()
{
}
};
}
int main()
{
A::B::B::B::B::B::B::B::B::B::B::f();
}
Why could i do :
A::B::B::B::B::B::B::B::B::B::B::f();
I don't understand it why it's happening.
It is due to injected-class-name
inside class B, B refers to class B, as B::B.
so A::B::B refers to class B. and so on.
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:
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:
"X does not name a type" error in C++
(8 answers)
Closed 6 years ago.
I have two class the first one contains the struct,
that's what in A.h
class A
{
public:
A();
struct structname
{double a;....};
};
and I want to use this struct as an argument of the constructor of B
#include "A.h"
B::B(A::structname data)
{
}
but I got the error message:
'structname' in 'class A' does not name a type
what is the problem?
details:Two classes are in different .cpp file.
In A.cpp and B.cpp: I just #include the A.h and B.h
In B.h :
class A;
class B
{
public:
B(A::structname data);
}
actual struct:
struct structname
{
std::vector<double> a={0,0,0};
std::vector<double> B={0,0,0};
std::vector<double> c={0,0,0};
std::vector<double> d={0,0,0};
}
class A
{
public:
A();
struct structname
{
std::vector<double> a={0,0,0};
std::vector<double> B={0,0,0};
std::vector<double> c={0,0,0};
std::vector<double> d={0,0,0};
};
};
class B
{
public:
B(A::structname data);
};
Just format your code. You'll see all your mistakes.
Apperently you are forward declaring A in B.h (thats what I read from your snippets). If you are forward declaring A, the compiler only knows that there is somewhere a type named A that is of class type. It does not know about A::structname there.
Do your forward declaring the other way round or just avoid nested types at all.