class B {
public:
static int a;
};
class C:B {
};
I want to use a variable through any inherited classes but it has problem when I declare a.
B::B() {
a=1;
};
Do I do it right ?
Thanks for reading and waiting for your comments.
// I miss semicolons which is not the error I'm talking .
// This is an error when I try to delcare
class GameState {
public:
static int a = 1;
//...
};
Error 7 error C2864: 'CGameState::a' : only static const integral data members can be initialized within a class d:\my dropbox\work\#today\gdimario\gdimario\gamestate.h 18
I try to write a simple question which shows the problem I want instead of pasting my whole code.
You can use it directly like you did from both the derived and base class.
Perhaps your error is that you don't have semicolons at the end of your class declarations?
class B {
public:
static int a;
};
class C:B {
};
If you want to call it from an instance of C then you need to use public inheritance: (If nothing is specified private inheritance is assumed)
class C : public B {
};
To initialize a you need to do this (typically at the top of your corresponding .CPP file):
int B::a = 3;
You need to write in a CPP file:
int B::a;
And add the semicolons that Brad suggests. (Did you even compile your code? What did the compiler say?)
i think you ll get linker error.
since you have not defined of the static variable in the .cpp file.
e.g.
//hearer file
class X{
public : static int a ;
}
//impl file
int X::a(0);
....or...
For integral type you can also defined static variables when they are declared like:
class X{
public : static int a = 0;
}
Related
In a single header file, I have 2 class declaration. And one of them contains const value that I would like to use on top declared class.
Following is the basic implementation.
class B;
class A
{
public :
friend class B; // this should not matter since CONST_VAL is public
int a;
A() { a = B::CONST_VAL; } // !!! error line
}
class B
{
public :
static const int CONST_VAL = 1;
}
I feel this should be very basic concept, but I am getting compiler error. error C2027 : use of undefined type 'B' as well as error 2065: 'CONST_VAL': undeclared identifier
Why would this constructor think it does not know B nor B::CONST_VAL?
In the code you've given, B is forward declared at the top of the file:
class B;
...
This tells the compiler that B exists as a class, but the compiler doesn't know anything about B because its definition hasn't yet appeared. With a forward declaration like this, it doesn't make sense to use any B objects or members. You're allowed to use B* pointers because they don't require any implementation details of B however.
To fix this, you need to provide the definition of B before it's needed, either at the top of the file, or in a separate header file that you #include. For example:
class B {
public :
static const int CONST_VAL = 1;
};
class A {
public :
int a;
A() : a(B::CONST_VAL) { }
}
Im trying to do a C++ class function that can return other classes values. The code works if class A is defined first but i have more code that i dont want to mangle around. I figured i need somekind of forward declaration for class A.
What kind of forward declaration do i need to get this work? All my code is in one file. Does this problem dissapear if i properly split my classes to multiple files and include them to project or does it make any difference to VC++ compiler?
Semi pseudo code below.
// forward declaration
class A;
// class deifinitions
class B {
private:
int testvalue;
public:
void settestvalue(A &Aobj);
}
void B::settestvalue(A &Aobj) {
testvalue = Aobj.settestvalue();
}
class A {
private:
int test = 10;
public:
int testvalue();
};
int A::testvalue() {
return test;
}
// mainloop
A Aobj;
B Bobj;
Bobj.settestvalue (Aobj);
just put the defination of B's member-function after A's class definition.
I have 2 class(A & C) defined in a file allocate.h and class B defined in work.h.
I included allocate.h inside work.h file
My code looks like :
Class A{
}
Class C
{
public:
struct xyz{
A ob;
};
}
include"allocate.h"
Class C;
Class B{
void test(){
xyz obj; // `Error : error c2036 : xyz unknown size`
}
}
Can someone tell me how can i communicate the size of xyz structure in function test?
class in C++ (which is case-sensitive) is all lower-case and the declarations end in semicolons. Please paste real code into your questions!
In this instance, xyz is declared within the scope of class C, so you'll need to specify the scope when you use it.
class B{
void test(){
C::xyz obj;
}
};
Here is my code -
#include <iostream>
#include <conio.h>
using namespace std;
class Base
{
public:
int a;
};
//int Base::a = 5;
class Derived : public Base
{
public:
int static a;
};
int main()
{
Derived d;
cout<<d.a;
getch();
return 0;
}
I get a linker error here. But when I do it the other way round -
class Base
{
public:
int static a;
};
int Base::a = 5;
class Derived : public Base
{
public:
int a;
};
I get no error. Can someone please explain what is happening here.
All static members have to be explicitly defined/initialized outside the class.
In the second example you do this correctly (int Base::a=5), but in the first example you don't do this for Derived::a, adding the following line to the first example should solve it:
int Derived::a = 5;
You need to actually define static members. The same way as you do
int Base::a = 5
in the second case you should have done
int Derived::a = 5;
in the first case.
There are two questions here.
The first is why do you get the linker error in the first example?
Well, you get a linker error in the first example because you have not defined/initialized the static member of class B.
The second question is why does the compiler not complain of multiple declarations?
The compiler does not complain of multiple declarations because as far as it is concerned, the two variables are in different scopes and their mangled names will be different anyway. This has got nothing to do with one of the variables being static. In fact, static members are not even inherited. SO, the following code snippet without static variables is also correct:
class B {
public:
int a;
};
class C: public B {
public:
int a;
};
i got a compile error which i do not understand.
i have a h/cpp file combination that does not contain a class but just defines some utility functions. when i try to use a struct that is defined in another class i get the error:
error C2027: use of undefined type 'B::C'
so, stripped down to the problem, the h-file looks like this
namespace A {
void foo(B::C::SStruct const & Var);
}
the definition of SStruct is in a class which is in another h-file, that is of course included.
namespace B {
class C {
public:
struct SStruct { };
};
}
the strange thing is, i can use this struct in other classes fine, it just seems to be related to this one h-file which contains just utility functions.
what am i missing here?
thanks!
After correcting missing semicolons etc. this compiles:
namespace B {
class C {
public:
struct SStruct { };
};
}
namespace A {
void foo(B::C::SStruct const & Var);
}
Obviously, if the order of the two namespaces were switched, this would not work. Possibly you are #including your headers in the wrong order. If this is the error, that's bad design - you should not allow header order to matter in your code.
I assume you meant "class C", not "Class C".
struct SStruct is private to class C (private being the default visibility of class members).
As such, it is not visible outside class C and its friends, which does not include A::foo().
Class C {
struct SStruct { };
}
==>
class C {
public:
struct SStruct { };
};
Your class is missing a semicolon after the definition. It should be:
namespace B {
class C {
public:
struct SStruct { };
}; // <=== Here
}