为什么会收到“无效使用::(作用域分辨率运算符)”的信息?

时间:2019-05-22 22:11:44

标签: c++ build cmake std gnu-make

我正在使用cmake构建数据结构项目,并构建了一个数组类。

我使用std::size_t作为默认构造函数的参数。 但是,当我尝试构建项目时,出现错误,提示Invalid use of ::

我尝试了using namespace std;,但是也没用。

barra.h文件

#ifndef BARRAY_H
#define BARRAY_H

class BArray
{
public:
    BArray() = delete;                            //Declare the default constructor as deleted to avoid
                                                  //declaring an array without specifying its size.
    BArray(std::size_t);
    BArray(int, int);                             //Constructor that initializes the array with init_val.
private:
    int* array;
    int length;
};

#endif // BARRAY_H

还有barray.cpp

#include <iostream>
#include "barray.h"

BArray::BArray(std::size_t init_size)
{
    array = new int[init_size]();
    length = init_size;
}

BArray::BArray(int init_size, int init_val)
{
    array = new int[init_size];
    length = init_size;

    for(int i = 0; i < init_size; ++i)
        array[i] = init_val;
}

错误消息:

错误:无效使用::

1 个答案:

答案 0 :(得分:0)

添加以下头文件,它应该可以工作。

#include <cstddef>