class syntax error help

I am writing a program to store in a class a particular temp. I'm not very far along and am new to classes so I don't really understand this syntax error. The program is probably not very good but I'm just getting started and can't figure this out. The error is commented out. Thanks.

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
#include <iostream>
#include <cstdlib>
using namespace std;

class temp
{
	double kelvin;
	void setFahrenheit ();
	void setCelsius ();
	void setKelvin ();
	void getFahrenheit ();
	void getCelsius ();
	void getKelvin ();
};

int main ()
{
	char type;
	cout << "Enter the type of temperature: ";
	cin >> type;
	if (type == 'f' || type =='F')
	{
		temp.setFahrenheit ();  //expected an identifier and ; is missing before the period
	}
	
}
void temp::setFahrenheit()
{
	double celsius, fahrenheit;
	cout << "Enter the temperature in degrees: ";
	cin >> fahrenheit;
	celsius = (5/9)*(fahrenheit - 32);
	kelvin = celsius + 273.15;
}
Your problem is that you did not create a temp object none the less one called temp in your main function.

Also you shouldn't be allowed to or even try and name a object the same as the objects type. Ex Dont do int int or temp temp or string string.

Your main function should look like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main ()
{
        temp tmp; //Creating a temp object called tmp
	char type;
	cout << "Enter the type of temperature: ";
	cin >> type;
	if (type == 'f' || type =='F')
	{
		tmp.setFahrenheit ();  //The error was because you were trying
                //to create a temp object and call a function at same time
                //This would be the same as string.find(); or some function
	}
	
}
I am getting hit with an error saying that the function is inaccessible. I think I may have tried that before because I remember getting a similar error. My book isn't being very helpful nor the searches on the web.
Hi there,

Most likely this is because you have not declared any visibility on the class functions, which implies they are private (only visible within the class).

To correct this:

1
2
3
4
5
6
7
8
9
10
11
class temp
{
   public:
	double kelvin;
	void setFahrenheit ();
	void setCelsius ();
	void setKelvin ();
	void getFahrenheit ();
	void getCelsius ();
	void getKelvin ();
};


Hope that helps.

All the best,
NwN
That did it. Thanks.
Topic archived. No new replies allowed.