not seeing the error

May 31, 2016 at 4:47pm
Trying to pass a pointer to a string via a function. Get the error (at bottom).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
#include<string>

using namespace std;

void PrintString( string * string_h)
{
	        cout<<string_h[2];
}

int main()
{

		string * x = 'red';
		PrintString(x);
		return 0;
}


..\src\main.cpp:14:16: error: invalid conversion from 'int' to 'std::string* {aka std::basic_string<char>*}' [-fpermissive]
May 31, 2016 at 5:03pm
closed account (48bpfSEw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include<iostream>
#include<string>

using namespace std;

void PrintString( string * string_h)
{
	        cout << string_h->c_str();
}

int main()
{    
		string *x = new string("red");
		PrintString(x);
		delete x;
			
		return 0;
}
May 31, 2016 at 5:16pm
Hi,

Pass the string by const reference:

1
2
3
4
void PrintString( const std::string&  string_h)
{
	        std::cout << string_h[2];
}


Try to avoid raw pointers with modern C++.

@Necip
No need to use new and delete, STL containers implicitly store their data on the heap. delete can cause problems: if an exception is thrown, delete is never reached, neither is the destructor.
May 31, 2016 at 8:02pm
Ok, got rid of the namespace (hard habit to break for me sometimes), added some further examples to facilitate my understanding. Sort of mystified that sometimes a ' will trigger an error where a " reconciles it, and sometimes the opposite occurs. For me it has been trial and error in these limited cases.

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



void PrintString(const std::string &string_h)
{
	std::cout<<"Index [3] of long sentence ="<<string_h[3];
	std::cout<<std::endl;
	char d = 't';
	int index = string_h.find(d);
	std::cout<<"character "<<d<<" found at index "<<index<<" of: "<<string_h<<std::endl;
	std::cout<<"Non-symbolic (magical number)"<<std::endl;
	index = string_h.find('s');
	std::cout<<"character "<<'s'<<" found at index "<<index<<" of: "<<string_h<<std::endl;
}

int main()
{
		std::string string_h;
		string_h= "red";
		char d = 'r';
		int index = string_h.find(d);
		std::cout<<"character "<<d<<" found at index "<<index<<" of: "<<string_h<<std::endl;
		std::cout<<"To function:"<<std::endl;
		string_h = "long sentence";
		PrintString(string_h);

		return 0;
}




Sorry about the excessive tabbing I cannot as of now find the place on my ide where I can change it. (Eclipse)
Last edited on May 31, 2016 at 8:05pm
Jun 1, 2016 at 12:00pm
Sort of mystified that sometimes a ' will trigger an error where a " reconciles it, and sometimes the opposite occurs.


' ' is for a solitary char, while " " is for strings:

1
2
char a = 'X';
std::string b = "Fred'; 

although with an array of char " " is used :

char[] c = "Ginger"

But one should avoid char arrays, unless it is required by some library function.

Also, be aware that lots of library functions return a std::size_t type as opposed to int For example std::string::find
Jun 1, 2016 at 3:10pm
Thank you very helpful. I was going crazy with the ' vs. "
Topic archived. No new replies allowed.