pointer to a class?

D

dvir

Hi

I want to declare a variable which is pointer to a class at my header.
here is my header:

base_functions.H
-----------------------------

#ifndef BASE_FUNCTIONS_H
#define BASE_FUNCTIONS_H

#ifndef BASIS
#define BASIS

#define DT 0.01

#include "weights.H"
#include <math.h>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
#include <boost/numeric/ublas/vector_proxy.hpp>




typedef struct basis_t
{
boost::numeric::ublas::matrix<float> cov;
boost::numeric::ublas::vector<float> mu;
float a;
float b;
float previous_b;
} basis;

#define ETA_C 0.1
#define ETA_M 0.1
#define INITIAL_VALUE 1.0
#define INITIAL_DELTA 1

class base_functions
{

private:

int N;
boost::numeric::ublas::matrix<float> diag
(boost::numeric::ublas::vector<float> v);
int function_num;
boost::numeric::ublas::vector<float> initial_cov;
basis* functions_vec;
C_weights* weights;



public:
base_functions(boost::numeric::ublas::vector<float>
covariance,C_weights* new_weights );
~base_functions();
int get_function_num();
bool build_new_function(float func_value, float delta);
void update(boost::numeric::ublas::vector<float> x);
float get_previous_b(int index);
float get_b(int index);
float get_max_a();
void compute_function(boost::numeric::ublas::vector<float> x);

};

#endif
#endif

and here is the interface of C_weight class

weights.H
-------------------

#ifndef WEIGHTS_H
#define WEIGHTS_H

#ifndef WEIGHT
#define WEIGHT

#define DT 0.01

#include "base_functions.H"

typedef struct weight_t
{
float current_value;
float prev_value;
}weight;




class C_weights
{

private:
weight* weights_vec;


public:
C_weights();
~C_weights();
float get_previous_weight(int i);
float get_current_wieght(int i);
void update_wieghts(float eta, float noise,base_functions* functions,
float delta);
bool new_weight(float func_value, float delta,int func_num);
//weight wheits_vec[0];
boost::numeric::ublas::matrix<weight> wghits_mat;
};

#endif
#endif


As you can see I tried to declare "C_weight* weights" at
base_function.H.
But when I compiled it I got the following error:

base_functions.H:66: error: ISO C++ forbids declaration of `C_weights'
with no type
base_functions.H:66: error: expected `;' before '*' token
base_functions.H:71: error: `C_weights' has not been declared
base_functions.H:71: error: ISO C++ forbids declaration of
`new_weights' with no type


I'm using Cygwin g++ 3.4.4

Thanks for your help.

Dvir
 
Y

yuvalif

You have a circular inclusion in the header files.
Don't include weight.H in base_function.H and use forward declaration
of C_weights.

BTW, In c++ you don't need to typedef the struct.
 
D

dvir

What do you mean by forward declaration?
Can you please write the right code line?

Thanks.
Dvir
 
S

shailesh

Here is an example of forward declaration.

-- A.h --

class B; //forward declaration of class B.
//Note that I have not included B.h

class A
{
public:
B* pb;
};


----------- B.h -------------
#include "A.h"

class B
{
public:
A m_a; //B includes an A object as member
// hence B needs to have definition of A available to it.
};
 
B

boaz_sade

"What do you mean by forward declaration? "
Forward declaratio is when you want to tell the compiler that there
is some type that you are not using but it should be award of -
class MyClass;
void f(MyClass &); // this will only work if you are not using the type
(for exmple you don't have inline function in the H file that you
implemented using this type). It is useful when you want to minimized
your include file at your H files. It will not work for member of
class/struct/unions that are not pointer/reference, function getting
this type by value (and not pointer/refernce), base classes and
enumerators/namespace
You may think of forward declaration as like the saperation between
function declation and the function implementation. Declation tell the
compiler that you have some type of function with a given name and the
implemetination is were you actualy using it. In type forward
declaration you are only tellling the compiler about at type name
Its a good practice to include as little as posible in the H file as
this is why this technic is useful. This will solve you other problem -
circular inclusion. If you are using forward declation you don't have
to include in the H file in your case.
Last but not least - never use C style typedef on struct/union/enum
since they are all types already, doing so is like typedef a class. And
even more impotant DO NOT use macro to declare const values in C++
!!!!!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,590
Members
45,100
Latest member
MelodeeFaj
Top