Hello i am trying to construct a class for the first time...its called Field and is very similar to a vector in that it tries to store an array of double.. i am not able to compile the code..i get the error that field.h does not exist..pls see if u r able to compile it..

//*********************Field.h
#ifndef CLASS_Field
#define CLASS_Field

#include <iostream>
#include <cmath>
using std::cout;
using std::endl;
using namespace std;


class Field {
      public:
             Field (unsigned int);
             Field (const Field&);
             ~Field();
             
             Field& operator= (const Field&);
             double & operator[]; (constant unsigned int);
             
             bool operator== (const Field&);
             bool operator!= (const Field&);
             
             Field operator+ (const Field&);
             Field operator- (const Field&);
             Field operator* (const double&);
             Field operator/ (const double&);
      
      void print ();
      };
      
#endif


//**************Field.cpp
#include "Field.h"

//constructor

Field::Field(unsigned int N) {
             if (N == 0) {
                   cout << "Value must be a positive integer" << endl;
                   exit(-1);                
                          }
             size = N;
             data = new double [size];
             
                             }
             
 //copy constructor
 Field::Field(const Field& F) {
              size = F.size;
              for (unsigned int i = 0; i< size; ++i) {
                  data[i] = F.data[i];
                  
                  }
              } 
//destructor
Field::~Field () {
              delete[] data;
              }
              
              
//Assignment operator

Field& Field::operator= (const Field& F) {
       if (F.size != size) {
                  delete[] data;
                  size = F.size;
                  data= new double [size];
                  }
                 for (unsigned int i = 0; i < size; ++i){
                 data[i] = F.data[i];    
                     } 
                return *this,  
       }

// Array index operator
double& Field::operator [] (const unsigned int index) {
        if (index >= size) {
                  cout << "index is out of range" << endl;
                  exit(-1);
                  }
                  return data[index];
        }
        
// Comparison operator
bool Field::operator== (const Field& F) {
     if (F.size != size) {
                return false;
                }
                for (unsigned int i = 0; i < size; ++i) {
                    if (F.data[i] != data[i]) {
                                  return false;
                                  }
                              }
                              return true;
     }
     
bool Field::operator!= (const Field& F) {
     return !operator==(F);
     }
     
//Arithmetic operators
//Addition
Field Field::operator+ (const Field& F) {
      if (F.size != size) {
                 cout << "Fields are of different sizes!" << endl;
                 exit(-1);
                 }
      Field T = *this;
      for (unsigned int i = 0; i<size; ++i) {
          T[i] += F.data[i];
          }
          return T;
          }
//Subtraction
Field Field::operator- (const Field& F) {
      if (F.size != size) {
                 cout << "Fields are of different sizes!" << endl;
                 exit(-1);
                 }
      Field T = *this;
      for (unsigned int i = 0; i<size; ++i) {
          T[i] -= F.data[i];
          }
          return T;
}
          
//Multiplication
Field Field::operator* (const double& d) {
      if (F.size != size) {
                 cout << "Fields are of different sizes!" << endl;
                 exit(-1);
                 }
      Field T = *this;
      for (unsigned int i = 0; i<size; ++i) {
          T[i] *= d;
          }
          return T;
}
          
          
//Divide
Field Field::operator/ (const double& d) {
      if (F.size != size) {
                 cout << "Fields are of different sizes!" << endl;
                 exit(-1);
                 }
      Field T = *this;
      for (unsigned int i = 0; i<size; ++i) {
          T[i] /= d;
          }
          return T;
}
          
//To print out Field as a column vector
void Field::print() {
     for (unsigned int i; i<size; ++i){
         cout << data[i] << endl;
         }
}


//********************main.cpp
#include "Field.h"
int main() {
    unsigned int N = 5;
    Field a(N);
    for (unsigned int i; i<N; ++i) {
        a[i] = i + 1;
        }
    cout << "Field a:" << endl;
    a.print();
    
    Field b = a;
    cout << "Field b:" << endl;
    b.print();
    
    if (a==b) {
              cout << "fields a and b are the same" << endl;
              
              }
              
    Field c(N);
    c = a + (b * 0.5);
    cout << "Field a + (b*0.5)" << endl;
    c.print();
    
    system("pause")
    return 0;
}

.i get the error that field.h does not exist..

It's reflected properly in the code that you've put up but if that's your exact error message than you've used the wrong case (f versus F) in one of your cpp files.

One of the main problems you'll face after that is you forgot to declare your private member variables in the header (e.g., size and data).

Take a look at the following declaration and see if you can spot the two things wrong with it: double & operator[]; (constant unsigned int); Also, if you have

using std::cout;
using std::endl;
using namespace std;

the third line makes the first 2 redundant.

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.