-4

I have to make a class that will make arrays act like vectors. When I try and pass the class into the method into my main I get an error telling me that "[" and "]" are incorrect operators. I was wondering if I'm just completely doing this wrong or if it's just a simple mistake. Help is greatly appreciated. Here is my header file:

#ifndef PROGRAM5HEADER_H
#ifndef PROGRAM5HEADER_H
#define PROGRAM5HEADER_H
#include <string>

        using namespace std;

class FloatArray
{
        int *rep;
        int _size;
public:

FloatArray(int sz=100):_size(sz)
  {
        rep=new int[sz];
  }
~FloatArray()
  {
        delete [] rep;
  }
int size() const
{
     return _size;
}
FloatArray(const FloatArray& x)
  {
        copy(x);
  }
void copy(const FloatArray& x)
  {
        _size == x.size();
        rep=new int[_size];
        for(int k=0;k<_size;k++)
                rep[k]=x.rep[k];
  }




    };

    #endif

and here is my main program

#include <iostream>
#include <string>
#include <cstdlib>
#include "program5header.h"
#include <cmath>
        using namespace std;

int meanstd(FloatArray x, int& std)
{
        int sx=0,sx2=0,mean;
        for(int i=0;i<x.size();i++)
        {
           sx+=x[i];
           sx2+=x[i]*x[i];
        }
   mean=sx/x.size();

 std=sqrt(sx2/x.size()-mean*mean);
return mean;
}
int main()
{       int f;
        cout<<"How big of an array would you like: "<<endl;
        cin>>f;
        FloatArray x(f);



}
Drogo
  • 1
  • 2
  • Be nice to see the full error with line number. – Richard Critten Apr 30 '15 at 20:40
  • program5main.cpp:13:10: error: no match for âoperator[]â (operand types are âFloatArrayâ and âintâ) sx+=x[i]; ^ program5main.cpp:14:11: error: no match for âoperator[]â (operand types are âFloatArrayâ and âintâ) sx2+=x[i]*x[i]; ^ program5main.cpp:14:16: error: no match for âoperator[]â (operand types are âFloatArrayâ and âintâ) sx2+=x[i]*x[i]; ^ – Drogo Apr 30 '15 at 20:40
  • 4
    You never implemented `operator[]`. Read up on [operator overloading](http://en.cppreference.com/w/cpp/language/operators). – Dark Falcon Apr 30 '15 at 20:40
  • I'm confused on where I would do that then... – Drogo Apr 30 '15 at 20:42
  • but thank you I will :) – Drogo Apr 30 '15 at 20:42
  • Like Dark Falcon said, you never implemented `operator[]`. See `sx += x[ i ]`, do you mean `x.rep[ i ]`? Because the only possible array I see is `int * rep` inside your class. If you want an array of _CLASSES_, you should set `FloatArray x` to `FloatArray * x`. That should allow you to pass in an array – NukingDragons Apr 30 '15 at 20:43
  • 2
    Btw, I seriously doubt this: `_size == x.size()` does what you intended. Turn up your warning levels to pedantic. – WhozCraig Apr 30 '15 at 20:47
  • @Drogo Also have a look at [What is The Rule of Three?](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – πάντα ῥεῖ Apr 30 '15 at 20:52
  • 2
    You might want to consider matching the class name with the type of array it actually is. – chris Apr 30 '15 at 20:54

1 Answers1

0

There are a lot of issues with a lot of your implementation, I'd suggest doing some research on the subject. I'll touch on a few.

Firstly, you should make your FloatArray a templated class and allow for different types other than just int.

When you initialize a FloatArray x and then try to access it's underlying array through "[]" you are actually invoking the following:

x.operator[](...)

You haven't defined the '[]' operator on your FloatArray class so you are getting an error.

You need something similar to this:

int FloatArray.operator[](int index) {
  assert(index < _size);
  return _rep[index]
}

Your copy isn't doing what you want, it's not copying the size over to "this". It should look something similar to this:

void copy(const FloatArray& x)
{
  _size = x._size;
  rep=new int[_size];
  for(int k=0;k<_size;k++)
    rep[k]=x.rep[k];
}

However I would suggest not having a copy method and instead implement everything in your copy constructor.

ZachSand
  • 143
  • 8
  • 1
    There's more things wrong with the code presented in the OP. They'll come back at you after fixing this particular issue, and force you to fix all of their remaining problems (just like like sucking off your heart blood). You really should consider to retain from answering like this (fixing the 1st stage problems). – πάντα ῥεῖ Apr 30 '15 at 20:53