Opaque pointer With Class Constructor - c++

I need to create some variable defined in class source private to that class only. I am not able to move this variable to class header because of some other header file issue. I found this page http://en.wikipedia.org/wiki/Opaque_pointer and explains how it can achieve,
Here is my class source
struct testClass::testStruct {
int a;
int b;
boost::asio::io_service io_service_2;
client c_3(io_service_2); // this is another class, getting error here
};
testClass::testClass(): test(new testStruct())
{
// do something
}
class header
class testClass
{
public:
testClass();
~testClass();
private:
struct testStruct;
testStruct* test;
};
While compile the I am getting the error
error: C2061: syntax error : identifier 'io_service_2'
Actually client is another class which I previously initialized as global
client c_3(io_service_2);
Now I cannot use it as global, I need to make it as private to the class, so choose above method.
Note: I cannot define client c_3 as class variable in class header because of some header issue. How can I resolve this issue?.
Thanks
Haris

Related

Issue in using structure in different functions of different classes

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

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

Qt C++ - Creation QList<Myclass> - is not a valid template type argument for parameter 'T

I have just started with C++ Qt and I wrote the following .h file: The .cpp file contains only empty constructors and destructors.
#include <QList>
class XML_Files
{
public:
XML_Files();
~XML_Files();
protected:
QList<Myclass> m_Xmls;
};
class Myclass
{
public:
Myclass();
~Myclass();
protected:
int a;
};
but I keep getting the following errors:
error C2065: 'Myclass': undeclared identifier
error C2923: 'QList': 'Myclass' is not a valid template type argument for parameter 'T'
What do I have to do to declare a Qlist with my own data type?
You can't use the name MyClass until the compiler knows about it. Since you do not declare MyClass until after XML_Files you cannot use it's name in XML_Files.
The simplest solution here is to just change the order of the declarations and declare MyClass before XML_Files.
The easy way to fix this, is to turn the order of both classes. However there is a second solution, if this is not desired or possible:
You may declare Myclass before defining it. Then compilation will succeed.
#include <QList>
class Myclass;
class XML_Files
{
public:
XML_Files();
~XML_Files();
protected:
QList<Myclass> m_Xmls;
};
class Myclass
{
// ...
};

Class static method returning struct containing object of this class

I've got a class:
class SOMECLASS
{
public:
(...)
static SOMESTRUCT GetInfo();
};
And a struct:
struct SOMESTRUCT
{
(...)
SOMECLASS Instance;
};
They are declared in SOMESTRUCT.h and SOMECLASS.h
I'm not able to compile it, even with forward declarations.
I'm still getting:
error: field ‘Instance’ has incomplete type
How can I solve that?
Try the following
SOMECLASS.h
#ifndef SOMECLASS_H
#DEFINE SOMECLASS_H
class SOMECLASS
{
public:
(...)
static struct SOMESTRUCT GetInfo();
};
#endif // SOMECLASS_H
SOMESTRUCT,h
#ifndef SOMESTRUCT_H
#DEFINE SOMESTRUCT_H
#include "SOMECLASS.h"
struct SOMESTRUCT
{
(...)
SOMECLASS Instance;
};
#endif // SOMESTRUCT_H
The answers given above would have been correct if the users include SOMESTRUCT.h.
If the user do this
#include "SOMECLASS.h"
int main()
{
SOMECLASS::GetInfo();
}
The same error would occur.
The reason is the compiler has to know the full definition of the return variable; otherwise it cannot know how to construct it when the function returns.
When SOMECLASS.h is included, the compiler only knows the identifier SOMESTRUCT is a sturct. If the user calls SOMECLASS::GetInfo, the function has to construct a SOMESTRUCT, so the full definition of the struct has to be given before that line in the same file.
To solve it, it is more reasonable to include SOMESTRUCT.h in SOMECLASS.h, so the user can be sure that calling any function in the class would not result in compilation error, as long as they include the header.
One should also forward declare class SOMECLASS in SOMESTRUCT.h.
If there are cases that only SOMESTRUCT.h is used, then the SOMECLASS.h should also be included in SOMESTRUCT.h, because constructing SOMESTRUCT requires the definition of SOMECLASS.h.
You could avoid this is by declaring Instance variable as SOMECLASS& or SOMECLASS* instead of SOMECLASS.
If you really don't want to expose the detailed definition of the SOMESTRUCT, you can try these
define an IInfo interface class with virtual accessor to get information from SOMESTRUCT
use pimpl idiom
By these ways you don't have to include SOMESTRUCT.h in SOMECLASS.h.

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