【问题标题】:Why do I get "Invalid use of :: (scope resolution operator)?"为什么我会收到“无效使用 ::(范围解析运算符)?”
【发布时间】:2019-05-22 22:11:43
【问题描述】:

我正在使用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;
}

错误信息:

错误::: 的使用无效

【问题讨论】:

  • 您是否包含任何内容,或者您​​的头文件是否完全按照原样?
  • @Tas 不,头文件完全一样。
  • @BilalAhmed No repro。请根据需要发布minimal reproducible example重现问题。
  • 如果你要使用它,你的头文件需要包含std::size_t的定义位置。
  • 请参阅this 了解您可以使用的包含。

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


【解决方案1】:

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

#include &lt;cstddef&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 2015-02-28
    • 2020-12-12
    • 2011-02-23
    • 2012-04-20
    • 2017-10-26
    相关资源
    最近更新 更多