0

I have a function Readf I'm trying to call to fill in an array inside each constructor, I tried only with the default constructor with a code statement ReadF(cap,*point,counter);.

The second argument is what's giving me a problem in figuring out, I would get an error with '&' beside it or ' '. And I get an external error with the '*', I'm still new to c++ so I'm not experienced in dealing with classes.

I have to use the ReadF member function, are there other ways instead of calling it to get the results I need.

Also some of the code for the functions I have might not be perfect, but I would like to deal with this problem first before I move on to another.

array.h:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class AR {
public:
    AR();
    AR(int);
    AR(const AR&);
    //~AR();

    void ReadF(const int, string&, int);   
    AR& operator=(const AR&);

    friend ostream& operator<<(ostream&, AR&);
    friend ifstream & operator>>(ifstream&, AR&);

private:
    string* point;
    int counter;
    int cap;
};

array.cpp:

#include "array.h"


AR::AR() {
    counter = 0;
    cap = 2;
    point = new string[cap];
}

AR::AR(int no_of_cells) {
    counter = 0;
    cap = no_of_cells;
    point = new string[cap];
}

AR::AR(const AR& Original) {
    counter = Original.counter;
    cap = Original.cap;
    point = new string[cap];

    for(int i=0; i<counter; i++) {
        point[i] =Original.point[i];
    }
}

// AR::~AR() {
//  delete [ ]point;
//}

ostream& operator<<(ostream& out, AR& Original) {
    cout << "operator<< has been invoked\n";
    for (int i=0; i< Original.counter; i++) {
        out << "point[" << i <<"] = " << Original.point[i] << endl;
    }
    return out;
}

AR& AR::operator=(const AR& rhs) {
    cout << "operator= invoked\n";
    if (this == &rhs)
        return *this;
    point = rhs.point;
    return *this;
}

void ReadF(const int neocap, string& neopoint, int neocounter) {

    ifstream in;
    in.open("sample_strings.txt"); //ifstream in;  in.open("sample_data.txt");

    if (in.fail())
        cout<<"sample_data not opened correctly"<<endl;

    while(!in.eof() && neocounter < neocap) {
        in >> neopoint[neocounter];
        neocounter++;
    }

    in.close();
}


ifstream& operator>>(ifstream& in, AR& Original) {
    Original.counter = 0;

    while(!in.eof() && Original.counter < Original.cap) {
        in >> Original.point[Original.counter];
        (Original.counter)++;
    }

    return in;
}
Adam Burry
  • 1,786
  • 11
  • 20
Ron
  • 21
  • 1
  • 4
  • Can you quote the errors verbatim, please? – Cort Ammon Sep 15 '13 at 23:11
  • Is there a *good* reason why you're *not* using a `std::vector point;` in your `AR` class ?, because I don't see one. For that matter, using it *instead* of the `AR` class entirely. And as you have been [told now at-least-twice](http://stackoverflow.com/questions/18817999/c-getting-a-no-operator-matches-these-operands-for-a-function-that) in your prior post, `while(!in.eof()...)` is ***not correct***. – WhozCraig Sep 15 '13 at 23:15
  • I was given the while loop in class by the professor, what's wrong with it? – Ron Sep 15 '13 at 23:51
  • if you want to read more on why WhozCraig says `while(!in.eof()..)` is wrong then read this SO question (http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – miltonb Sep 15 '13 at 23:53
  • I think you need to post a `main()` that shows what you are trying to do. – Adam Burry Sep 16 '13 at 00:25

1 Answers1

2

It appears you are missing AR:: in the ReadF definition.

On second thought you are reading into an index of neopoint that likely isn't allocated yet. You could change ReadF to read:

void ReadF(const int neocap, string& neopoint,int neocounter)
{

    ifstream in;
    in.open("sample_strings.txt"); //ifstream in;  in.open("sample_data.txt");

    if (in.fail())
    {
        cout<<"sample_data not opened correctly"<<endl;
        return;
    }

    neopoint.resize(neocap);

    while(neocounter < neocap && in >> neopoint[neocounter])
    {
        neocounter++;
    }

    in.close();
}

Although you probably want to look into stringstream

std::ifstream in("sample_strings.txt");

if (in)
{
    std::stringstream buffer;
    buffer << in.rdbuf();
    in.close();
    inneopoint= buffer.str();
}
user1193
  • 121
  • 3