How to do forward declaration of vector in a header file.

Hi, I have a header file in which we include vector at the top. But we want to remove that include. On doing so I get compilation errors as the header file uses std::vector<> at several instances in header file so I have to forward declare the vector to solve this issue. Can anyone help me as to how i can do it.

Header file :

#ifndef MKLMulticlassOPTIMIZATIONBASE_H_
#define MKLMulticlassOPTIMIZATIONBASE_H_
#include <shogun/lib/config.h>
#include <vector>
#include <shogun/base/SGObject.h>
namespace shogun
{
/** @brief MKLMulticlassOptimizationBase is a helper class for MKLMulticlass.
*
* it is a virtual base class for MKLMulticlassGLPK and MKLMulticlassGradient which are instances of optimization
*
*/
class MKLMulticlassOptimizationBase: public CSGObject
{
public:
/** Class default Constructor
*
*/
MKLMulticlassOptimizationBase();
/** Class default Destructor
*
*/
virtual ~MKLMulticlassOptimizationBase();
/** initializes solver
*
* @param numkernels2 is the number of kernels
*
*
*/
virtual void setup(const int32_t numkernels2);
/** adds a constraint to the LP arising in L1 MKL based on two parameters
*
* @param normw2 is the vector of \f$ \|w_k \|^2 \f$ for all kernels
* @param sumofpositivealphas is a term depending on alphas, labels and
* biases, see in the function float64_t getsumofsignfreealphas() from
* MKLMulticlass.h, it depends on the formulation of the underlying GMNPSVM.
*
*/
virtual void addconstraint(const ::std::vector<float64_t> & normw2,
const float64_t sumofpositivealphas);
/** computes MKL weights
*
* @param weights2 stores the new weights
*
*/
virtual void computeweights(std::vector<float64_t> & weights2);
/** @return object name */
virtual const char* get_name() const { return "MKLMulticlassOptimizationBase"; }
/** sets p-norm parameter for MKL
* @param norm the MKL norm
*/
virtual void set_mkl_norm(float64_t norm);
protected:
protected:
};
}
#endif
Don't. You're wasting your time and going to break stuff. Just leave the include there.
I agree with Duoas here. Why exactly do you want to remove the include statement?

-Albatross
Last edited on
actually we are transfering includes to its corresponding .cpp file so it was succesful for most of the classes but for this header file its showing compilation error so we thought we would forward declare it.
The entities in the C ++ standard library are defined in headers, whose contents are made available to a translation unit when it contains the appropriate #include preprocessing directive - IS


This may work with some implementations:

1
2
3
4
5
6
7
8
9
10
namespace std 
{
    template < typename > struct allocator ;
    template < typename, typename > struct vector ;
}

namespace shogun
{
    std::vector< int, std::allocator<int> > foo( std::vector< int, std::allocator<int> > v ) ;
}

http://coliru.stacked-crooked.com/a/c7ddda593874940d
http://rextester.com/ASIUW21962
Last edited on
sry m not getting how to implement it in the above header file i tried and got several compilation errors.
Like what?

Simply forward-declaring it isn't enough if you use it for anything but a pointer or reference type, or if you have anything referencing any of its members.

Again, you need to ask why you are so dead-set on removing something that you require?
It may not compile since it is NOT something you're supposed to be able to do.
(You should never add stuff to namespace std. Some compilers allow it, but it's better to leave the include at its place. Or, try using iosfwd)
This is probably part of your problems.

1
2
        virtual void addconstraint(const ::std::vector<float64_t> & normw2,
                                    const float64_t sumofpositivealphas);


You seem to have an extra scope resolution operator:: before the first std.
The forward declaration does not have a default for the second template parameter. Try

1
2
3
4
5
// virtual void addconstraint(const ::std::vector<float64_t> & normw2, const float64_t sumofpositivealphas);
virtual void addconstraint(const ::std::vector< float64_t, std::allocator<float64_t> > & normw2, const float64_t sumofpositivealphas);

// virtual void computeweights(std::vector<float64_t> & weights2);
virtual void computeweights(std::vector< float64_t, std::allocator<float64_t> > & weights2);
@ JLBorges this is what i tried i am still getting errors:
#ifndef MKLMulticlassOPTIMIZATIONBASE_H_
#define MKLMulticlassOPTIMIZATIONBASE_H_
//#include <vector>
#include <shogun/base/SGObject.h>

namespace std
{
template < typename > struct allocator ;
template < typename, typename > struct vector ;
}

namespace shogun
{
std::vector< int, std::allocator<int> > foo( std::vector< int, std::allocator<int> > v ) ;
/** @brief MKLMulticlassOptimizationBase is a helper class for MKLMulticlass.
*
* it is a virtual base class for MKLMulticlassGLPK and MKLMulticlassGradient which are instances of optimization
*
*/
class MKLMulticlassOptimizationBase: public CSGObject
{
public:
/** Class default Constructor
*
*/
MKLMulticlassOptimizationBase();
/** Class default Destructor
*
*/
virtual ~MKLMulticlassOptimizationBase();

/** initializes solver
*
* @param numkernels2 is the number of kernels
*
*
*/
virtual void setup(const int32_t numkernels2);

/** adds a constraint to the LP arising in L1 MKL based on two parameters
*
* @param normw2 is the vector of \f$ \|w_k \|^2 \f$ for all kernels
* @param sumofpositivealphas is a term depending on alphas, labels and
* biases, see in the function float64_t getsumofsignfreealphas() from
* MKLMulticlass.h, it depends on the formulation of the underlying GMNPSVM.
*
*/
//virtual void addconstraint(const ::std::vector<float64_t> & normw2,
// const float64_t sumofpositivealphas);
// virtual void addconstraint(const ::std::vector<float64_t> & normw2, const float64_t sumofpositivealphas);
virtual void addconstraint(const ::std::vector< float64_t, std::allocator<float64_t> > & normw2, const float64_t sumofpositivealphas);

/** computes MKL weights
*
* @param weights2 stores the new weights
*
*/
//virtual void computeweights(std::vector<float64_t> & weights2);
// virtual void computeweights(std::vector<float64_t> & weights2);
virtual void computeweights(std::vector< float64_t, std::allocator<float64_t> > & weights2);
/** @return object name */
virtual const char* get_name() const { return "MKLMulticlassOptimizationBase"; }

/** sets p-norm parameter for MKL
* @param norm the MKL norm
*/
virtual void set_mkl_norm(float64_t norm);

protected:


protected:

};
}

#endif
Yeah, clang++ generates errors.

g++ and msc++ seem to be accepting it.

http://coliru.stacked-crooked.com/a/12321c0a2866f105
http://rextester.com/RBZHXA76710
i have compiling in ubuntu 14.04 and i get this error :

/home/saurabh/shogun-install/shogun-3.1.1/src/shogun/../shogun/classifier/mkl/MKLMulticlassOptimizationBase.h:19:44: error: provided for ‘template<class, class> struct std::vector’
template < typename, typename > struct vector ;

any ideas to solve it ??
> any ideas to solve it ??

No.
The behavior of a C ++ program is undefined if it adds declarations or definitions to namespace std or to a namespace within namespace std unless otherwise specified. - IS

g++ seems to accept a program adding declarations for allocator<> and vector<> in the namespace std;
clang++ does not, and gives an error.
http://coliru.stacked-crooked.com/a/cd97178f6f1c1444
Topic archived. No new replies allowed.