【问题标题】:error C2352 illegal call of non-static member function错误 C2352 非法调用非静态成员函数
【发布时间】:2016-03-31 00:15:37
【问题描述】:

我正在使用动态大小的数组创建堆类型优先级队列。我知道向量会更容易实现,但这对我来说是一个学习练习。一切都很好,但只有在 Visual Studio '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;
}

【问题讨论】:

  • 不要将有关c++ 的问题标记为c
  • 对不起!第一次海报。

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


【解决方案1】:

您需要将您的测试方法声明为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 方法只能引用静态数据成员,因为调用这些方法时没有类实例。

【讨论】:

    【解决方案2】:

    你需要一个UnitTest的实例

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

    请注意,目前没有充分的理由说明您的测试函数完全是类的一部分。

    【讨论】:

    • 考虑将测试函数作为组织工具放在类中,类似于使用命名空间。这也是一种常见的 OOP 技术。
    【解决方案3】:

    之前的两个答案都是正确的,static 或使用对象可能会解决问题。

    另一种解决方案是使用namespace 而不是class

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

    【讨论】:

    • 我想使用您的方法,但我收到错误消息“具有此名称的符号已存在,因此此名称不能用作命名空间名称”。有什么建议吗?
    • 您应该先删除该类或更改其名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多