c++ help with class(new to classes)


 
Thread Tools Search this Thread
Top Forums Programming c++ help with class(new to classes)
# 1  
Old 02-04-2012
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.
# 2  
Old 02-05-2012
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 06:36 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Java Abstract Classes

Can anyone tell me if this is correct when creating classes in Java? public abstract class Animal { public class Point { public int x,y; } public class Animal { protected Point loc; protected String name; protected Random rng; String... (3 Replies)
Discussion started by: totoro125
3 Replies

2. Programming

C++ : Base class member function not accessible from derived class

Hello All, I am a learner in C++. I was testing my inheritance knowledge with following piece of code. #include <iostream> using namespace std; class base { public : void display() { cout << "In base display()" << endl; } void display(int k) {... (2 Replies)
Discussion started by: anand.shah
2 Replies

3. Programming

Size of Derived class, upon virtual base class inheritance

I have the two class definition as follows. class A { public: int a; }; class B : virtual public A{ }; The size of class A is shown as 4, and size of class B is shown as 16. Why is this effect ?. (2 Replies)
Discussion started by: techmonk
2 Replies

4. UNIX for Advanced & Expert Users

Get pointer for existing device class (struct class) in Linux kernel module

Hi all! I am trying to register a device in an existing device class, but I am having trouble getting the pointer to an existing class. I can create a class in a module, get the pointer to it and then use it to register the device with: *cl = class_create(THIS_MODULE, className);... (0 Replies)
Discussion started by: hdaniel@ualg.pt
0 Replies

5. Programming

static use for class inside the same class c++

Hi, I believe the next code is wrong: class Egg { Egg e; int i; Egg(int ii=0) : i(ii) {} }; because you would end up with an endless definition (memory allocation) of Egg objects, thus int i. Ok, so God Eckel proposes for a singleton: class Egg { static Egg e; int... (5 Replies)
Discussion started by: xavipoes
5 Replies

6. Programming

Separating two classes in two files

I have a file Map.hh shown below. I want to put the two classes Phase and Map in two different files Phase.hh and Map.hh. I have forward declaration before the Map class. How can I tackle this situation? ////////////////////////////////////////////////////////////////////////// #ifndef... (3 Replies)
Discussion started by: kristinu
3 Replies

7. Programming

C++ class definition with a member of the same class

Hi, i have a question about C++. Is it possible to declare a class with a member ot the same class? For example, a linked list or i want to convert this C code to C++ class (Elemento) typedef struct elemento { char name; char value; List<struct elemento> ltElementos; ... (7 Replies)
Discussion started by: pogdorica
7 Replies

8. Programming

Use of C++ Classes

I was wondering if I could put the section at the beginning rather than at the end before the definition of the class. const REAL ModMisfit::DefMinDT = 0.01; const REAL ModMisfit::DefSigma0 = 0.01; const double ModMisfit::DefDAngSh = 2; const REAL ModMisfit::DefKBeta = 5;... (2 Replies)
Discussion started by: kristinu
2 Replies

9. UNIX for Dummies Questions & Answers

car class (not school class)

im just trying to have some fun and kill some time writing a c++ program that has a person type in a car make and model then gives them a year and a price. or something like that. i always have problems getting it goin but once the ball is rolling im usually pretty good. anyone wanna help me out? ... (1 Reply)
Discussion started by: rickym2626
1 Replies

10. Programming

how to use classes in c ?!?!?

Hi, I've tried to use classes in my program, but the compiler simply gives an error on the word class . Am I the only one with this problem ? I have no idea how to use classes in c in linux environment(suse). If you've got any idea what should I do I would be very thankful. Thanks to ya all !... (4 Replies)
Discussion started by: atticus
4 Replies
Login or Register to Ask a Question