Hey Guys

I am trying to get a basic program to work with dynamic memory allocation.

I have a problem with a overload operating my stream operator...<<

On my previous program i used this:

ostream& operator<<(ostream& output, const Vector& p)
{
  output << "(" <<  p.x << ", " << p.y <<", " << p.z << ")";
  return output;
}

The new program is using a dynamically allocated array - what i am trying to do is:

void operator<<(float *p, int arraySize)
{
  cout << "(";
  for (int i = 0; i < arraySize; i++)
     cout << "(" <<  p[i];
  cout << ")";
}

Would this sort of thing work? Im assuming not as it obviously doesnt when i try running the program with this line:

const Vector v1;

   cout << "v1: " << v1 << endl;

The 2nd program i think should have 3 arguments for the function overload... the ostream object, the floater pointer to the array, and the array size... right? Could someone help me out?

Thank you ... your help is appreciated as always

You can't do this because too many parameters
ofstream& operator<<(ofstream& out, float *p, int arraySize)

But you can put that pointer into a structure and make the operator output the struct

struct arr
{
    float* p;
    int arraySize;
};

ofstream& operator<<(ofstream& out, struct arr* p)
{
}

Edited 3 Years Ago by happygeek: fixed formatting

Thank you... thats a very good point. I have everything in a class called Vector... so i should therefore have access to the size variable right? Note .... this function is set as a friend method of the class, should this be that way - im not exactly sure why, but for some reason i cant get it to work as a regualr method of class...

If i do what you said i get this as the function:

void operator<<(ostream& output, float *p)
{
  cout << "(";
  for (int i = 0; i < arraySize; i++)
     output << p[i];
  cout << ")";
}

Should i have this instead:
ostream& output, Vector x

Then the function would have access to all the classes variables etc - so that when i do this line:
for (int i = 0; i < arraySize; i++) i dont get an error on the arraySize being undeclared...

Thank you dragon for your help . awesome as always

I have got this part of the program working with the following:

ostream& operator<<(ostream& output, const Vector &vec)
{
  cout << vec.arraySize;
  cout << "(";
  for (int i = 0; i < vec.arraySize; i++)
     cout << vec.p[i];
  cout << ")";
}

By working - i mean i dont get any errors... but when i try to do this:

cout << "\nTest " << test++ << ": Array constructor and printing\n" << endl;

   float ar2[] = {1.0, 2.0, 3.0};

   const Vector v2(ar2, 3);

   cout << "v2: " << v2 << endl;

I get a large amout of randomness printed out, in what looks like memory addresses and such...and then the cmd window crashes... obviously not right... so here is what my program consists of. main.cpp, vector.h and vector.cpp

The class looks like:

class Vector
   { 
   friend ostream& operator<<(ostream& output, const Vector &);
   
   private:
        float *p;
        int arraySize,
            maxSize;
   public:
        Vector();
        Vector(float [], int);
        ~Vector();
        Vector(const Vector& otherObject);
        void clear();
   
   };

And the functions in question within the vector.cpp file are:
constructors (with no parameters)

Vector::Vector()
{
 p = NULL;
 arraySize = 0;
}

With 2 parameters - an array, then the int size of that array

Vector::Vector(float array[], int size)
{
  p = new float[size];
  for (int i = 0; i < size; i++)
    p[i] = array[size];
}

Above is the ostream function as is currently in the program ...

And that is all you should need i think to figure out the current problem i am having. I believe it is my ostream function that is incorrectly printing fake values taken at random from memory instead of the correct values given in the vector passed to it, but i may be wrong. I also think that the constructor for the 2 parameters might not create the dynamic array correctly - but this was pretty much copied out of text book.

Hope you guys can help once again. Thank you again in advance.

you have a couple problems

Vector::Vector(float array[], int size)
        {
            p = new float[size];
            for (int i = 0; i < size; i++)
                p[i] = array[i]; // << change size to i
            arraySize = size; // <<<<<<<<<<<< add this line
         }
This article has been dead for over six months. Start a new discussion instead.