Go Back   The UNIX and Linux Forums > Top Forums > Programming
Search Forums:



Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-04-2012
Registered User
 

Join Date: Feb 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
c++ help with class(new to classes)

Hello there, I am new to using classes, and have been having so many problems. I don't want to go to my teacher if I don't have to, because it is always my luck that it is something easy that I just overlooked somehow. I have been working on this for 3 days and I can't get it to read from a file.

Here is my header file:

Code:
#include <iostream>
#include <fstream>
using namespace std;

ifstream fin;

class cMonthlyRainfall
{
    private:
        float   *rainfall,
                *hi_temp,
                *lo_temp,
                *average_temp;

    public:
        cMonthlyRainfall ();
        ~cMonthlyRainfall();
        void    set_values (cMonthlyRainfall &month, ifstream &fin);
};

Here is the cpp file attached to the header file:

Code:
#include <iostream>
#include "cMonthlyRainfall.h"

cMonthlyRainfall::cMonthlyRainfall()
{

}

void cMonthlyRainfall::set_values (cMonthlyRainfall &month, ifstream &fin)
{
    int i;

    for (i = 0; i < 12; i++)
    {
        fin >> month[i].rainfall;
        fin >> month[i].hi_temp;
        fin >> month[i].lo_temp;

        month[i].average_temp = (month[i].hi_temp + month[i].lo_temp) / 2;
    }       //End for loop
}       //End function set_values()

The errors that I am receiving are attached to that cpp file and the error codes are:

Code:
C:\Users\KingAroan\Desktop\CodeBlocks\Rainfall\cMonthlyRainfall.cpp||In member function 'void cMonthlyRainfall::set_values(cMonthlyRainfall&, std::ifstream&)':|
C:\Users\KingAroan\Desktop\CodeBlocks\Rainfall\cMonthlyRainfall.cpp|23|error: no match for 'operator[]' in 'month[i]'|
C:\Users\KingAroan\Desktop\CodeBlocks\Rainfall\cMonthlyRainfall.cpp|24|error: no match for 'operator[]' in 'month[i]'|
C:\Users\KingAroan\Desktop\CodeBlocks\Rainfall\cMonthlyRainfall.cpp|25|error: no match for 'operator[]' in 'month[i]'|
C:\Users\KingAroan\Desktop\CodeBlocks\Rainfall\cMonthlyRainfall.cpp|27|error: no match for 'operator[]' in 'month[i]'|
C:\Users\KingAroan\Desktop\CodeBlocks\Rainfall\cMonthlyRainfall.cpp|27|error: no match for 'operator[]' in 'month[i]'|
C:\Users\KingAroan\Desktop\CodeBlocks\Rainfall\cMonthlyRainfall.cpp|27|error: no match for 'operator[]' in 'month[i]'|
||=== Build finished: 6 errors, 0 warnings ===|

And finally here is my main.cpp file:

Code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include "cMonthlyRainfall.h"
using namespace std;

int main()
{
    ifstream fin;
    int count;
    cMonthlyRainfall    month[12];   //Creates an object for each month

    fin.open ("weather.dat");

    if (!fin)
    {
        cout << "ERROR: Unable to find weather.dat";
        return 1;
    }

    cMonthlyRainfall::set_values(month, fin)

    return 0;
}

I am sure the errors are connected to the array but I am not sure since I haven't worked with a class before. I have been googling and reading the book but I can't figure it out. Thanks for any help.
Sponsored Links
    #2  
Old 02-05-2012
Moderator
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 12,127
Thanks: 230
Thanked 1,686 Times in 1,618 Posts
The error has nothing to do with it being a class or not...

You are passing it as a reference(&), which implies a single object. You should be passing it as a pointer(*) which implies one or more objects in a row -- i.e. a pointer to an object, or a pointer to several objects. You don't need to take the address of the array itself -- the array itself already is an address, which the [] operator converts into a direct access..

---------- Post updated at 04:38 PM ---------- Previous update was at 04:15 PM ----------

Why have you declared all your members as pointers, by the way? You'll have to allocate them or make them point to something before you can use them -- being pointers, they are useless until you give them something to point to. If you just want them to hold one value per object, remove the * and just let them be floats.

Last edited by Corona688; 02-05-2012 at 05:36 PM..
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Separating two classes in two files kristinu Programming 3 10-14-2011 10:31 AM
C++ class definition with a member of the same class pogdorica Programming 7 06-21-2010 04:26 AM
Grep character classes '\w' '\d' glev2005 UNIX for Advanced & Expert Users 4 02-28-2010 09:40 PM
Use of C++ Classes kristinu Programming 2 01-25-2010 10:46 AM
how to use classes in c ?!?!? atticus Programming 4 05-19-2007 10:06 PM



All times are GMT -4. The time now is 12:18 AM.