Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 设置位数组_C++_Arrays_Mask_Bits - Fatal编程技术网

C++ 设置位数组

C++ 设置位数组,c++,arrays,mask,bits,C++,Arrays,Mask,Bits,我正在为我的OO类编写一个程序,我的cout语句遇到了一些问题。程序应该接收一个值并将其转换为按位数组。我的问题是,无论我在数组中输入什么数字,数组中的每个元素都会为0。赋值表示将构造函数中的每个元素初始化为0,但将其与查询函数中的&operator进行比较时,只能返回false。有什么建议吗?注意:我们不能使用向量或任何预制函数 在头文件中: private: static const CSIZE = 8 * sizeof(char); int arraySize; u

我正在为我的OO类编写一个程序,我的cout语句遇到了一些问题。程序应该接收一个值并将其转换为按位数组。我的问题是,无论我在数组中输入什么数字,数组中的每个元素都会为0。赋值表示将构造函数中的每个元素初始化为0,但将其与查询函数中的&operator进行比较时,只能返回false。有什么建议吗?注意:我们不能使用向量或任何预制函数

在头文件中:

private:
    static const CSIZE = 8 * sizeof(char);
    int arraySize;
    unsigned char* barray;
在.cpp文件中

#include <iostream>
#include "bitarray.h"
using namespace std;

ostream& operator<< (ostream& os, const BitArray& a)
{
    for (unsigned int i = 0; i < a.arraySize * a.CSIZE; i++)
        os << a.Query(i);

    return os;
}

BitArray::BitArray(unsigned int n)
{
    if (n % CSIZE != 0)
        arraySize = (n / CSIZE) + 1;
    else
        arraySize = n / CSIZE;

    barray = new unsigned char[arraySize];

    for (int i = 0; i < arraySize; i++)
        barray[i] = 0;
}

unsigned char BitArray::Mask (unsigned int num) const
{
    return (1 << num % CSIZE);
}

void BitArray::Set   (unsigned int index)
{
    unsigned int i = index / CSIZE;
    barray[i] |= Mask (index);
}

bool BitArray::Query (unsigned int index) const 
{
    unsigned int i = index / CSIZE;

    if (barray[i] & Mask (index))
        return true;

    return false;
}
主程序:

#include <iostream>

using namespace std;

#include "sieve.h"
#include "bitarray.h"

int main()
{
   unsigned int i, max, counter = 0;

   cout << "\nEnter a positive integer for the maximum value: ";
   cin >> max;

   BitArray ba(max);

   Sieve(ba);                    // find the primes (marking the bits)

   cout << "The bit array looks like this: \n"
        << ba
        << '\n';  

   cout << "\nPrimes less than " << max << ':' << '\n';
   for (i = 0; i < max; i++)
   {    
       if (ba.Query(i))
       {
        counter++;
            cout << i;
            if (counter % 8 == 0)
            {
                cout << '\n';
                counter = 0;
            }
            else
                cout << '\t';
       }
   }


   cout << "\nGoodbye!\n";
   return 0;
}

您提到您的问题涉及cout,但您的示例代码没有显示它-您可以包括您正在使用的实际代码吗?static const CSIZE=8*sizeofchar*arraySize;这是怎么回事?@SimonMᶜ肯齐,cout只在主程序中。我附加了一个链接到我们收到的主程序。对于.cpp文件,我们被要求对ostream进行重载。编辑:哦!我没意识到代码中已经删掉了,对不起!在这方面还是有点新…@MooseBoys,我在更新代码时编辑了这个,并修复了错误的东西!谢谢,这是给我的奥斯特雷姆接线员的。我将对此进行编辑。在输出数组位之前,您没有显示正在操作数组位的Sieve函数的代码。