Just need a sanity check re: headers and undeclared identifier error

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>
using namespace std;

class binarysearch 
{
	binarysearch(int inArray[], int sKey, int com)

	{
	
		int lo = 1;
		int hi = inSize;

		do  


my .cpp (main) (excerpt)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "stdafx.h" 
#include "searching.h"
#include "sorting.h"
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int 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;



void foo(int array[42]) is equivalent to void foo(int *array)
pass the size as an extra parameter
void foo(int array[], int size)


by the way, ¿what're `com' and `swa'?
Hello CapitalJeep,

You have a good start, but I think this might work a little better for you:

searching header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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_ 


searching .cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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(). 


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "stdafx.h" 
#include "searching.h"
#include "sorting.h"
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;  // Not a good idea.

const int 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".

Hope that helps,

Andy

Edit: This is worth reading http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/
Last edited on
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?
Handy Andy--sent you a PM if you don't mind
Last edited on
-is there any reason why I shouldn't use <vector> instead of a array?

Absolutely not. std::vector is far safer to use than your own array.

For example, you can can add a nearly limited number of entries to a vector, while an array is limited to the declared size.
Topic archived. No new replies allowed.