Strange errors

Pages: 123
I'm getting some strange errors here that I don't quite understand. Here's the errors:

error LNK2019: unresolved external symbol "public: __thiscall Search::Search(int,int)"
error LNK1120: 1 unresolved externals


This is my code that I'm working with:
Driver Code(Not complete):
1
2
3
4
5
6
7
8
9
10
11
12
13
  #include <iostream>
#include <iomanip>
#include <time.h>
#include "search.h"

using namespace std;

int main()
{
	Search(1000);

	system("PAUSE");
}


Search Class
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include<iostream>
#include<iomanip>
#include "search.h"
#include <random>

using namespace std;

Search::~Search()
{
	delete [] array;
}

int Search::getSize()
{
	return size;
}

// Set Seed Function
void Search::set_seed(int seed)
{
	seed = seed;
}

// Sequential_Search Function
bool Search::sequential_search(int num)
{
	for(int i=0;i<size;i++)
	{
		if(array[i] == num)
		{
			return true;
		}
	}
	return false;
}

// Recursive_Binary_Search
bool Search::recursive_binary_search(int num)
{
	int lowIndex = 0;
	int highIndex = size-1;
	return recursive_binary_search(num, lowIndex, highIndex);
}

// Recursive_Binary_Search Helper
bool Search::recursive_binary_search(int num, int lowIndex, int highIndex)
{
	if(lowIndex>highIndex)
	{
		return false;
	}
	int midIndex = (lowIndex+highIndex)/2;
	if(array[midIndex] = num)
	{
		return true;
	}
	else
	{
		if(num<array[midIndex])
		{
			return recursive_binary_search(num, lowIndex, midIndex-1);
		}
		else
		{
			return recursive_binary_search(num, midIndex+1, highIndex);
		}
	}
}

// Iterative_Binary_Search Function
bool Search::iterative_binary_search(int num)
{	
	int lowIndex = 0;
	int highIndex = size-1;
	while(lowIndex<=highIndex)
	{
		int midIndex = (lowIndex+highIndex)/2;
		if(array[midIndex] > num)
		{
			highIndex=midIndex-1;
		}
		else if(array[midIndex] < num)
		{
			lowIndex=midIndex+1;
		}
		else
		{
			return true;
		}
	}
	return false;
}

// Initialize_array Function
void Search::initialize_array()
{
	int i=0;
//TODO	srand(seed);
	for(i; i<=getSize();i++)
	{
		double number = rand() % 1000;
		array[i]=number;
	}
}

//	Function to initialize the array with sorted random numbers
//	Don’t use a sort() function here. Rather, when initializing, insert a number a bit larger than the previous number. 
// array[0] = rand(5), array[1] = array[0] + rand(5) etc.
// Initialize_Sorted_array Function
void Search::initialize_sorted_array()
{	
// TODO	srand(seed);
	double number = 0;
	int i = 0;
	for(i; i<(getSize()); i++)
	{
		number = rand() % 1000;
		array[i]=array[i] + number;
		i++;
	}
}


and here is my header file that coincides with the class code:
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
#pragma once

#include<iostream>
#include<fstream>
#include<random>

using namespace std;

class Search
{
private:
	int size;
	double *array;
	// Recursive Helper function
	bool recursive_binary_search(int, int ,int);

public:
	// Param Constructor
	Search(int size, int seed = 0);
	// Deconstructor
	~Search();

	bool sequential_search(int);
	bool recursive_binary_search(int);
	bool iterative_binary_search(int);

	void initialize_array();
	void initialize_sorted_array();
	void set_seed(int seed);
	int getSize();

	friend ostream& operator<< (ostream& out, Search& s)
	{
		for(int i=0;i<s.getSize(); ++i)
		{
			out<<s.array[i]<<" ";
			return out;
		}
	}
};
The error means that you have declared that a function exists and tried to use it, but you forgot to provide a definition. In this case, where have you defined your constructor?
The constructor is defined on line 19 of the search header that was supplied to me by my professor.
By "defined" he means "gave it a body"

Your constructor has no body, which is why you're getting that error.
I get how to make a body for this kind of constructor:
1
2
3
4
5
Search::Search(int size, int seed)
{
   size = size;
   seed = seed;
}

but I'm at a loss as to how to make a body for this kind of constructor:
1
2
3
4
5
6
7
8
Search(int size, int seed = 0)

// Would it look like this:
Search::Search(int size, int seed)
{
   size = size;
   seed = 0;
}

If I do it like either one it comes up great with the console window but then this error pops up right after:

Unhandled exception at 0x54e765ca (msvcr100d.dll) in search.exe: 0xC0000005: Access violation reading location 0xccccccc0.
For future reference, line 19 of the header is a declaration and not a definition.
Okay. I've never really seen a declaration like that before.
A declaration tells you that something exists, and what type of thing it is, but it doesn't tell you how it actually works.

These are declarations:
1
2
3
4
struct MyClass;
class MyOtherClass;
extern int GlobalVariablesAreBad;
double ExampleFunction(double, double);
These are definitions:
1
2
3
4
5
6
7
8
9
10
11
12
13
struct MyClass
{
    //...
};
class MyOtherClass
{
    //...
};
int GlobalVariablesAreBad = 7;
double ExampleFunction(double a, double b)
{
    return a * b;
}
I get that but this declaration Search(int size, int seed = 0) is kinda new to me. I've never seen a declaration that already had part of it defined (seed = 0). So would this translate into this:
1
2
3
4
5
6
7
Search(int size, int seed = 0)

//translate to 
Search::Search(int size, int seed=0) 
{
   size = size;
}

or is my process of thought way off base here?
The 2nd parameter there is called a default parameter. If, say you called Search(10, 10), size and seed will have the value 10 each. If you called Search(10), then size will have 10 and seed will have 0. Because we only provided one argument, seed will have that default value of 0.

Note defaults must be the rightmost:

1
2
Search::Search(int seed=0, int size) // error
Search::Search(int size, int seed = 0) // correct way 
Last edited on
natekelsey wrote:
I get that but this declaration Search(int size, int seed = 0) is kinda new to me. I've never seen a declaration that already had part of it defined (seed = 0). So would this translate into this:
1
2
3
4
5
6
7
Search(int size, int seed = 0)

//translate to 
Search::Search(int size, int seed=0) 
{
   size = size;
}

or is my process of thought way off base here?
No, only the declaration has the = 0 part, the definition can not have it or you get an error.
So if I understand this correctly, then the declaration:
 
Search(int size, int seed = 0)

translates to
1
2
3
4
5
Search::Search(int size, int seed)
{
   size = size;
   seed = seed;
}

Or does it look like what Codeez is showing?
Yes, although since it's a constructor it would look more like this:
1
2
3
4
5
6
7
8
Search::Search(int size, int seed)
: size(size)
, seed(seed)
{
   //these just assign values to themselves
   //size = size;
   //seed = seed;
}
Well I've entered in the constructor as:
1
2
3
4
5
Search::Search(int size, int seed)
{
  size=size;
  seed=seed;
}


I've got a test driver set up as this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <iomanip>
#include <time.h>
#include "search.h"

using namespace std;

int main()
{
	Search search(1000);
	cout<<"Program runs."<<endl;
	system("PAUSE");
	return 0;
}

I'm running into this error:
Unhandled exception at 0x6e6e65ca (msvcr100d.dll) in search.exe: 0xC0000005: Access violation reading location 0xccccccc0.

and the compiler sends me off to here in the dbgdel.cpp:
1
2
   /* verify block type */
            _ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));

I'm clueless on compiler code(my university doesn't have us take a compiler building course till senior year).
natekelsey wrote:
Well I've entered in the constructor as:
1
2
3
4
5
Search::Search(int size, int seed)
{
  size=size;
  seed=seed;
}
You didn't read what I said at all. This does literally nothing at all - it assigns two variables to themselves.

http://ideone.com/cGO1PY

As for your error, it is because you are reading uninitialized memory - you never initialized your array in your constructor.
Last edited on
Sorry L B. When I was typing my post I didn't see your's. My apologies.
So it should look like this:
1
2
3
4
Search::Search(int size, int seed) : size(size), seed(seed)
{
	double *array = new double[size];
}


So I just need set functions for the size and seed, in order to define the size() and seed()? This is all new to me and none of my profs so far have taught this type of coding.
Last edited on
Close, you're accidentally declaring a new variable called array that exists only in the constructor, rather than using the existing array member. Just remove the "double *" text that is in front of "array".
Hey thanks for the help on most of this. Do I need to declare anything for the size(size) and seed(seed)? Or would set_seed(int seed) work for seed and something similar work for size?
I'm working on my initialize_array(). Currently getting this error message:

Windows has triggered a breakpoint in search.exe.

This may be due to a corruption of the heap, which indicates a bug in search.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while search.exe has focus.

The output window may have more diagnostic information.


The source of the error is located somewhere in this function:
1
2
3
4
5
6
7
8
9
10
void Search::initialize_array()
{
	int i=0;
	srand(seed);
	for(i; i<=getSize();i++)
	{
		double number = rand() % 1000;
		array[i]=number;  // I think it has something to do with this line
	}
}
Pages: 123