错误C2352非法调用非静态成员函数

时间:2016-03-31 00:15:37

标签: c++ arrays unit-testing testing priority-queue

我正在使用动态大小的数组创建堆类型优先级队列。我知道向量会更容易实现,但这对我来说是一个学习练习。一切都很好,但我只是在视觉工作室尝试一些单元测试时遇到问题13。我遇到了this error

这是我尝试运行单元测试的源文件:

//Prog1Test.cpp
#include "UnitTest.h"
#include <iostream>

int main()
{
    PriorityQueue Q = PriorityQueue();
    UnitTest::test1(Q);
    UnitTest::test2(Q);
    UnitTest::test3(Q);
    UnitTest::test4(Q);
    return 0;
}

这是UnitTest.cpp:

//UnitTest.cpp
#include "UnitTest.h"
#include <cassert>

void UnitTest::test1(PriorityQueue Q)
{
    Q.clear();
    Q.append('a');
    Q.append('b');
    assert(Q.size() == 2);
    assert(Q.check() == true);
}

void UnitTest::test2(PriorityQueue Q)
{
    Q.clear();
    Q.append('b');
    Q.append('a');
    assert(Q.size() == 2);
    assert(Q.check() == false);
}

void UnitTest::test3(PriorityQueue Q)
{
    Q.clear();
    Q.insert('a');
    Q.insert('b');
    assert(Q.size() == 2);
    assert(Q.check() == true);
    assert(Q.remove() == 'a');
    assert(Q.size() == 1);
}

void UnitTest::test4(PriorityQueue Q)
{
    Q.clear();
    Q.insert('b');
    Q.insert('a');
    assert(Q.size() == 2);
    assert(Q.check() == true);
    assert(Q.remove() == 'a');
    assert(Q.size() == 1);
}

这是UnitTest头文件:

//UnitTest.h
#ifndef UnitTest_H
#define UnitTest_H
#include "PriorityQueue.h"

class UnitTest
{
public:
    void test1(PriorityQueue Q);
    void test2(PriorityQueue Q);
    void test3(PriorityQueue Q);
    void test4(PriorityQueue Q);
};


#endif

这是PriorityQueue类标题:

#ifndef PriorityQueue_H
#define PriorityQueue_H

class PriorityQueue
{
private:
    char *pq;
    int length;
    int nextIndex;
    char root;
public:
    PriorityQueue();
    ~PriorityQueue();
    char& operator[](int index);
    void append(char val);
    int size();
    void clear();
    void heapify();
    bool check();
    void insert(char val);
    char remove();
    friend class UnitTest;
};


#endif

这是priorityqueue.cpp文件:

#include<math.h>
#include "PriorityQueue.h"




PriorityQueue::PriorityQueue()
{
    pq = new char[0];
    this->length = 0;
    this->nextIndex = 0;
}




PriorityQueue::~PriorityQueue() {
    delete[] pq;
}




char& PriorityQueue::operator[](int index) {
    char *pnewa;
    if (index >= this->length) {
        pnewa = new char[index + 1];
        for (int i = 0; i < this->nextIndex; i++)
            pnewa[i] = pq[i];
        for (int j = this->nextIndex; j < index + 1; j++)
            pnewa[j] = 0;
        this->length = index + 1;
        delete[] pq;
        pq = pnewa;
    }
    if (index > this->nextIndex)
        this->nextIndex = index + 1;
    return *(pq + index);
}




void PriorityQueue::append(char val) {
    char *pnewa;
    if (this->nextIndex == this->length) {
        this->length = this->length + 1;
        pnewa = new char[this->length];
        for (int i = 0; i < this->nextIndex; i++)
            pnewa[i] = pq[i];
        for (int j = this->nextIndex; j < this->length; j++)
            pnewa[j] = 0;
        delete[] pq;
        pq = pnewa;
    }
    pq[this->nextIndex++] = val;
}



int PriorityQueue::size() {
    return this->length;
}




void PriorityQueue::clear() {
    delete[] pq;
    pq = new char[0];
    this->length = 0;
    this->nextIndex = 0;
}




void PriorityQueue::heapify() {
    char parent;
    char root;
    char temp;
    for (double i = this->length - 1; i >= 0; i--)
    {
        root = pq[0];
        int parentindex = floor((i - 1) / 2);
        int leftchildindex = 2 * i + 1;
        int rightchildindex = 2 * i + 2;
        if (pq[(int)i] <= pq[leftchildindex] && pq[(int)i] <= pq[rightchildindex])
        {
            pq[(int)i] = pq[(int)i];
        }
        else if (rightchildindex < this->length && pq[(int)i] > pq[rightchildindex])
        {
            temp = pq[(int)i];
            pq[(int)i] = pq[rightchildindex];
            pq[rightchildindex] = temp;
            heapify();
        }
        else if (leftchildindex < this->length && pq[(int)i] > pq[leftchildindex])
        {
            temp = pq[(int)i];
            pq[(int)i] = pq[leftchildindex];
            pq[leftchildindex] = temp;
            heapify();
        }
    }
}



void PriorityQueue::insert(char val) {
    char *pnewa;
    if (this->nextIndex == this->length) {
        this->length = this->length + 1;
        pnewa = new char[this->length];
        for (int i = 0; i < this->nextIndex; i++)
            pnewa[i] = pq[i];
        for (int j = this->nextIndex; j < this->length; j++)
            pnewa[j] = 0;
        delete[] pq;
        pq = pnewa;
    }
    pq[this->nextIndex++] = val;
    PriorityQueue::heapify();
}


bool PriorityQueue::check() {
    char root;
    root = pq[0];
    for (int i = this->length - 1; i >= 0; i--)
    {
        if ((int)pq[i]< (int)root)
            return false;
    }
    return true;
}



char PriorityQueue::remove() {
    char root = pq[0];
    char *qminus;
    qminus = new char[this->length];
    for (int i = 1; i<this->length; i++)
        qminus[i - 1] = pq[i];
    pq = qminus;
    this->length -= 1;
    PriorityQueue::heapify();
    return root;
}

3 个答案:

答案 0 :(得分:3)

您需要将测试方法声明为static

class UnitTest
{
public:
    static void test1(PriorityQueue Q);
    static void test2(PriorityQueue Q);
    static void test3(PriorityQueue Q);
    static void test4(PriorityQueue Q);
};

请注意,static方法只能引用静态数据成员,因为调用这些方法时没有类实例。

答案 1 :(得分:1)

您需要UnitTest

的实例
PriorityQueue Q = PriorityQueue();
UnitTest t;
t.test1(Q);
t.test2(Q);
t.test3(Q);
t.test4(Q);
return 0;

请注意,目前没有充分的理由说明您的测试功能完全属于某个类的一部分。

答案 2 :(得分:0)

以前的答案都是正确的,static或使用对象可以解决问题。

另一种解决方案是使用namespace代替class

namespace UnitTest
{
    void test1(PriorityQueue Q);
    void test2(PriorityQueue Q);
    void test3(PriorityQueue Q);
    void test4(PriorityQueue Q);
};