Pergunta

I am trying to write a function that asks the user to input multiple hex strings to a console and then convert those strings to bitsets. I want the function to use pointers to the bitsets so that the bitsets are stored in the parent function. Due to not using c++ 11 I split the 64bit bitset into two hex conversion operations.

void consoleDataInput(bitset<1> verbose, bitset<32>* addr, bitset<64>* wdata, bitset<8>* wdata_mask, bitset<1>* rdnwr)
{
    cout << "enter 1 for read 0 for write : ";
    cin >> *rdnwr;

    string        tempStr;
    unsigned long tempVal;
    istringstream tempIss;

    // Input the addr in hex and convert to a bitset

    cout << "enter addr in hex : ";
    cin >> tempStr;
    tempIss.str(tempStr);
    tempIss >> hex >> tempVal;
    *addr = tempVal;

    // enter wdata and wdata_mask in hex and convert to bitsets

    if (rdnwr[0] == 1)
    {
        *wdata = 0;
        *wdata_mask = 0;
    }
    else
    {
        // wdata

        bitset<32> tempBitset;
        cout << "enter wdata in hex : ";
        cin >> tempStr;
        if (tempStr.length() > 8)
        {
            tempIss.str(tempStr.substr(0,tempStr.length()-8));
            tempIss >> hex >> tempVal;
            tempBitset = tempVal;
            for (int i=31; i>=0; i--)
            {
                wdata[i+32] = tempBitset[i];
            }

            tempIss.str(tempStr.substr(tempStr.length()-8,tempStr.length()));
            tempIss >> hex >> tempVal;
            tempBitset = tempVal;
            for (int i=32; i>=0; i--)
            {
                wdata[i] = tempBitset[i];
            }
        }
        else
        {
            tempIss.str(tempStr);
            tempIss >> hex >> tempVal;
            tempBitset = tempVal;
            for (int i=32; i>=0; i--)
            {
                wdata[i] = tempBitset[i];
            }
        }

        // wdata_mask

        cout << "enter wdata_mask in hex : ";
        cin >> tempStr;
        tempIss.str(tempStr);
        tempIss >> hex >> tempVal;
        *wdata_mask = tempVal;
    }

When I try to compile using GCC in code::blocks I get the error

C:\Diolan\DLN\demo\ApolloSPI\main.cpp|202|error: no match for 'operator=' in '*(wdata + ((((sizetype)i) + 32u) * 8u)) = std::bitset<_Nb>::operator[](std::size_t) [with unsigned int _Nb = 32u; std::size_t = unsigned int](((std::size_t)i))'|

Which is highlighting the line

wdata[i+32] = tempBitset[i];

As I understand it, I don't need to use the * operator before wdata[i+32] because the [i+32]acts as the indication that it is a pointer.

I am unsure how to move forward. The = operator is a valid one to use with a bitset so I don't understand the error.

Thanks.

Foi útil?

Solução

Your wdata is indeed a pointer, thus wdata[i+32] actually is equivalent to *(wdata + i + 32), pointing at a Bitset<64>. You are assigning this bitset with tempBitset[i] which unless I'm mistaken is a single bit.

That is, you're trying to assign the value of a single bit to an entire bitset.

I suspect that you're wanting to set a single bit in one bitset to the value of a bit in another, in which case I think you do want the '*':

(*wdata)[i+32] = tempBitset[i];

Meaning, roughly, take the Bitset<64> pointed at by wdata, and set its bit [i+32] to the same value as the bit [i] of the bitset in tempBitset.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top