return type specification for constructor invalid
Mar 7, 2011 at 4:25am UTC
When I try to run this program, I'm told "return type specification for constructor invalid". I've got my semicolon after the end of the class, so I'm not sure what I'm doing wrong.
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
//Create class Year
class Year
{
public :
Year(int = 0);
void GetData();
void FindMax();
void Average();
void DisplayValues();
private :
int current_year, max_index,i;
float year_avg, month_avg[12];
};
int main()
{
int a;
cout << endl << "Enter any year between 1980 and 2007: " ;
cin >> a;
Year y1(a);
y1.GetData();
y1.FindMax();
y1.Average();
y1.DisplayValues();
}
//Constructor member function
void Year::Year(int y)
{
max_index=0;
year_avg=0;
current_year=y;
for (i=0;i<12;i++)
{ month_avg[i]=0;}
i=0;
}
void Year::GetData()
{
ifstream data ("co2data.txt" );
int year=(current_year-1980)*12;
char alldata[324][16];
for (i=0;i<324;i++)
{
data.getline(alldata[i],16);
}
for (i=0;i<12;i++)
{
month_avg[i]=atof(alldata[year+i]);
}
}
void Year::FindMax()
{
for (i=0;i<12;i++)
{
if (month_avg[max_index] < month_avg[i])
{
max_index=i;
}
}
}
void Year::Average()
{
float month_total=0;
for (i=0;i<12;i++)
{
month_total+=month_avg[i];
year_avg=month_total/12;
}
}
void Year::DisplayValues()
{
cout << "In" << current_year << ", the average CO2 concentration at Mauna Loa was " << year_avg << " ppm.\nThe maximum CO2 reading was " << month_avg[max_index] << "ppm in " << current_year << current_year << endl;
}
Mar 7, 2011 at 4:44am UTC
Line 34 should not have a return type on the constructor.
Mar 7, 2011 at 4:45am UTC
I'm sorry, I haven't worked with C++ much before. Could you explain?
Mar 7, 2011 at 6:36am UTC
He means you need to remove the "void" keyword from Line 34. Constructors do not have return types.
Mar 7, 2011 at 1:18pm UTC
That did the trick :) Thanks!
Topic archived. No new replies allowed.