重载插入操作符失败

时间:2015-03-28 21:25:12

标签: c++ memory-management overloading operator-keyword insertion

我不确定我在这里做错了什么,但我使用谷歌并发现几乎相同的代码,但他们没有声称错误。我们的任务是重载插入操作符。问题是我每次通过SAM或JOE时都会在驱动程序中出错。错误状态:错误2错误C2679:二进制'<<' :找不到哪个操作符采用了'MyVector'类型的右手操作数(或者没有可接受的转换)。 任何帮助表示赞赏。请注意,我还没有完成这项任务,如果其他功能似乎不正确,可能是因为我没有完成它。 提前致谢。

Driver.cpp

#include <iostream>
#include "MyVector.h"     
using namespace std;

// the printV function
// used to test the copy constructor
// parameter: a MyVector object
void printV(MyVector);

int main()
{
    cout << "\nCreating a vector Sam of size 4.";
    MyVector sam(4);

    cout << "\nPush 12 values into the vector.";
    for (int i = 0; i < 12; i++)
        sam.push_back(i);

    cout << "\nHere is sam: ";
    cout << sam;
    cout << "\n---------------\n";

    cout << "\nCreating a vector Joe of size 4.";
    MyVector joe(4);
    cout << "\nPush 6 values into the vector.";
    for (int i = 0; i < 6; i++)
        joe.push_back(i * 3);

    cout << "\nHere is joe: ";
    cout << joe;
    cout << "\n---------------\n";

    cout << "\nTest the overloaded assignment operator \"joe = sam\": ";
    joe = sam;

    cout << "\nHere is sam: ";
    cout << sam;
    cout << "\n---------------\n";

    cout << "\nHere is joe: ";
    cout << joe;
    cout << "\n---------------\n";

    // pass a copy of sam by value
    printV(sam);


    cout << endl;
    system("PAUSE");
    return 0;
}

void printV(MyVector v)
{
    cout << "\n--------------------\n";
    cout << "Printing a copy of a vector\n";
    cout << v;
}

我的Vector.h

#pragma once

class MyVector
{
private:
    int vSize;
    int vCapacity;
    int* vArray;
    void grow();

public:
    MyVector();
    MyVector(int n);
    MyVector(const MyVector& b);
    int size() const;
    int capacity() const;
    void clear();
    void push_back(int n);
    int& at(int n) const;
    MyVector& operator=(const MyVector& rho);
    ~MyVector();
};

MyVector.cpp

#include "MyVector.h"
#include <iostream>
using namespace std;


MyVector::MyVector()
{
    vArray = nullptr;
    vSize = 0;
    vCapacity = 0;
}

MyVector::MyVector(int n)
{
    vArray = new int[vCapacity];
    vSize = 0;
    vCapacity = n;
}

MyVector::MyVector(const MyVector& b)
{
    vSize = b.size();
    vArray = new int[vSize];
    for (int i = 0; i < b.size(); i++)
    {
        this->vArray[i] = b.vArray[i];
    }
}

int MyVector::size() const
{
    return vSize;
}

int MyVector::capacity() const
{
    return vCapacity;
}

void MyVector::clear(void)
{
    if (vArray != nullptr)
    {
        delete[] vArray;
        vArray = nullptr;
        vSize = 0;
        vCapacity = 0;
    }
}

void MyVector::push_back(int n)
{
    if (vCapacity == 0)
    {
        vCapacity++;
        int* tmp = new int[vCapacity];
        delete[] vArray;
        vArray = tmp;
    }

    if (vSize >= vCapacity)
    {
        grow();
    }
    vArray[vSize] = n;
    vSize++;
}

void MyVector::grow()
{
    vCapacity = vCapacity + vCapacity;
    int* tmp = new int[vCapacity];
    for (int i = 0; i < vSize; i++)
    {
        tmp[i] = vArray[i];
    }
    delete[] vArray;
    vArray = tmp;
}

int& MyVector::at(int index) const
{
    if (index >= 0 && index<vSize)
    {
        return vArray[index];
    }
    else
    {
        throw index;
    }
}

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

    // clean up array in left hand object (this)
    delete[] this->vArray;

    // create a new array big enough to hold right hand object's data
    vSize = rho.size();
    this->vArray = new int[vSize];

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

    // return this object
    return *this;
}

MyVector::~MyVector()
{
    if (vArray != nullptr)
    {
        clear();
    }
}

ostream& operator<<(ostream& out, const MyVector& rho)
{
    for (int i = 0; i < rho.size(); i++)
    {
        out << rho.at(i);
    }
    return out;
}

2 个答案:

答案 0 :(得分:0)

My Vector.h 中声明std::ostream& operator<<(std::ostream& out, const MyVector& rho)。该函数在 Driver.cpp 中使用,因此 Driver.cpp 必须知道它。

如果你这样做,你还应该在 My Vector.h #include <ostream>

答案 1 :(得分:0)

为了能够在operator<<()中使用您的违规main(),在编译Driver.cpp时,编译器需要声明它。

这意味着您需要在operator<<()中声明MyVector.h

operator<<()中定义MyVector.cpp很好,但在编译Driver.cpp

时编译器看不到这一点