cannot convert "int" to "const char*" error

Aug 24, 2018 at 7:12am
Cannot convert "int: to "cons char*".Please explain why this error happens and how to rectify it. Iam using C++ 4.5.

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
  void delOutlet()
{
	char search[30], searchp[10], ch; int u, p, flag=0;
	ifstream fin;
	ofstream fout;
	outlet obj;
	fin.open("OUTLET.dat", ios::in|ios::binary);
	fout.open("temp.dat", ios::app|ios::binary);
	cout<<"\nName of outlet to be removed : ";
	gets(search);
	cout<<"Password : ";
	gets(searchp);

	while(fin.read((char*)&obj, sizeof(obj)))
	{
		u=strcmp(search, obj.retName());       //This line has the above mentioned error 
  		p=strcmp(searchp, obj.retPassword()); //This line has the above mentioned error 
		if(u==0&&p==0)
		{
			flag=1;
			continue;
		}
		else
		{
			fout.write((char*)&obj, sizeof(obj));
		}
	}
	remove("OUTLET.dat");
	rename("temp.dat", "OUTLET.dat");
	fin.close();
	fout.close();
	if(flag==0)
	{
		cout<<"Outlet not found!\nPress any key to continue...";
		getch();
	}
}
Last edited on Aug 24, 2018 at 7:14am
Aug 24, 2018 at 7:33am
Can you post the definition of outlet please.
Aug 24, 2018 at 7:35am
You need to show the definition of outlet otherwise we cannot tell what the return value of obj.outlet() or obj.retPassword() actually is.
Aug 24, 2018 at 7:47am
Definition of class outlet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class outlet
{
	char name[30], add[30], password[10];
	public:
		char retName()
		{
			return name[30];
		}
		char retPassword()
		{
			return password[10];
		}
		void getdata()
		{
			cout<<"\nEnter Details:\n";
			cout<<"Name            : ";
			gets(name);
			cout<<"Address         : ";
			gets(add);
			cout<<"Create password : ";
			gets(password);
		}
};
Last edited on Aug 24, 2018 at 7:48am
Aug 24, 2018 at 7:51am
Change to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class outlet
{
	char name[30], add[30], password[10];
	public:
		char *retName() // Note: *
		{
			return name[30];
		}
		char *retPassword() // Note: *
		{
			return password[10];
		}
		void getdata()
		{
			cout<<"\nEnter Details:\n";
			cout<<"Name            : ";
			gets(name);
			cout<<"Address         : ";
			gets(add);
			cout<<"Create password : ";
			gets(password);
		}
};
Last edited on Aug 24, 2018 at 7:52am
Aug 24, 2018 at 8:49am
Thank you!!!!!!
It worked...
Aug 24, 2018 at 9:41am
Can u explain what the error was?
Aug 24, 2018 at 12:06pm
the name of an array is a pointer.
so char name[30]…
char* retname = name; //ok, name IS a pointer


retname = name[2]; //not ok, name[2] is not a char *, its a char, and c++ wont let you do this. you need an address of if you want to pull from the middle:

retname = &(name[2]); //ok, this is a pointer again by taking the address.


also, char is a type of integer that happens to be 1 byte. So your error says you can't convert an int (char is an int type) to a pointer, which is a little confusing.
Aug 24, 2018 at 12:10pm
1
2
3
4
char retName()
{
	return name[30];
}

This returns a char. Specifically, it returns the 31st of name (which only has 30). So it's returning an undefined value.

1
2
3
4
char* retName()
{
	return name;
}

This returns a pointer to a char, which is how C strings are implemented. The actual pointer returned is the start of the array.

Array and pointers are kinda confusing. C++ inherits this from C. I posted my attempt to explain it here: http://www.cplusplus.com/forum/general/70081/#msg373940
Last edited on Aug 24, 2018 at 12:19pm
Topic archived. No new replies allowed.