I'm writing a program that uses several algorithms to do some basic searching and sorting. I wrote them all in the same .cpp file originally--but I want to do the right thing and break them out into separate header files and the main program.
I'm getting 4 errors upon compilation(1 error in the included header, 3 in the non-included header--left out for brevity sake)--all have to do with the inSize identifier. Error is 'inSize' undeclared identifier.
the inSize int is what I use to set an array to 1000 entries. I probably shouldn't make this variable a global, correct? If so, where should I place it to get around this compilation error? Its probably mind-numbingly simple and I really should stop writing my programs and THEN splitting them out (unless that's a good way of doing things?)--I welcome your inputs, both for my code issue and for your learned "best Practices" in writing multiple part programs. Thank you!
My Header File(excerpt)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#ifndef __searching_INCLUDED__
#define __searching_INCLUDED__
#include <iostream>
usingnamespace std;
class binarysearch
{
binarysearch(int inArray[], int sKey, int com)
{
int lo = 1;
int hi = inSize;
do
#include "stdafx.h"
#include "searching.h"
#include "sorting.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
constint inSize = 1000;
void insertionsort(int inArray[inSize], int com, int swa); //Function definition for insertionsort().
void selectionsort(int inArray[inSize], int com, int swa);//Function definition for selectionsort().
void binarysearch(int inArray[], int sKey, int com);//Function definition for binarysearch().
int main()
{
int times;
int com = 0;
int swa = 0;
#ifndef _searching_INCLUDED_
#define _searching_INCLUDED_
//#include <iostream> // <--- Not needed in a heder file.
//using namespace std; // <--- Never use in a header file.
class binarysearch
{
public:
binarysearch(); // default ctor.
~binarysearch(); // Default dtor.
void binarysearch(int inArray[], int sKey, int com);
// Prototypes or forward declarations here.
private:
// variables here
//.
//.
//.
};
#endif // end !_searching_INCLUDED_
#include <iostream>
// Other needed header files.
#include "searching.h"
// Default ctor. Definition not necessary, but good programming.
binarysearch::binaarysearch()
{
// You could initialize the private member variables here or create an overloaded ctor to set the variables when created.
}
// Default dtor. Definition not necessary, but good programming.
binarysearch::~binarysearch()
{
}
void binarysearch::binarysearch(int inArray[], int sKey, int com);
{
int lo = 1;
int hi = inSize;
do
{
.
.
.
} while (??)
} // End binarysearch().
#include "stdafx.h"
#include "searching.h"
#include "sorting.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std; // Not a good idea.
constint inSize = 1000;
// These are declarations or prototypes. Not definitions. The semi-colon is a giveawy.
void insertionsort(int inArray[], int com, int swa); //Function definition for insertionsort(). inSize not needednfor the array here.
void selectionsort(int inArray[], int com, int swa);//Function definition for selectionsort(). inSize not needednfor the array here.
void binarysearch(int inArray[], int sKey, int com);//Function definition for binarysearch(). This one is OK.
int main()
{
int times;
int com = 0;
int swa = 0;
// Need to define your array here.
//.
//.
//.
return 0;
}
A basic idea to get you started.
When passing a 1D array the "inSize" is not need in the function parameters. Either in the prototype or the definition.
"inSize" is defined in main and is not visible to functions in other files.
Done right "inSize"would be passed as a function parameter and not thought of as a global variable that can be seen by all files. If you need this variable in other files you could define it as an "extern".
Thank you all for your inputs--I do have a question though--and I may be tumbling down another rabbit hole on this one---is there any reason why I shouldn't use <vector> instead of a array?