成员类功能

时间:2018-02-01 21:05:47

标签: c++ arrays class object

所以我试图找出如何创建一个函数,该函数返回与self和ary相同的所有整数的数组,如果没有交叉,则返回一个空数组。这就是我到目前为止所做的:

IntArray* findIntersections(IntArray &ary) {
int length1 = ary.getLength(); // Getting the length of &ary.
int length2 = getLength([//Right here, there's something to do with "this" because I've created a mArray already and defined it, but I'm not sure how to re-call it here.]); // Getting the length of self.
int length4 = 0; // Because anarchy
if (length1 > length2){ // An if statement to match length4 with the longest array.
    length4 = length2;
} else {
    length4 = length1;
}

IntArray* newArray; // New IntArray on the heap.
newArray = new* int[length4];

int k = 0;
for (int j = 0; j < length1; j++){ // Two for loops getting the iterator on the same page.
    for (int i = 0; i < length2; i++){
        if (ary.get(j) == [//The this from earlier, whatever it is][i]){ // Checking to see if digits match each other.
            newArray[k] = (ary[j]); // Writing the digit into the NewArray.
            k++; // Adding one to the k count to progress the the iterator.
        }
    }
}
return newArray; 
}

最后,我知道会有三个阵列。传入的那个,正在创建和传递的那个,然后这个引用我老实说甚至不知道它是什么。我认为这是我之前创建的一个mArray,但我不太确定。对此有任何帮助绝对是太棒了!

另外,这是我的.hpp文件:

class IntArray {
private:
    int *mArray;
    int mSize;

public:

    IntArray(int *array, int size);
    int get(int index);
    int getLength();
    int indexOf(int value);
    bool remove(int index);
    IntArray* findIntersections(IntArray &ary);
    bool isSubsequence(IntArray &ary);
    ~IntArray();
};

我的.cpp文件:

#include "IntArray.hpp"

IntArray::IntArray(int *array, int size) {
int *newArray = new int [size];
for (int i = 0; i < size; i++){
    newArray[i] = array[i];
}
}

int IntArray::get(int index) {
return mArray[index];
}

IntArray::~IntArray() {
delete[] mArray;
}

int IntArray::getLength() {
return mSize;
}

int getLength(int *array){
int length = (sizeof(array)/sizeof(array[0]));
return length;
}

int indexOf(int *array, int value){
int length = getLength(array);
for (int i = 0; i < length; i++){
    if (value == array[i]){
        return i;
    }
}

return -1;
}

bool remove(int *array, int index){
int length = getLength(array);

if (0 <= index && index < length){
    for (int i = index + 1; i < length; ++i){
        array[i - 1] = array[i];
    }
    for (int i = 0; i < length; i++)
        return true;
} else {
    return false;
}
}

IntArray* findIntersections(IntArray &ary) {
int length1 = ary.getLength(); // Getting the length of &ary.
int length2 = getLength([//Right here, there's something to do with "this" because I've created a mArray already and defined it, but I'm not sure how to re-call it here.]); // Getting the length of self.
int length4 = 0; // Because anarchy
if (length1 > length2){ // An if statement to match length4 with the longest array.
    length4 = length2;
} else {
    length4 = length1;
}

IntArray* newArray; // New IntArray on the heap.
newArray = new* int[length4];

int k = 0;
for (int j = 0; j < length1; j++){ // Two for loops getting the iterator on the same page.
    for (int i = 0; i < length2; i++){
        if (ary.get(j) == [//The this from earlier, whatever it is][i]){ // Checking to see if digits match each other.
            newArray[k] = (ary[j]); // Writing the digit into the NewArray.
            k++; // Adding one to the k count to progress the the iterator.
        }
    }
}
return newArray; 
}

bool isSubsequence(int *array) {
int length = getLength(array);
for (int i = 0; i < length - 1; i++) {
    if (array[i] != array[i + 1])
        return false;
}
return true;
}

1 个答案:

答案 0 :(得分:0)

您的findIntersection实现未定义为成员函数,您已将其定义为全局函数。未定义名为findIntersection的成员函数声明。简而言之:您在函数名称

之前忘记了IntArray::