Page 1 of 1

friend function from a namespace

Posted: Mon Dec 04, 2006 1:16 pm
by beloni
hello, I want a friend function void set_prob( Combinadic &comb ) to be friend of a class Combinadic. but class Combinadic is splited in two files (combinadic.h and combinadic.cpp). set_prob is extern on a namespace in other file (wildcat.h) and must be implemented in file probability.cpp.
it must be someting like that (but still wrong):

Code: Select all

// this is combinadic.h
#include "wildcat.h"    // namespace wc

class Combinadic
{
	private:
		int **dic;
		int size, nlayer;

		void make_comb( int *tmp, int *seq, int n, int s, int a );
		void write2dic( int *data, int last );

	public:
		explicit Combinadic( int init_nlayer );
		~Combinadic();

		int get_size() const;
		void print() const;

//		friend void wc::set_prob( Combinadic &comb );
};


// this is wildcat.h
#include "combinadic.h"  // class Combinadic

#include "layer.h"

#include <map>
#include <string>

namespace wc
{
	using namespace std;

	extern map<string, double> *prob;
	extern int prob_size;
	extern Layer **layer_data;
	extern void set_prob( Combinadic &comb );
	extern void print_prob();
}

so how to avoid "loop includes" and how to handle a situation like that?
thanks

Posted: Tue Dec 05, 2006 6:46 pm
by misof
I'm not sure whether this is what you wanted :D but the canonical way of avoiding including a file more than once is:

Code: Select all

// this is file wildcat.h
#ifndef __wildcat_h
#define __wildcat_h
// ... original wildcat.h contents goes here
#endif
What this does: whenever you type #include "wildcat.h", the meaning is: if wildcat.h wasn't included above, include it now.

Posted: Wed Dec 06, 2006 6:32 pm
by beloni
thanks, but I know that and I used #ifndef, #define and #endif in my codes, I just have cut out it to simplify my examples...