problem with function call

Hello,

I am writing a program which sorts a bunch of data and performs various physics calculations. Here is my code so far:

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
34
35
36
37
38
39
40
#include "stdafx.h"

#include "binary_search.cpp"
#include "dm_Steps.h"
#include "dm_Tracks.h"
#include "dm_Data.h"

int main() 
{
	std::cout << "Enter Proton Energy (MeV): ";
	float key;
	std::cin >> key;
	
	std::cout << "Enter Proton Flux (x1000): ";
	int k_proton_flux;
	std::cin >> k_proton_flux;

	double t0 = clock();

	std::cout << endl;
	string line;
	dm_Step current_step;
	dm_Track current_track, key_vec;
	dm_Data data_base;
	float x_pos, y_pos, z_pos;

	ifstream myfile("C:\\file");    

	// Check if file is found. 
	if (!myfile) 
	{
		std::cout << "Unable to open file!!! " << endl;
		cin.get();
		exit(1); 
	}

binary_search(myfile, line, current_track, key_vec, current_step, data_base, key, x_pos, y_pos, z_pos);

};


The dm_Steps, dm_Tracks and dm_Data h files are classes that organize a large data base. Here are the h files:

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
//dm_Steps
#include <iostream>
#include <iomanip>
#include <time.h>
#include <fstream>
#include <istream>
#include <string>
#include <vector>

#ifndef dm_Steps_h
#define dm_Steps_h

class dm_Step
{
public:
	float m_energy;
	float m_energy_loss;
	float m_step_length;
	float m_xpos;
	float m_ypos;
	float m_zpos;

	void print()
	{
		cout << m_energy << " " << m_energy_loss << " " << m_step_length << " " << m_xpos << " " << m_ypos << " " << m_zpos << endl;
	}
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//dm_Tracks
#ifndef dm_Tracks_h
#define dm_Tracks_h
#include "dm_Steps.h"
#include <vector>


class dm_Track
{
public:
	vector<dm_Step> m_track;

	void print()
	{
		for(int c = 0; c < m_track.size(); c++)
			m_track.at(c).print();
	}

};

#endif 


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
34
35
36
//dm_Data
#ifndef dm_Steps_h       
#define dm_Steps_h
#include "dm_Steps.h"
#endif 

#ifndef dm_Tracks_h       
#define dm_Tracks_h
#include "dm_Tracks.h"
#endif 

#ifndef dm_Data_h
#define dm_Data_h
#include "dm_Data.h"


# include <vector>

class dm_Data
{
public:
	vector<dm_Track> m_data;

	void print()
	{
		for(int c = 0; c < m_data.size(); c++) 
		{
			m_data.at(c).print();
			std::cout << endl;
		}

	}

};

#endif 


Finally my binary search function performs a binary search of certain parts of the data. Here is the code for binary_search.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "dm_Steps.h"
#include "dm_Tracks.h"
#include "dm_Data.h"

void binary_search(ifstream myfile, string line, dm_Track current_track, dm_Track key_vec, dm_Step current_step, dm_Data data_base, float key, float x_pos, float y_pos, float z_pos)
{
while (myfile.good())
	{
		getline(myfile, line);

		if (line == "end")
			break;

		else if (line == "")
		{
			int high = (int) current_track.m_track.size();
			int low = 0;
			double midpoint;

			while (low <= high)
			{
				midpoint = low + (high - low)/2 -1;
				setprecision(0); midpoint;

				if (key == current_track.m_track.at(midpoint).m_energy)
				{
					for(int i = midpoint; i < current_track.m_track.size(); i++)
						key_vec.m_track.push_back(current_track.m_track.at(i));	

					x_pos = key_vec.m_track.at(0).m_xpos;
					y_pos = key_vec.m_track.at(0).m_ypos;
					z_pos = key_vec.m_track.at(0).m_zpos;

					for(int j = 0; j < key_vec.m_track.size(); j++)
					{
						key_vec.m_track.at(j).m_xpos = key_vec.m_track.at(j).m_xpos - x_pos;
						key_vec.m_track.at(j).m_ypos = key_vec.m_track.at(j).m_ypos - y_pos;
						key_vec.m_track.at(j).m_zpos = key_vec.m_track.at(j).m_zpos - z_pos;
					}

					break;
				}

				else if (low == high)
				{
					if (current_track.m_track.at(midpoint).m_energy > key)
						midpoint = midpoint + 1;
					for(int i = midpoint; i < current_track.m_track.size(); i++)
						key_vec.m_track.push_back(current_track.m_track.at(i));

					x_pos = key_vec.m_track.at(0).m_xpos;
					y_pos = key_vec.m_track.at(0).m_ypos;
					z_pos = key_vec.m_track.at(0).m_zpos;

					for(int j = 0; j < key_vec.m_track.size(); j++)
					{
						key_vec.m_track.at(j).m_xpos = key_vec.m_track.at(j).m_xpos - x_pos;
						key_vec.m_track.at(j).m_ypos = key_vec.m_track.at(j).m_ypos - y_pos;
						key_vec.m_track.at(j).m_zpos = key_vec.m_track.at(j).m_zpos - z_pos;
					}

					break;
				}

				else if (key > current_track.m_track.at(midpoint).m_energy)
				{high = midpoint;}

				else
				{low = midpoint + 2;}
				
			}
			data_base.m_data.push_back(key_vec);
			key_vec = dm_Track();
			current_track.m_track.clear();
		}

		else 
		{
			current_step = dm_Step();
			sscanf(line.c_str(),"%f %f %f %f %f %f", &(current_step.m_energy), &(current_step.m_energy_loss), &(current_step.m_step_length), &(current_step.m_xpos), &(current_step.m_ypos), &(current_step.m_zpos));
			current_track.m_track.push_back(current_step);
		}
	}
}


When I try to compile the program in visual studio using the binary search function I get a compiler error C2248. I am pretty sure the problem lies in the use of the function call since when I insert the binary search code directly into the main program, the problem goes away. Can any body please help me execute my binary search algorithm using a function call?
#include "binary_search.cpp"

You shouldn't #include a .cpp file. Instead, you should put the prototype for binary_search into a .h file and #include that.

The compiler error is due to binary_search taking a ifstream by value. ifstreams can't be copy constructed. Instead, binary_search should take a reference to the ifstream.

i.e.

void binary_search(ifstream& myfile, ...);
Thanks shacktar. That seemed to work. However now I have another problem. When I run the program I now get the following error:
Run-Time Check Failure #3 - The variable 'z_pos' is being used without being initialized.

When I continue to run through the program I get the same error for y-pos, x-pos and current_step. Why does the compiler think these variables are not initialized?
25
26
	float x_pos, y_pos, z_pos;
	//Where do you ever set values to these? 
And you never set any values inside current_step? Also, that's NOT your compiler giving you those messages nor are they errors. They're debug warnings ;)
Last edited on
L B, I do initialize these values inside dm_Steps. I still don't understand why I am getting these warnings.
@ OP: You are not initializing the variables, all you are doing is telling the compiler to allocate memory. Also, the variables the you are mentioning in dm_Steps are not only named differently, they are specific to the instances of the objects they are associated with, also they are not being initialized either.
Ok I think I understand now. Thanks a lot!!
Topic archived. No new replies allowed.