470,833 Members | 1,405 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

pointer to a class?

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<flo at> 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

May 30 '06 #1
5 1981
* dvir:
#include "weights.H" .... base_functions.H:71: error: `C_weights' has not been declared


Declare C_weights before using it.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 30 '06 #2
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.

May 30 '06 #3
What do you mean by forward declaration?
Can you please write the right code line?

Thanks.
Dvir

May 30 '06 #4
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.
};

May 30 '06 #5
"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++
!!!!!!!

May 30 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Asfand Yar Qazi | last post: by
4 posts views Thread by Carsten Spieß | last post: by
6 posts views Thread by Itay_k | last post: by
7 posts views Thread by WaterWalk | last post: by
5 posts views Thread by Tim Frink | last post: by
5 posts views Thread by Immortal Nephi | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.