In IHello.hpp file
#include "trees.hpp" // EDITED this line
namespace world
{
class IHello
{
public:
struct hi
{
char a;
};
void func(plants a); //// EDITED this line
};
}
In Hello.hpp file
#include "IHello.hpp"
class Hello : public world::IHello
{
private:
void new_world(hi *init); // function in which struct is used
hi init; // initialization of structure , in this file it does not give error
};
In trees.hpp // different file where I want to use the structure
#include "IHello.hpp"
enum plants
{
cactus = 0x01
}
class trees
{
public:
void new_func(hi *b); // using that structure here, shows error of hi structure has not been declared
// 2nd method - void new_func(world::IHello::hi *b) // error that world has not been declared and 2nd error - error: expected ',' or '...' before '*' token
};
So what do I need to do in order to get the structure which is initialized in class IHello to be visible in class which does not inherit IHello?
There is no error in class Hello because it inherits class world::IHello. The structure hi is part of that class, and because it's not a private declaration, it is visible to and usable by Hello too.
The error in class trees is because this class does not inherit world::IHello and there is no type named hi within the naming scope of where that member function is declared. That type only exists within the class world::IHello.
To fix the error, you must qualify the name so that the compiler knows what you want:
void new_func(world::IHello::hi *b);
Related
im learing c++.I was goofing around trying new stuff and wanted to use an object class sub in class object.But i was getting error saying that the object of class sub not in not defined.I know how to solve this issue i just have to move class sub above class object so that the compiler knows that there is a class called sub.
But I feel like this will get annoying as my code grows bigger and bigger so i tried forward declaring class like we do for function prototyping.But this doesn't work as it gives me this error -
'object::thing' uses undefined class 'sub'
Here is the code -
#include <iostream>
#include <vector>
class sub;
class object;
class object
{
private:
sub thing;
int ray;
public:
void set(int n);
void get() const;
};
class sub
{
public:
int num;
public:
void set_num(int n);
void get_num() const;
};
int main()
{
object ray;
ray.set(4);
ray.get();
}
Can you guys help me out??
thanks
When a type is used in c++, at a minimum that type needs to have been declared previously.
However, in some cases, the type needs to be defined (complete) as well.
class A;
class B {
A a; // error, because A needs to be defined
};
In other cases, the type only needs to be declared (i.e. it can be incomplete):
class A;
class B {
A *a; // fine, because A only needs to be declared
};
If a type needs to be defined when it's used (as in your case), then you have no choice but to define it before hand. You could put the definition in a header file, but that file still needs to be included before the type is used.
I got the following problem, does anyone have a good idea?
class Vector_2d;
namespace Utils {
class Align_vector : public Vector_2d {
protected:
bool check_range(int x, int y);
public:
enum alignment {left, right, up, down};
Align_vector(Alignment alignment);
void set_alignment(Alignment alignment);
Alignment get_alignment();
};
}
the error is:
error: invalid use of incomplete type ‘class Vector_2d’
But how is there an error?
class Vector_2d; This only declares a class by that name exits.
To inherit from it, the full class definition needs to be available.
class Vector_2d {
// Your code goes here
};
class Align_vector : public Vector_2d {
// Other stuff
};
If you have separate header files for these classes, be sure to include it before defining the class that inherits.
#include <vector_2d.h>
namespace Utils {
class Align_vector : public Vector_2d {
// Other stuff
};
}
To put it simply, when class B inherits from class A, objects of class B will have an A sub-object as part of their layout.
So you can't define the layout of B, which depends on A, if you don't have the full definition of A.
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.
How do I initialize the structure variables of type class objects? I have the following code:
#include<iostream>
using namespace std;
class bitmap {
public :
bitmap() { clear() ;}
get();
set();
clear();
static const int a=10;
};
bitmap::get() {
};
struct bitmap_list {
bitmap_list_value _value;
}
int main()
{
bitmap bitmap_list_value;
bitmap_list bbbb;
bbbb. _value=bitmap_list_value.a;
cout << bbbb._value << endl;
}
Is this code correct, or is it possible to initialize the structure containing the class objects? This is the error I receive:
>error: ‘struct error: ‘_bitmap_list_value’ does not name a type
>error:bitmap_list’ has no member named ‘_value’
No, this code is not correct. You're referencing a type bitmap_list_value which is never declared.
Based on your comment ("bitmap_list_value is object of class bitmap"), it sounds as if you also have this, but haven't included it in your question's code for some reason:
typedef bitmap bitmap_list_value;
But yes, of course you can include members of class types inside structs. A struct is more or less a class with all fields made public by default, you can define methods inside structs just as you can with classes, and so on.
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;
}