2013-11-27 73 views
0

我不得不使用int和一堆运算符重载一个向量类。每次尝试使用+, - 或/运算符时,我都会遇到一个运行时错误,它表示无效的分配大小:4294967295字节。任何关于如何改进我的代码的反馈也是受欢迎的。无效的分配运行时错误

我的代码: myArray.h

#include<iostream> 
using namespace std; 

class myArray{ 
private: 
    int *A; 
    int lenght; 
    int maxSize; 
public: 
    myArray(){lenght = 0; maxSize = 0; A = new int[maxSize];} 
    myArray(int s){maxSize = s; lenght = 0; A = new int[maxSize];} 
    myArray(const myArray &M); 
    ~myArray(){delete[] A;} 
    const int getMaxSize(){return maxSize;} 
    const int getLenght(){return lenght;} 
    const myArray& operator +(const myArray& A); 
    const myArray& operator -(const myArray A); 
    const int operator *(const myArray& A); 
    const myArray& operator /(const myArray A); 
    const myArray& operator +(int A); 
    const myArray& operator -(int A); 
    const int operator *(int A); 
    const myArray operator /(int A); 
    const myArray operator ++(); 
    const myArray operator ++(int); 
    const myArray operator --(); 
    const myArray operator --(int); 
    myArray operator -(); 
    int operator [](int ind) const; 
    myArray& operator =(const myArray& rho); 
    void push(int n); 
    int pop(); 
    void insert(int n, int pos); 
    int remove(int pos); 
    void resize(int newSize); 
}; 

myException.h

#include<iostream> 
#include<exception> 
#include<string> 
using namespace std; 

class myException: public exception 
{ 
private: 
    int code; 
    string reason; 
public: 
    myException(){code = 0; reason = "Unknown";} 
    myException(int c, string r){code = c; reason = r;} 
    friend ostream& operator <<(ostream& outputStream, const myException A); 
}; 

ostream& operator <<(ostream& outputStream, const myException A) 
{ 
outputStream << "Code: " << A.code << " Reason: " << A.reason << endl; 
return outputStream; 
} 

myArray.cpp

#ifndef MYARRAY_H 
#define MYARRAY_H 
#include "myArray.h" 
#include "myException.h" 
//Copy contructor 
myArray::myArray(const myArray &M) 
{ 
    maxSize = M.maxSize; 
    lenght = M.lenght; 
    A = new int[maxSize]; 
    for(int i = 0; i < M.lenght; i++) 
     A[i] = M.A[i]; 
}  
//Adds the elements of the array with each other and returns the result 
const myArray& myArray::operator +(const myArray& secondArray) 
{ 
    try 
    { 
     if(lenght != secondArray.lenght) 
      throw myException(10, "Different sizes!"); 

     myArray result(secondArray); 
     for(int i = 0; i < lenght;i++) 
      result.A[i] = A[i] + secondArray.A[i]; 
     return result; 
    } 
    catch(myException& e) 
    { 
     cout << e; 
    } 
} 
//Subtracts the elements of the array with each other and returns the result 
const myArray& myArray::operator -(const myArray secondArray) 
{ 
    try 
    { 
     if(lenght != secondArray.lenght) 
      throw myException(10, "Different sizes!"); 

     myArray result(secondArray); 
     for(int i = 0; i < lenght;i++) 
      result.A[i] = this->A[i] - secondArray.A[i]; 
     return result; 
    } 
    catch(myException& e) 
    { 
     cout << e; 
    } 
} 
//Gets the dot product of 2 vectors 
const int myArray::operator *(const myArray& secondArray) 
{ 
    try 
    { 
     if(lenght != secondArray.lenght) 
      throw myException(10, "Different sizes!"); 

     int result = 0; 
     for(int i = 0; i < lenght;i++) 
      result += this->A[i] * secondArray.A[i]; 
     return result; 
    } 
    catch(myException& e) 
    { 
     cout << e; 
    } 
} 
//Divides the elements of the array with each other and returns the result 
const myArray& myArray::operator /(const myArray secondArray) 
{ 
    try 
    { 
     if(lenght != secondArray.lenght) 
      throw myException(10, "Different sizes!"); 

     myArray result(secondArray); 
     for(int i = 0; i < lenght;i++) 
      result.A[i] = this->A[i]/secondArray.A[i]; 
     return result; 
    } 
    catch(myException& e) 
    { 
     cout << e; 
    } 
}  
//Adds the elements of the array with an int and returns the result 
const myArray& myArray::operator +(int A) 
{ 
    myArray result(*this); 
    for(int i = 0; i < lenght;i++) 
     result = this->A[i] + A; 
    return result; 
} 
//Subtracts the elements of the array with an int and returns the result 
const myArray& myArray::operator -(int A) 
{ 
    myArray result(*this); 
    for(int i = 0; i < lenght;i++) 
     result = this->A[i] - A; 
    return result; 
} 
//Gets the dot product of a vector multiplied by an int 
const int myArray::operator *(int A) 
{ 
    int result = 0; 
    for(int i = 0; i < lenght;i++) 
     result += this->A[i] * A; 
    return result; 
} 
//Divides the elements of the array with an int and returns the result 
const myArray myArray::operator /(int A) 
{ 
    myArray result(*this); 
    for(int i = 0; i < lenght;i++) 
     result = this->A[i]/A; 
    return result; 
}  

//increments every element in the array by 1(Pre-increment) 
const myArray myArray::operator ++() 
{ 
    for(int i = 0; i < lenght; i++) 
     ++A[i]; 

    return *this; 
} 
//increments every element in the array by 1(Post-increment) 
const myArray myArray::operator ++(int) 
{ 
    myArray temp(maxSize); 

    for(int i = 0; i < lenght; i++) 
     temp.A[i] = A[i]++; 

    return temp; 
} 
//decrements every element in the array by 1(Pre-decrement) 
const myArray myArray::operator --() 
{ 
    for(int i = 0; i < lenght; i++) 
     --A[i]; 

    return *this; 
} 
//decrements every element in the array by 1(Post-decrement) 
const myArray myArray::operator --(int) 
{ 
    myArray temp(maxSize); 
    for(int i = 0; i < lenght; i++) 
     temp.A[i] = A[i]--; 

    return temp; 
} 
//Makes every element in the array negative 
myArray myArray::operator -() 
{ 
    for(int i = 0; i < lenght; i++) 
     A[i] = -A[i]; 
    return *this; 
}  
//returns the number in the array using [] 
int myArray::operator [](int ind) const 
{ 
    try 
    { 
     if(ind > lenght) 
      throw myException(60, "Array index out of bounds"); 

     return A[ind]; 
    } 
    catch(myException& e) 
    { 
     cout << e; 
    } 
} 
//Assignment operator 
myArray& myArray::operator=(const myArray& B) 
{ 
    delete [] A; 
    A = new int[B.maxSize]; 
    lenght = B.lenght; 
    maxSize = B.maxSize; 
    for(int i = 0; i < B.lenght; i++) 
    { 
     A[i] = B.A[i]; 
    } 
    return (*this); 
} 

//pushes the value inserted to the next available spot in the array 
void myArray::push(int n) 
{ 
    try 
    { 
     if(lenght == maxSize) 
      throw myException(30, "Not enough space"); 

     if(lenght == 0) 
     { 
      A[0] = n; 
      lenght++; 
     } 
     else 
     { 
      for(int i = 0; i < lenght; i++) 
      { 
       if(i+1 == lenght) 
       { 
        A[i+1] = n; 
        lenght++; 
        break; 
       } 
      } 
     } 
    } 
    catch(myException& e) 
    { 
     cout << e; 
    } 
} 
//Removes the last element in the array and returns it 
int myArray::pop() 
{ 
    try 
    { 
     if(lenght <= 0) 
      throw myException(60, "Array index out of bounds"); 

     int temp = A[lenght - 1]; 
     A[lenght - 1] = NULL; 
     lenght--; 
     return temp; 
    } 
    catch(myException& e) 
    { 
     cout << e; 
    } 
} 
inserts an element at the specified position 
void myArray::insert(int n, int pos) 
{ 
    try 
    { 
     if(pos > lenght) 
      throw myException(60, "Array index out of bounds"); 

     for(int i = 0; i <= lenght; i++) 
     { 
      if(i == pos) 
      { 
       A[i-1] = n; 
      } 
     } 
    } 
    catch(myException& e) 
    { 
     cout << e; 
    } 
} 
//removes an element at a specified position an returns the value. 
int myArray::remove(int pos) 
{ 
    try 
    { 
     if(pos < 0 || (pos > lenght -1)) 
      throw myException(50, "Invalid Position"); 

     int temp = A[pos]; 
     A[pos] = NULL; 

     for(int i = pos; i < lenght; i++) 
     { 
      A[i] = A[i+1]; 
     } 

     return temp; 

    } 
    catch(myException& e) 
    { 
     cout << e; 
    } 

}  
//Re sizes the entire array 
void myArray::resize(int newSize) 
{ 
    int *B; 
    B = new int[newSize]; 
    maxSize = newSize; 
    for(int i = 0; i < lenght; i++) 
     B[i] = A[i]; 

    delete[] A; 
    A = B; 
} 

#endif 

这仅仅是一个伪主对myArray的测试一切

的main.cpp

#include "myArray.h" 

int main() 
{ 
    int num; 
    myArray vector1; 
    myArray vector2(5); 
    myArray vector3; 
    vector1.resize(5); 
    //cout << "Max Size: " << vector1.getMaxSize() << endl; 
    for(int i = 0; i < 4; i++) 
    { 
     cin >> num; 
     vector1.push(num); 
    } 

    for(int i = 0; i < 4; i++) 
    { 
     cin >> num; 
     vector2.push(num); 
    } 

    vector3 = vector1 + vector2; 

    for(int i = 0; i < 4; i++) 
     cout << vector3.pop() << endl; 

} 
+2

有趣。你有一个名为'A'的成员变量,并将你的函数参数命名为'A'。采访失败。 – kfsone

+1

你不会喜欢这个建议,但我会对你很痛苦诚实。你应该重新开始。你写了很多代码,你很清楚的了解它。重新开始,不要只写代码,通过调试器运行它并观察它的功能。花时间让你的代码可读和有意义。然后再回答关于您不确定的个别项目的个别问题。另外,当你覆盖一个指针'A = new [maxSize]'时,以前的值不会自动解除分配。这被称为内存泄漏。你有它。还有:'长度'。 – kfsone

回答

0

创建动态数组大小为零的默认构造函数。 但在push方法中,如果长度为零,则尝试为其设置值。

在C++标准,它说:

解引用一个指针的作用返回零 尺寸是不确定的请求。

您只能删除它。修复您的push方法

相关问题