Why am I getting an error

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 755 Views - Last Post: 31 December 2012 - 04:13 PM Rate Topic: -----

#1 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

Why am I getting an error

Posted 31 December 2012 - 08:30 AM

We are practicing friend functions this week. It seemed pretty easy when we were studying this but when I tried to compile it, it gave me an error and I have tried tweaking it but I am still at a lost.


//Lynette Wilkins
//Week 11
//Program that transers a guest from one room to another room

//Lynette Wilkins
//Week 8
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

class HotelRoom
{
	friend int Transfer(HotelRoom&, HotelRoom&);
private:
	char room_num[3]; //Character array that stores a 3-character room number
	char transroom_num;
	char* guest; //Character pointer,which stores the name of the guest occupying the room
	int room_cap;
	int occup_stat;
	double daily_rt;
	int tranfer_room;

public:
	HotelRoom(char room[], char* g_p, int roomcap, int occup, double rate = 89.00);
	~HotelRoom();
	void Display_Number();  //Displays room number and add the method Display_Guest()
	int Get_Capacity();
	int Get_Status();
	double Get_Rate();
	int Change_Status(int);
	double Change_Rate(double);
	void Display_Guest();

};

HotelRoom::HotelRoom(char room[], char* g_p, int roomcap,int occup, double rate )
{
	strcpy(room_num, room); //copy first argument into room_num[]
	guest = new char[strlen(g_p) + 1]; //reserve space for the guest name
	strcpy(guest, g_p); //copy fourth argument into new space
    room_cap = roomcap;
	daily_rt = rate;
	occup_stat = occup;

}

HotelRoom::~HotelRoom()
{
	cout << endl<<endl;
	cout << "Guest in room "<<room_num << " has checked out." <<endl;
	delete [] guest;
}

 void HotelRoom::Display_Number()
{
	cout << room_num;
}

int HotelRoom::Get_Capacity()
{
	return room_cap;
}

int HotelRoom::Get_Status()
{
	
	return occup_stat;
}



int HotelRoom::Change_Status(int occup)
{
	occup_stat = occup;

	if (occup > room_cap)
	{
		return -1;
	}
	else
	
	return occup_stat;
	
}

double HotelRoom::Get_Rate()
{
	return daily_rt;
}

double HotelRoom::Change_Rate(double rate)
{
	daily_rt = rate;
		return daily_rt;
}

int Transfer(char room, char transroom)

{
	

	room = transroom;
	return room;
}

void HotelRoom::Display_Guest()
{
	cout<< guest;
}
int main()
{
	cout<< setprecision(2)
		<<setiosflags(ios::fixed)
		<<setiosflags(ios::showpoint);
	
char room[4]; 
char buffer[100]; //temporarily stores guest name
int roomcap = 4;
char transroom[4];
int occup;
double rate = 89.00;


cout<<"\nEnter the room number: "<<endl;
cin.getline(room, 5);

cout<<"\nEnter the amount of guest to occupy this room: "<<endl;
cin>>occup;


cout<<"\nEnter the primary guest name: "<<endl;
cin.ignore();
cin.getline(buffer, 100);

cout<<"\nThe guest has decided to transfer rooms"<<endl;
cout<<"\nEnter the room to transfer the guest to"<<endl;
cin.getline(transroom,5);

HotelRoom room1(room, buffer, roomcap, occup, rate); //initialize the object

if (room1.Change_Status(occup) == -1)
{
	cout<<"You have exceeded the room capacity"<<endl;
}
else
{

cout <<"\nThe room number is ";
room1.Display_Number();
cout<<"."<<endl;
cout<<"\nThe name of the primary guest is ";
room1.Display_Guest();
cout <<"."<<endl;
cout<<"\nThe number of guest in the room is "<<room1.Change_Status(occup)<<"."<<endl;
cout<<"\nThe daily rate for room "<<room<< " is "<<room1.Get_Rate()<<"."<<endl<<endl;

cout<<"\nYou have tranferred the guest from room"<<room1.Display_Number()<<"to"<<Transfer(room, transroom)<<endl;
}





cout<<"\nRoom ";
room1.Display_Number();
cout<<" is vacant."<<endl;





system("PAUSE");

	
	return 0;
}




The error is saying

"(159): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)"

Any help would be greatly appreciated.

Thanks in advance!!

This post has been edited by llwilkins: 31 December 2012 - 08:32 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Why am I getting an error

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3643
  • View blog
  • Posts: 11,405
  • Joined: 05-May 12

Re: Why am I getting an error

Posted 31 December 2012 - 08:32 AM

It would help if you tell us what error you are seeing.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 4219
  • View blog
  • Posts: 13,227
  • Joined: 25-December 09

Re: Why am I getting an error

Posted 31 December 2012 - 08:40 AM

Quote

"(159): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)"

In the following line (159):
cout<<"\nYou have tranferred the guest from room"<<room1.Display_Number()<<"to"<<Transfer(room, transroom)<<endl;

You are trying to print the value returned from Display_Number() but that function is defined as:
void Display_Number();

You can't print something that doesn't exist (void).

Jim

This post has been edited by Skydiver: 31 December 2012 - 08:48 AM
Reason for edit:: Fixed code that Jim was referring to.

Was This Post Helpful? 0
  • +
  • -

#4 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

Re: Why am I getting an error

Posted 31 December 2012 - 09:04 AM

This is a program I am adding on to and this is the same thing that I have had for weeks and it was compiling correctly. It only stopped when i added the Transfer().

I will try removing the void and see what happens. Thanks Jim
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4219
  • View blog
  • Posts: 13,227
  • Joined: 25-December 09

Re: Why am I getting an error

Posted 31 December 2012 - 09:07 AM

The error is not with Transfer() but with Display_Number(). To check change your program to put both of these functions in a separate cout.
cout << "\nYou have tranferred the guest from room"<< room1.Display_Number();
cout << "to" << Transfer(room, transroom) << endl;


Your Display_Number() function is returning nothing, void, you can't print a void.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3643
  • View blog
  • Posts: 11,405
  • Joined: 05-May 12

Re: Why am I getting an error

Posted 31 December 2012 - 09:11 AM

And as for your Tranfer() method. Check your line 15 vs line 99. Your parameters are not the same. Also, why does Transfer() need to be a friend if it only take char types. Even if they were HotelRoom types, you don't seem to be doing anything that needs friend level access.
Was This Post Helpful? 1
  • +
  • -

#7 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

Re: Why am I getting an error

Posted 31 December 2012 - 09:14 AM

Ok I did what Jim told me to do and now it is giving me a new error
The error is:

(15): could be 'int Transfer(HotelRoom &,HotelRoom &)'
(99): or 'int Transfer(char,char)'
while trying to match the argument list '(char [4], char [4])'



//Lynette Wilkins
//Week 11
//Program that transers a guest from one room to another room

//Lynette Wilkins
//Week 8
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

class HotelRoom
{
	friend int Transfer(HotelRoom&, HotelRoom&);
private:
	char room_num[3]; //Character array that stores a 3-character room number
	char transroom_num;
	char* guest; //Character pointer,which stores the name of the guest occupying the room
	int room_cap;
	int occup_stat;
	double daily_rt;
	int tranfer_room;

public:
	HotelRoom(char room[], char* g_p, int roomcap, int occup, double rate = 89.00);
	~HotelRoom();
	char Display_Number();  //Displays room number and add the method Display_Guest()
	int Get_Capacity();
	int Get_Status();
	double Get_Rate();
	int Change_Status(int);
	double Change_Rate(double);
	void Display_Guest();

};

HotelRoom::HotelRoom(char room[], char* g_p, int roomcap,int occup, double rate )
{
	strcpy(room_num, room); //copy first argument into room_num[]
	guest = new char[strlen(g_p) + 1]; //reserve space for the guest name
	strcpy(guest, g_p); //copy fourth argument into new space
    room_cap = roomcap;
	daily_rt = rate;
	occup_stat = occup;

}

HotelRoom::~HotelRoom()
{
	cout << endl<<endl;
	cout << "Guest in room "<<room_num << " has checked out." <<endl;
	delete [] guest;
}

 char HotelRoom::Display_Number()
{
	cout << room_num;
}

int HotelRoom::Get_Capacity()
{
	return room_cap;
}

int HotelRoom::Get_Status()
{
	
	return occup_stat;
}



int HotelRoom::Change_Status(int occup)
{
	occup_stat = occup;

	if (occup > room_cap)
	{
		return -1;
	}
	else
	
	return occup_stat;
	
}

double HotelRoom::Get_Rate()
{
	return daily_rt;
}

double HotelRoom::Change_Rate(double rate)
{
	daily_rt = rate;
		return daily_rt;
}

int Transfer(char room, char transroom)

{
	
	room = transroom;
	return room;
}

void HotelRoom::Display_Guest()
{
	cout<< guest;
}
int main()
{
	cout<< setprecision(2)
		<<setiosflags(ios::fixed)
		<<setiosflags(ios::showpoint);
	
char room[4]; 
char buffer[100]; //temporarily stores guest name
int roomcap = 4;
char transroom[4];
int occup;
double rate = 89.00;


cout<<"\nEnter the room number: "<<endl;
cin.getline(room, 5);

cout<<"\nEnter the amount of guest to occupy this room: "<<endl;
cin>>occup;


cout<<"\nEnter the primary guest name: "<<endl;
cin.ignore();
cin.getline(buffer, 100);

cout<<"\nThe guest has decided to transfer rooms"<<endl;
cout<<"\nEnter the room to transfer the guest to"<<endl;
cin.getline(transroom,5);

HotelRoom room1(room, buffer, roomcap, occup, rate); //initialize the object

if (room1.Change_Status(occup) == -1)
{
	cout<<"You have exceeded the room capacity"<<endl;
}
else
{

cout <<"\nThe room number is ";
room1.Display_Number();
cout<<"."<<endl;
cout<<"\nThe name of the primary guest is ";
room1.Display_Guest();
cout <<"."<<endl;
cout<<"\nThe number of guest in the room is "<<room1.Change_Status(occup)<<"."<<endl;
cout<<"\nThe daily rate for room "<<room<< " is "<<room1.Get_Rate()<<"."<<endl<<endl;

cout<<"\nYou have tranferred the guest from room"<<room1.Display_Number()<<"to"<<Transfer(room, transroom)<<endl;
}





cout<<"\nRoom ";
room1.Display_Number();
cout<<" is vacant."<<endl;





system("PAUSE");

	
	return 0;
}




Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4219
  • View blog
  • Posts: 13,227
  • Joined: 25-December 09

Re: Why am I getting an error

Posted 31 December 2012 - 09:30 AM

First I get the following errors:

Quote

main.cpp||In constructor ‘HotelRoom::HotelRoom(char*, char*, int, int, double)’:|
main.cpp|40|error: ‘strcpy’ was not declared in this scope|
main.cpp|41|error: ‘strlen’ was not declared in this scope|
main.cpp||In member function ‘char HotelRoom::Display_Number()’:|
main.cpp|59|warning: no return statement in function returning non-void [-Wreturn-type]|
main.cpp||In function ‘int Transfer(char, char)’:|
main.cpp|99|warning: no previous declaration for ‘int Transfer(char, char)’ [-Wmissing-declarations]|
main.cpp||In function ‘int main()’:|
main.cpp|159|error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]|
main.cpp|159|error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]|
||=== Build finished: 4 errors, 2 warnings ===|


You need to include <cstring> to use the C-string functions.

Next why did you decide to return a single character from Display_Number()? And why are you not actually returning something from this function?

Now of the problems with Transfer(). First is this supposed to be part of your class? If so you need to properly qualify this function with the class name. Next this function is takes two parameters, these parameters will accept only one character each, not a "string".

I suggest you review your documentation on arrays and character strings. You may also want to study the function tutorials contained in my signature.

Jim
Was This Post Helpful? 1
  • +
  • -

#9 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

Re: Why am I getting an error

Posted 31 December 2012 - 09:42 AM

View PostSkydiver, on 31 December 2012 - 09:11 AM, said:

And as for your Tranfer() method. Check your line 15 vs line 99. Your parameters are not the same. Also, why does Transfer() need to be a friend if it only take char types. Even if they were HotelRoom types, you don't seem to be doing anything that needs friend level access.


I am doing this because this is what the instructions asked for us to do.

Skydiver you are right and I noticed that and changed it to all char and it is still giving me the same errors.

class HotelRoom
{
	friend char Transfer(HotelRoom&, HotelRoom&, char, char);
private:




char Transfer(char room, char transroom)

{
	
	room == transroom;
	return room;
}



Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 4219
  • View blog
  • Posts: 13,227
  • Joined: 25-December 09

Re: Why am I getting an error

Posted 31 December 2012 - 09:47 AM

Your parameters must match, they don't.

friend char Transfer(HotelRoom&, HotelRoom&, char, char);

Here you have 4 parameters.
char Transfer(char room, char transroom)

Here you have two parameters.

And a char can hold exactly one character.

Jim
Was This Post Helpful? 0
  • +
  • -

#11 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

Re: Why am I getting an error

Posted 31 December 2012 - 10:32 AM

See Jim we just have a book in this particular class the instructors does very little talking and it is very confusing. I have to really try to understand it as best as I know how by going to youtube and tutorials. Its sad that I am paying this much to have to teach myself. But if I am understanding correctly I need to get rid of both the HotelRoom&'s. This will leave me with just 2 parameters that will match the other two right?


//Lynette Wilkins
//Week 11
//Program that transers a guest from one room to another room

//Lynette Wilkins
//Week 8
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

class HotelRoom
{
	friend char Transfer( char[], char[]);
private:
	char room_num [4]; //Character array that stores a 3-character room number
	char transroom_num [4];
	char* guest; //Character pointer,which stores the name of the guest occupying the room
	int room_cap;
	int occup_stat;
	double daily_rt;
	int tranfer_room;

public:
	HotelRoom(char room[], char* g_p, int roomcap, int occup, double rate = 89.00);
	~HotelRoom();
	char Display_Number();  //Displays room number and add the method Display_Guest()
	int Get_Capacity();
	int Get_Status();
	double Get_Rate();
	int Change_Status(int);
	double Change_Rate(double);
	void Display_Guest();

};

HotelRoom::HotelRoom(char room[], char* g_p, int roomcap,int occup, double rate )
{
	strcpy(room_num, room); //copy first argument into room_num[]
	guest = new char[strlen(g_p) + 1]; //reserve space for the guest name
	strcpy(guest, g_p); //copy fourth argument into new space
    room_cap = roomcap;
	daily_rt = rate;
	occup_stat = occup;

}

HotelRoom::~HotelRoom()
{
	cout << endl<<endl;
	cout << "Guest in room "<<room_num << " has checked out." <<endl;
	delete [] guest;
}

 char HotelRoom::Display_Number()
{
	return room_num;
}

int HotelRoom::Get_Capacity()
{
	return room_cap;
}

int HotelRoom::Get_Status()
{
	
	return occup_stat;
}



int HotelRoom::Change_Status(int occup)
{
	occup_stat = occup;

	if (occup > room_cap)
	{
		return -1;
	}
	else
	
	return occup_stat;
	
}

double HotelRoom::Get_Rate()
{
	return daily_rt;
}

double HotelRoom::Change_Rate(double rate)
{
	daily_rt = rate;
		return daily_rt;
}

char Transfer(char room, char transroom)

{
	
	room = transroom;
	return room;
}

void HotelRoom::Display_Guest()
{
	cout<< guest;
}
int main()
{
	cout<< setprecision(2)
		<<setiosflags(ios::fixed)
		<<setiosflags(ios::showpoint);
	
char room[4]; 
char buffer[100]; //temporarily stores guest name
int roomcap = 4;
char transroom[4];
int occup;
double rate = 89.00;


cout<<"\nEnter the room number: "<<endl;
cin.getline(room, 5);

cout<<"\nEnter the amount of guest to occupy this room: "<<endl;
cin>>occup;


cout<<"\nEnter the primary guest name: "<<endl;
cin.ignore();
cin.getline(buffer, 100);

cout<<"\nThe guest has decided to transfer rooms"<<endl;
cout<<"\nEnter the room to transfer the guest to"<<endl;
cin.getline(transroom,5);

HotelRoom room1(room, buffer, roomcap, occup, rate); //initialize the object

if (room1.Change_Status(occup) == -1)
{
	cout<<"You have exceeded the room capacity"<<endl;
}
else
{

cout <<"\nThe room number is ";
room1.Display_Number();
cout<<"."<<endl;
cout<<"\nThe name of the primary guest is ";
room1.Display_Guest();
cout <<"."<<endl;
cout<<"\nThe number of guest in the room is "<<room1.Change_Status(occup)<<"."<<endl;
cout<<"\nThe daily rate for room "<<room<< " is "<<room1.Get_Rate()<<"."<<endl<<endl;

cout<<"\nYou have tranferred the guest from room"<<room1.Display_Number()<<"to"<<Transfer(room, transroom)<<endl;
}





cout<<"\nRoom ";
room1.Display_Number();
cout<<" is vacant."<<endl;





system("PAUSE");

	
	return 0;
}








I did this and now it is giving me another error about parameters and I have checked this and I am not understanding why this would be an error but because it was I changed it and that didn't work either.


char HotelRoom::Display_Number()
{
	return room_num;
}




It is saying NOW : (58): error C2440: 'return' : cannot convert from 'char [4]' to 'char'
1> There is no context in which this conversion is possible

Ok if that is the case then I tried to change the Display_Number (char[4]). This gave me a new error so what in the world is this compiler talking about?
Was This Post Helpful? 0
  • +
  • -

#12 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 1
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Why am I getting an error

Posted 31 December 2012 - 11:51 AM

As Jim clearly indicates, you don't understand functions and parameters. As long as you don't you are going to go round in circles.
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is offline

  • member icon


Reputation: 4219
  • View blog
  • Posts: 13,227
  • Joined: 25-December 09

Re: Why am I getting an error

Posted 31 December 2012 - 12:09 PM

In the following snippet:
char HotelRoom::Display_Number()
{
	return room_num;
}


What type of variable is room_num? Is it a char? I don't think so.
class HotelRoom
{
...
private:
	char room_num [4]; //Character array that stores a 3-character room number

It looks like it is an array of characters, not a char. A char can hold one and only one character. You should probably be returning a char pointer, not a char.
EDIT: I probably should also be a const char* as well.

char* HotelRoom::Display_Number()
{
	return room_num;
}



Can your room number contain characters other than numbers? If not then I would suggest using an int to hold this number. I take it you haven't studied std::string as of yet, correct?

Quote

you don't understand functions and parameters. As long as you don't you are going to go round in circles.

Actually it is worse than that, you don't seem to understand the differences between the different type of variables. You may want to also study up on them.


Jim

This post has been edited by jimblumberg: 31 December 2012 - 12:15 PM

Was This Post Helpful? 0
  • +
  • -

#14 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

Re: Why am I getting an error

Posted 31 December 2012 - 12:15 PM

I do understand that the parameters are variables that are declared and passed to the function.
Ex:
Here is a function that is passing along variables:

void Display_Num(int x, int y)

So if Display_Num is called in the main program like this

Display_Num(5,4) then 5 is been passed to x and 4 is been passed to y. I understand this part. My confusion is not even the matching because when I look at the Display_Num in my program that I am having trouble with they are matched up now. I was just assuming the HotelRoom& was doing something more because it was a friend function, but I truly am lost here because like I said it is not going in depth on friend functions in our book and the instructor is so caught up in his full time job that we very rarely get help that is beneficial. I am just looking for a little guidance so I can understand this better. So if anyone can tell me what is not matching up I will appreciate it and if it is matching up and I am doing something else wrong please let me know. I don't want to be told the answer I want to learn this.
Was This Post Helpful? 0
  • +
  • -

#15 llwilkins  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 06-November 12

Re: Why am I getting an error

Posted 31 December 2012 - 12:21 PM

View Postjimblumberg, on 31 December 2012 - 12:09 PM, said:

In the following snippet:
char HotelRoom::Display_Number()
{
	return room_num;
}


What type of variable is room_num? Is it a char? I don't think so.
class HotelRoom
{
...
private:
	char room_num [4]; //Character array that stores a 3-character room number

It looks like it is an array of characters, not a char. A char can hold one and only one character. You should probably be returning a char pointer, not a char.
EDIT: I probably should also be a const char* as well.

char* HotelRoom::Display_Number()
{
	return room_num;
}



Can your room number contain characters other than numbers? If not then I would suggest using an int to hold this number. I take it you haven't studied std::string as of yet, correct?

Quote

you don't understand functions and parameters. As long as you don't you are going to go round in circles.

Actually it is worse than that, you don't seem to understand the differences between the different type of variables. You may want to also study up on them.


Jim


Jim this is what was asked of us in a prior assignment and I just kept it. I would have preferred it to be an integer but they asked for a character. The teacher told us to put an array of 4 so that we could make sure that it accepted up to this many characters.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2