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