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

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
Can you post the definition of outlet please.
You need to show the definition of outlet otherwise we cannot tell what the return value of obj.outlet() or obj.retPassword() actually is.
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
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
Thank you!!!!!!
It worked...
Can u explain what the error was?
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.
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
Topic archived. No new replies allowed.