2015-11-14 76 views
0

我正在为我的MyVector类超载运算符< <。这一切对我来说都是正确的,但当我尝试运行它时,我遇到了错误。我究竟做错了什么?试图超载<<运算符,出现错误

第一个错误是(我认为)“之前的意外标记(s)';' “我的.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; 

} 

回答

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 useusing namespace std;