6 Replies - 632 Views - Last Post: 24 July 2011 - 11:41 AM Rate Topic: -----

#1 machoolah  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 87
  • Joined: 17-May 09

interdependency between header files and source files

Posted 24 July 2011 - 11:20 AM

Hi,
I am trying to write a piece of code and this is the first time I am trying to separate the class files. I have a little problem here. There is a function, called shuffle defined in the main soucecode (SS.cpp). Is there anyway I could use this function in my class definitions? Or do I need to re-write it in the class file as well? Or there is a better way to do it?
the code is:

File name: SS.cpp
// SS.cpp
#include<iostream>
#include "util.h"
using namespace std;

void shuffle(ARRAY &p){
	int ind1, ind2;
	int tmp;
	for (int i = 0; i<p.size; i++){
		ind1 = rand() % p.size;
		ind2 = rand() % p.size;

		tmp         = p.ele[ind1];
		p.ele[ind1] = p.ele[ind2];
		p.ele[ind2] = tmp;
	}
}


int main(){
	ARRAY c(5);
	c.display();
	return 0;
}




File name: util.h

//util.h

#ifndef UTIL_H
#define UTIL_H

class ARRAY{
public:
	int ele[100];
	int size;
	double cost;


	////////////    INITIALIZER    ///////////
	ARRAY();
	ARRAY(int s);
};

#endif





File name: util.cpp

// util.cpp

#include <iostream>

#include "util.h"

ARRAY::ARRAY(){
	size = 0;
	cost = 0;
}

ARRAY::ARRAY(int s){
	size = s;
	for	(int i = 0;i<s; i++){
		ele[i] = i;
	}
	shuffle(this);
}



Please let me know how I can deal with this the best way. Just to clarify, the function is defined in SS.cpp in line 6 and it is used in util.cpp in line 17.


Thanks,

Is This A Good Question/Topic? 0
  • +

Replies To: interdependency between header files and source files

#2 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: interdependency between header files and source files

Posted 24 July 2011 - 11:22 AM

Presumably you are going to be using the ARRAY class in main, so you can just use the function normally.
Was This Post Helpful? 0
  • +
  • -

#3 machoolah  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 87
  • Joined: 17-May 09

Re: interdependency between header files and source files

Posted 24 July 2011 - 11:25 AM

Yes, the function is mainly used in the main function.
I can simply use the following in the main fuction:
ARRAY c;
shuffle (c)


However, this doesn't seem the best way to do it to me (I may be wrong though) if there is anyway to use shuffle in the class definition. What would be a good practice?

This post has been edited by machoolah: 24 July 2011 - 11:28 AM

Was This Post Helpful? 0
  • +
  • -

#4 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 550
  • Joined: 12-July 09

Re: interdependency between header files and source files

Posted 24 July 2011 - 11:27 AM

Just add the line
void shuffle(ARRAY &p);

to the util.h file. Then every file that includes it can use the function.
Was This Post Helpful? 0
  • +
  • -

#5 machoolah  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 87
  • Joined: 17-May 09

Re: interdependency between header files and source files

Posted 24 July 2011 - 11:33 AM

View PostHezekiah, on 24 July 2011 - 12:27 PM, said:

Just add the line
void shuffle(ARRAY &p);

to the util.h file. Then every file that includes it can use the function.


Where exactly in the util.h? It can't be before defining the ARRAY class.
Was This Post Helpful? 0
  • +
  • -

#6 Hezekiah  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 550
  • Joined: 12-July 09

Re: interdependency between header files and source files

Posted 24 July 2011 - 11:38 AM

Put it after the class (you've already said why), but before the #endif (so it isn't declared multiple times).
Was This Post Helpful? 1
  • +
  • -

#7 machoolah  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 87
  • Joined: 17-May 09

Re: interdependency between header files and source files

Posted 24 July 2011 - 11:41 AM

View PostHezekiah, on 24 July 2011 - 12:38 PM, said:

Put it after the class (you've already said why), but before the #endif (so it isn't declared multiple times).



Worked perfectly! Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1