试图重载<<操作员,得到错误

时间:2015-11-14 06:48:46

标签: c++ operator-overloading

我正在重载<<我的MyVector类的运算符。这一切对我来说都是正确的但我在尝试运行时遇到错误。我做错了什么?

第一个错误是(我认为)“'之前的意外令牌';' “对于我的.h文件中的第84行。 (我说“我认为”是因为我不小心按描述对错误进行了排序,并且不知道如何将其恢复到默认排序。如果你也可以告诉我如何做到这一点,我将不胜感激。)

感谢您的时间!

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;

}

1 个答案:

答案 0 :(得分:0)

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

我假设这是有问题的一行。您需要将std::放在ostream前面,以便知道在哪里查找它。虽然您将using namespace std;放在实现文件中,但这不会影响标题。

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

另一方面,don't use using namespace std;