friend function from a namespace

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
beloni
Learning poster
Posts: 66
Joined: Thu Jan 05, 2006 1:41 pm
Location: Pelotas, RS, Brazil

friend function from a namespace

Post by beloni » Mon Dec 04, 2006 1:16 pm

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
"A machine can do the work of fifty ordinary men, but no machine can do the work of one extraordinary man.", Shahriar Manzoor

misof
A great helper
Posts: 430
Joined: Wed Jun 09, 2004 1:31 pm

Post by misof » Tue Dec 05, 2006 6:46 pm

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.

beloni
Learning poster
Posts: 66
Joined: Thu Jan 05, 2006 1:41 pm
Location: Pelotas, RS, Brazil

Post by beloni » Wed Dec 06, 2006 6:32 pm

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...
"A machine can do the work of fifty ordinary men, but no machine can do the work of one extraordinary man.", Shahriar Manzoor

Post Reply

Return to “C++”