error c2036 : unknown size if structure - c++

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;
}
};

Related

Why would the use of static const in second class give compiler error in first class?

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) { }
}

C++ Calling a function from another class

Very new to c++ having trouble calling a function from another class.
Class B inherits from Class A, and I want class A to be able to call a function created in class B.
using namespace std;
class B;
class A
{
public:
void CallFunction ()
{
B b;
b.bFunction();
}
};
class B: public A
{
public:
virtual void bFunction()
{
//stuff done here
}
};
It all looks fine on screen (no obvious errors) but when I try to compile it i get an error C2079 'b' uses undefined class B.
I've tried making them pointers/ friends but I'm getting the same error.
void CallFunction ()
{ // <----- At this point the compiler knows
// nothing about the members of B.
B b;
b.bFunction();
}
This happens for the same reason that functions in C cannot call each other without at least one of them being declared as a function prototype.
To fix this issue we need to make sure both classes are declared before they are used. We separate the declaration from the definition. This MSDN article explains in more detail about the declarations and definitions.
class A
{
public:
void CallFunction ();
};
class B: public A
{
public:
virtual void bFunction()
{ ... }
};
void A::CallFunction ()
{
B b;
b.bFunction();
}
What you should do, is put CallFunction into *.cpp file, where you include B.h.
After edit, files will look like:
B.h:
#pragma once //or other specific to compiler...
using namespace std;
class A
{
public:
void CallFunction ();
};
class B: public A
{
public:
virtual void bFunction()
{
//stuff done here
}
};
B.cpp
#include "B.h"
void A::CallFunction(){
//use B object here...
}
Referencing to your explanation, that you have tried to change B b; into pointer- it would be okay, if you wouldn't use it in that same place. You can use pointer of undefined class(but declared), because ALL pointers have fixed byte size(4), so compiler doesn't have problems with that. But it knows nothing about the object they are pointing to(simply: knows the size/boundary, not the content).
So as long as you are using the knowledge, that all pointers are same size, you can use them anywhere. But if you want to use the object, they are pointing to, the class of this object must be already defined and known by compiler.
And last clarification: objects may differ in size, unlike pointers. Pointer is a number/index, which indicates the place in RAM, where something is stored(for example index: 0xf6a7b1).
class B is only declared but not defined at the beginning, which is what the compiler complains about. The root cause is that in class A's Call Function, you are referencing instance b of type B, which is incomplete and undefined. You can modify source like this without introducing new file(just for sake of simplicity, not recommended in practice):
using namespace std;
class A
{
public:
void CallFunction ();
};
class B: public A
{
public:
virtual void bFunction()
{
//stuff done here
}
};
// postpone definition of CallFunction here
void A::CallFunction ()
{
B b;
b.bFunction();
}
in A you have used a definition of B which is not given until then , that's why the compiler is giving error .
Forward declare class B and swap order of A and B definitions: 1st B and 2nd A. You can not call methods of forward declared B class.
Here's my solution to the issue. Tried to keep it straight and simple.
#include <iostream>
using namespace std;
class Game{
public:
void init(){
cout << "Hi" << endl;
}
}g;
class b : Game{ //class b uses/imports class Game
public:
void h(){
init(); //Use function from class Game
}
}A;
int main()
{
A.h();
return 0;
}
You can also have a look at the curiously recurring template pattern and solve your problem similar to this:
template<typename B_TYPE>
struct A
{
int callFctn()
{
B_TYPE b;
return b.bFctn();
}
};
struct B : A<B>
{
int bFctn()
{
return 5;
}
};
int main()
{
A<B> a;
return a.callFctn();
}

forward declared class in C++

I have a problem with C++.
I want to create two classes, A and B.
Class A has some methods that take an argument that is an instance of class B. But in Class B I also have some methods which take an argument that is an instance of class A.
I tried to forward declare class A and then define class B. Finally, I define class A.
Some code:
class A;
class B
{
void Method1(A* instaceOfA)
{
instaceOfA->MethodX();
}
.......
};
class A
{
Method1(B* instaceOfB);
MethodX();
.......
};
I code in Visual Studio 2010, and it shows an error because I invoke MethodX in class A but class A is not defined completely.
How can I solve this problem?
Put the definition of B::Method1 after declaration of class A
//header file
class A;
class B
{
void Method1(A* instaceOfA);
.......
};
class A
{
Method1(B* instaceOfB);
MethodX();
.......
};
// cpp file
void B::Method1(A* instaceOfA);
{
instaceOfA->MethodX();
}
This is the purpose of .cpp file. You declare the classes and methods in a header file then, add the definitions to the .cpp file.
I suggest moving implementations of methods outside class declarations, like this:
class A;
class B {
public:
void Method1(A* instanceOfA);
...
};
class A {
public:
void Method1(B* instanceOfB);
void MethodX();
...
};
void B::Method1(A* instanceOfA) {
...
}
Define and declare the classes the other way around?
class B;
class A
{
Method1(B* instaceOfB);
MethodX();
};
class B
{
void Method1(A* instaceOfA)
{
instaceOfA->MethodX();
}
};
Of course, this does not work if the A::Method1 function also is inline.
Is your impl code in the header file? If so, try moving it to a cpp file and #include A and B
Is there a reason you need to implement class B inline? If you seperate the class definitions from the implentations (ideally putting them in .h and .cpp files), this problem will go away..
//blah.h
class A;
class B
{
void Method1(A* instaceOfA);
.......
};
class A
{
Method1(B* instaceOfB);
MethodX();
.......
}l
//blah.cpp
#include "blah.h"
class B
{
void Method1(A* instaceOfA)
{
instaceOfA->MethodX();
}
.......
};
class A
{
Method1(B* instaceOfB)#
{
instaceOfB->Method1();
}
MethodX();
.......
}

How to : Static variable in inherited class

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;
}

c++ compilation error

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
}