I am little confused, maybe a little overwhelmed because we are just getting into the various storage classes and scopes, and also arrays. My biggest issue is getting my functions and data members recognized in the separate files. I was able to do this with a project I had on an array.

But now I am struggling with another issue: getting my objects recognized. And also, must I always have an object? Because an object is part of the OOA/OOD?

My program deals with just printing charts showing the Fahrenheit and Celsius equivalents. Here is my error:

.cpp(33) : error C2228: left of '.displayFahrenheit' must have class/struct/union
.cpp(34) : error C2228: left of '.displayCelsius' must have class/struct/union

#include "Temperatures.h"

int main()
{
	// create the object
	Temperatures degrees();

	// display the temperatures 
	degrees.displayFahrenheit();
	degrees.displayCelsius();

	cout << endl;
	return 0;		
}

Also, here are my function definitions:

#include <iostream>
using namespace std;	
 
#include <iomanip>	
using std::setw;	

#include "Temperatures.h"			

// function fahrenheit returns the Fahrenheit equivalent of a Celsius temperature
double Temperatures::fahrenheit(int countCelsius)
{
 return (((countCelsius * 9.00) / 5.00) + 32);
}

// function celsius returns the Celsius equivalent of a Fahrenheit temperature 
double Temperatures::celsius(int countFahrenheit)
{
 return ((countFahrenheit - 32) / (5.00/9.00));
}

// function displayFahrenheit  
void Temperatures::displayFahrenheit()
{ 
 cout << "Fahrenheit equivalents for Celsius 0 to 100 degrees are: \n";
 
 for(int counter = 0; counter <= 100; counter++)
 {
  cout << setprecision(2) << showpoint << fixed << setw(5) << fahrenheit(counter);
 
  if(counter % 10 == 0)
   cout << endl;
 }
} 

// function displayCelsius
void Temperatures::displayCelsius()
{
 cout << "\nCelsius equivalents of Fahrenheit 32 to 212 degrees: \n";
 
 for(int counter2 = 32; counter2 <= 212; counter2++)
 {
  cout << setw(5) << celsius(counter2);
 
  if(counter2 % 10 ==0)
   cout << endl;
 }
}

Recommended Answers

All 4 Replies

>Temperatures degrees();
You stumbled on an annoying and subtle problem with C++'s declarations. What this does is declares a function called degrees that takes no parameters and returns an object of type Temperatures. Remove the parentheses:

Temperatures degrees;

>Temperatures degrees();
You stumbled on an annoying and subtle problem with C++'s declarations. What this does is declares a function called degrees that takes no parameters and returns an object of type Temperatures. Remove the parentheses:

Temperatures degrees;

So in this case - I don't need a constructor or set functions because I don’t have any data members? I was thinking I did not need any data members because I do not need input and I am not trying to store anything.

>I don't need a constructor or set functions because I don’t have any data members?
Huh? I'm not sure I understand where you're going with this. Your error was caused by a syntax problem. Removing the parentheses causes the code to work like you originally expected in that it creates an object of Temperatures and calls the default constructor.

I see - the data members are just information for the object that I created, and in this case there are none. And since I did not create a constructor, the default constructor was called.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.