0

I am working on overloading the << operator for my MyVector class. It all looks correct to me but I am getting errors when I try to run it. What am I doing wrong?

The first error is (I think) "unexpected token(s) preceding ';' " for line 84 in my .h file. (I say "I think" because I accidentally sorted the errors by description and don't know how to get it back to the default sort. If you can also advise me on how to do that, I would appreciate it.)

Thank you for your time!

MyVector.h

#pragma once
#include <iostream>



class MyVector
{
private:

    //declaring variables
    int *myPointer;
    int vectorSize;
    int vectorCapacity;
    const int BASE_CAPACITY = 2;

public:

    //Default Constructor
    //Purpose: Create vector with default capacity of 2
    //Parameter: void
    //Return: None
    MyVector();

    //Parameterized Constructor
    //Purpose: Creates a vector of capacity "n"
    //Parameter: int
    //Return: None
    MyVector(const int);

    //Default Deconstructor
    //Purpose: Deletes any dynamically allocated storage
    //Parameter: void
    //Return: None
    ~MyVector();

    //Copy Constructor
    //Purpose: Copy the data of the vector
    //Parameter: a MyVector object
    //Return: None
    MyVector(const MyVector&);

    //Overloaded Assignment Operator
    //Purpose: Copy one vector to the other when = is used
    //Parameter: a MyVector object
    //Return: a MyVector object
    MyVector& operator=(const MyVector&);

    //The size Function
    //Purpose: returns the size of the vector
    //Parameter: void
    //Return: int
    int size() const;

    //The capacity Function
    //Purpose: returns the capacity of the vector
    //Parameter: void
    //Return: int
    int capacity() const;

    //The clear Function
    //Purpose: Deletes all elements from the vector and resets the size and capacity
    //Parameter: void
    //Return: None
    void clear();

    //The push_back Function
    //Purpose: adds an integer to the vector
    //Parameter: int
    //Return: None
    void push_back(const int);

    //The at Function
    //Purpose: returns the value of the element at position n
    //Parameter: int
    //Return: int
    int at(const int) const; 

    // Overloaded << operation
    // Purpose: Output a Vector
    // Parameter: an ostream and a vector
    // Return: ostream
    friend ostream& operator<<(ostream& out, const MyVector& rho);
};

MyVector.cpp

#include "MyVector.h"
#include <iostream>

using namespace std;


MyVector::MyVector()
{
    //create a vector with size 0 and capacity 2
    vectorSize = 0;
    vectorCapacity = BASE_CAPACITY;
    myPointer = new int[vectorSize];
}

MyVector::MyVector(int n)
{
    //create a vector of capacity n with the size 0
    vectorSize = 0;
    vectorCapacity = n;
    myPointer = new int[vectorSize];
}


MyVector::~MyVector()
{
    //check to see if 'myPointer' has a value and delete it
    if (myPointer != NULL)
    {
        delete[] myPointer;
        myPointer = NULL;
    }
}

MyVector::MyVector(const MyVector& b)
{
    if (b.myPointer != NULL)
    {
        vectorCapacity = b.vectorCapacity;

        vectorSize = b.vectorSize;

        myPointer = new int[vectorCapacity];

        for (int i = 0; i < vectorSize; i++)
        {

            myPointer[i] = b.at(i);

        }

    }
    else
    {

        delete[] myPointer;
    }


}

MyVector& MyVector::operator=(const MyVector& rho)
{
    //test for self assignment
    if (this == &rho)
        return *this;

    // clean up the vector on the left side
    delete[] this->myPointer;

    // create a new vector of the correct size and capacity
    vectorSize = rho.vectorSize;
    vectorCapacity = rho.vectorCapacity;
    this->myPointer = new int[vectorSize];

    // copy the data over
    for (int i = 0; i < vectorSize; i++)
    {
        this->myPointer[i] = rho.myPointer[i];
    }

    //return this object
    return *this;

}

int MyVector::size() const
{
    //return the size of the vector
    return vectorSize;
}

int MyVector::capacity() const
{
    //return the capacity of the vector
    return vectorCapacity;
}

void MyVector::clear()
{
    //clear the vector and reset it to a size of 0 and capacity of 2
    vectorSize = 0;
    vectorCapacity = BASE_CAPACITY;
    delete[] myPointer;
    myPointer = new int[vectorSize];
}

void MyVector::push_back(int addToVector)
{
    //this variable will be used to double the capacity
    const int DOUBLE_CAPACITY = 2;

    //check to see if the size of the vector has reached the capacity
    if (!(vectorSize < vectorCapacity))
    {
        //make sure the capacity of the vector is greater than 0
        if (vectorCapacity > 0)
        {
            vectorCapacity *= DOUBLE_CAPACITY;
        }
        else
        {
            //if vector capacity is 0 or less then the capacity equals 2
            vectorCapacity = BASE_CAPACITY;
        }

        //create a tempVector that will have double the capacity of the last vector.
        int *tempVector = new int[vectorCapacity];

        //copy the contents of the old vector to the tempVector
        if (myPointer != NULL)
        {
            for (int i = 0; i < vectorCapacity; i++)
            {
                tempVector[i] = myPointer[i];
            }
        }

        // delete the old array using the destructor
        MyVector::~MyVector();

        //set the pointer to the new tempVector
        myPointer = tempVector;
    }
    //add the passed in value to the vector
    myPointer[vectorSize] = addToVector;

    //increment the size of the vector
    vectorSize++;
}

int MyVector::at(int x) const
{
    //throw exception if index outside the range of the vector
    if (x > (vectorSize - 1))
    {
        throw x;
    }
    //return the value of the integer stored at index x
    return myPointer[x];
}

ostream& operator<<(ostream& out, const MyVector& rho)
{
    // output each value in the vector
    for (int i = 0; i < rho.size; i++)
    {
        out << rho.at(i) << " ";
    }

    // return the ostream
    return out;

}
M. Meacham
  • 121
  • 4

1 Answers1

0
friend ostream& operator<<(ostream& out, const MyVector& rho);

I'm assuming this is the line in question. You need to put std:: in front of ostream so it knows where to look for it. Although you put using namespace std; in the implementation file, that doesn't affect the header.

friend std::ostream& operator<<(std::ostream& out, const MyVector& rho);

On another note, don't use using namespace std;.

Community
  • 1
  • 1
Weak to Enuma Elish
  • 4,622
  • 3
  • 24
  • 36