Coding Issue

I have been coding a program that works fine as long as I have everything with the .cpp file but when my professor wanted me to split the code into .ccp and .h files I am receiving a fatal error.

Recursive.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
 #include "Recursion.h"

int index_of(
	const string &userString,
	const string &searchString,
	const size_t index)
{
	//decleration of specific datamembers 
	int len2 = searchString.length();

	//compares search string to userString to see if the searchString is present and at what index
	//this checks the userString and begins by removing one from each index until it is zero then
	//resets and checks the other driection by adding one to index and verify
	//if no match is found resturns a -1 to indicate no match found  if one is found then returns the location ofr the first match
	if ((userString.length() - index) < searchString.length())
		return -1;
	else if (userString.substr(index, len2) == searchString)
		return index;
	else
		return index_of(userString, searchString, index + 1);
	return -1;
}

// Overloading, so you can call index_of with just
//two arguments
int hindex_of(const string &userString, const string &searchString)
{
	return index_of(userString, searchString, 0);
}


int main()
{
	//Declaration of string variables
	string userString = "";
	string searchString = "";

	//Ask the user for some string they like
	cout << "Enter a string that you would like to be analyzed: ";
	getline(cin, userString);
	cout << endl;

	//ask user for a string to find within the string they entered
	cout << "Enter the string you are trying to find:  ";
	getline(cin, searchString);
	cout << endl;

	hindex_of(userString, searchString);

	if (hindex_of(userString, searchString) == -1)
	{
		cout << "Sorry the string you entered as not found." << endl << endl;
	}
	else
	{
		cout << "The index location of " + searchString + " is: " << hindex_of(userString, searchString) << endl << endl;
	}


	system("PAUSE");
	return 0;
};


Recursion.h 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
#include <iostream>
#include <string>

using namespace std;


//Declaration of string variables
string userString = "";
string searchString = "";


/*
index_of function
Purpose: To load user entered string and search term and having datamember that can hold a varying index length
Parameters: string, string, int
Returns: location of instance for search string or error
*/
int index_of(string&, string&, size_t);


/*
index_of function
Purpose: To load user entered string and search term and having datamember that can hold a varying index length
Parameters: string, string, int
Returns: the name as a string
*/
int hindex_of(string&, string&);


Any help or advice is greatly appreciated.
Please copy and paste the exact error message and indicate which line it is referring to.
Ok it not your simply this line is a problem error but there is the compile error that I am receiving
[code
Error 1 error LNK2019: unresolved external symbol "int __cdecl hindex_of(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?hindex_of@@YAHAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z) referenced in function _main C:\Users\Gerald Wright\Documents\Visual Studio 2013\Projects\Project6\Project6\Recursion.obj Project6

][/code]

and this error
1
2
Error	2	error LNK1120: 1 unresolved externals	C:\Users\Gerald Wright\Documents\Visual Studio 2013\Projects\Project6\Debug\Project6.exe	1	1	Project6


thanks again
The protoype: int hindex_of(string&, string&);
The implementation:
1
2
3
4
int hindex_of(const string &userString, const string &searchString)
{
	return index_of(userString, searchString, 0);
}

const changes the signature of the function and that means they are not the same.

From the view of the compiler
int hindex_of(string&, string&);
is not implemented (cannot be linked)
Thanks for the face palm, I was working as if they were pointers rather than references. Made the following changes and the code now works as expected

Recursion.h
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 <iostream>
#include <string>

using namespace std;

//Declaration of string variables
string userString = "";
string searchString = "";

/*
index_of function
Purpose: To load user entered string and search term and having datamember that can hold a varying index length
Parameters: string, string, int
Returns: the name as a string
*/
int index_of(string &, string &, const size_t );

/*
index_of function
Purpose: To load user entered string and search term and having datamember that can hold a varying index length
Parameters: string, string, int
Returns: the name as a string
*/
int index_of(const string &, const string &);


Again thanks for the awesome help.
Unless you intend to modify the parameters, there is no reason to have line 16.
Topic archived. No new replies allowed.