【问题标题】:'this' cannot be used in a constant expression error'this' 不能用于常量表达式错误
【发布时间】:2021-11-17 07:55:03
【问题描述】:

错误出现在第 76 行 int res[mSize];,问题出现在 mSize。这似乎是一个简单的修复,但我无法弄清楚。如果有人能弄清楚或指出我正确的方向,将不胜感激。

还有,解构器~MyContainer(),我不知道我用对了还是有合适的地方放。

这是我的代码:

#include <iostream>

using namespace std;

class MyContainer
{
private:
    int* mHead; // head of the member array
    int mSize;  // size of the member array

public:
    MyContainer();
    MyContainer(int*, int);
    //~MyContainer();
    void Add(int);
    void Delete(int);
    int GetSize();
    void DisplayAll();
    int FindMissing();
    ~MyContainer() {}
};

MyContainer::MyContainer()
{
    mHead = NULL;
    mSize = 0;
}

MyContainer::MyContainer(int* a, int b)
{
    mHead = a;
    mSize = b;
}

void MyContainer::Add(int a)
{
    *(mHead + mSize) = a;
    mSize++;
}

void MyContainer::Delete(int a)
{
    int index;
    for (int i = 0; i < mSize; i++)
    {
        if (*(mHead + i) == a)
        {
            index = i;
            break;
        }
    }

    for (int i = index; i < mSize; i++)
    {
        *(mHead + i) = *(mHead + i + 1);
    }
    mSize--;
}

int MyContainer::GetSize()
{
    return mSize;
}

void MyContainer::DisplayAll()
{
    cout << "\n";
    for (int i = 0; i < mSize; i++)
    {
        cout << *(mHead + i) << " ";
    }
}

int MyContainer::FindMissing()
{
    int res[mSize];
    int temp;
    int flag = 0;
    for (int i = 1; i <= mSize; i++)
    {
        flag = 0;
        for (int j = 0; j < mSize; j++)
        {
            if (*(mHead + j) == i)
            {
                flag = 1;
                break;
            }
        }

        if (flag == 0)
        {
            temp = i;
            break;
        }
    }
    return temp;
}


int main() 
{
    const int cSize = 5; 
    int lArray[cSize] = { 2, 3, 7, 6, 8 }; 
    MyContainer lContainer(lArray, cSize); 
    lContainer.DisplayAll(); 
    lContainer.Delete(7); 
    lContainer.DisplayAll(); 
    cout << "Size now is: " << lContainer.GetSize() << endl; lContainer.Add(-1); 
    lContainer.Add(-10); 
    lContainer.Add(15); 
    lContainer.DisplayAll(); 
    cout << "Size now is: " << lContainer.GetSize() << endl; 
    cout << "First missing positive is: " << lContainer.FindMissing() << endl;
    system("PAUSE"); return 0;
}

【问题讨论】:

  • 数组需要有一个恒定的大小,所以你不能在运行时选择大小。使用std::vector 将解决该问题。
  • MyContainer::MyContainer(int* a, int b)中,ownsa指向的分配是谁?在开始使用析构函数之前,这是一件非常重要的事情。如果您决定需要一个析构函数,那么您需要阅读有关 the Rule of Three 和朋友的信息。
  • 更密切相关,int res[mSize]; 是什么?它似乎没有使用,我想不出我会用它做什么。您不能返回一个作为局部变量的数组,它 decays to a pointer 然后超出范围,给调用者留下一个定时炸弹,因此您甚至不能使用它来跟踪多个丢失的数字。
  • 'this' 和它有什么关系?你对析构函数有什么疑问?

标签: c++ class pointers


【解决方案1】:

int res[mSize];

数组mSize 的大小必须在编译时知道。您不能在此处使用变量。一个选项可能是定义一个不会超过的较大值的宏。

static const int kLargeSize =100;
int res[kLargeSize];

根据 cmets 进行编辑 - const 和 constexpr 是比宏更好的选择。

或者更好,你可以使用std::vector - https://en.cppreference.com/w/cpp/container/vector

【讨论】:

  • 当可以使用常量时,没有充分的理由使用宏。
  • 它更好的编程实践,随着代码库的增长,这些“神奇的数字”往往会随处出现。如果有宏,则可以跟踪并轻松修改这些宏。但是,如上所述,std::vector 是更好的选择。
  • 来自链接 = Macros are just like any other tool 请注意,我已经声明 std::vector 是更好的解决方案。
  • 有问题的代码有多个逻辑问题。在这种特殊情况下,给出的解决方案修复了列出的问题。而且,如前所述,std::vector 是更好的解决方案。
  • /*static*/ constexpr 变量完成了这项工作。这不是宏观工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-29
  • 2020-03-09
  • 1970-01-01
  • 2021-12-03
  • 2023-01-31
  • 1970-01-01
相关资源
最近更新 更多