Hello all,

I am getting the following linker error when I try to pass an object of type "string" to a function. Otherwise, the code compiles correctly.

exercise_3.10.obj : error LNK2019: unresolved external symbol "void __cdecl reshuffle(unsigned int,unsigned int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?reshuffle@@YAXIIV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main

Here is my code:

#include <iostream>
#include <conio.h>
#include <string>
#include <cctype>

void reshuffle (std::string::size_type,std::string::size_type,std::string);

int main()
{
	std::string s;
	std::string::size_type index(0);
	std::string::size_type size(0);
	std::cout << "Please, enter a sentence with full "
		  << "punctuation:\n\n";

	getline(std::cin,s);

	size = s.size();

	for (index = 0; index != size; ++index)
	{
		if (isspace(s[index]))
		{
			if (isspace(s[index + 1]))
			{
				reshuffle(index,size,s);
			}
		}

		if (ispunct(s[index]))
		{
			reshuffle(index,size,s);
		}
	}

	std::cout << "\nThe sentence you entered with "
			  << "punctuation removed is:\n\n"
			  << s << std::endl;

	
	
	std::cout << std::endl << std::endl << std::endl;
	std::cout << "Press any key to exit.\t";
	getch();
	return 0;
}


void reshuffle(std::string::size_type &x,std::string::size_type &y,std::string &z)
{
	std::string::size_type aux_index(0);

	for (aux_index = x; aux_index != y; ++aux_index)
	{
		z[aux_index] = z[aux_index + 1];
	}

	--y;
	--x;
}

Now, I understand this can be done much more easily by just using the "erase" member function but that's not the point. I want to learn how to pass "string" objects to functions by reference correctly. I hope you can help me find out what I am doing wrong. Thanks very much in advance.

Martin

I think it is because your declaration does not take the variables by reference.

Try changing

void reshuffle (std::string::size_type,std::string::size_type,std::string);

to

void reshuffle (std::string::size_type&,std::string::size_type&,std::string&);

Thank you David. Where was I looking - didn't see that at all :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.