vector<bool> error with pointers

Hello everybody and thanks for your help! :-)

I'm trying to write a simple code which has two classes: LogicGate and CircuitDescription.

CircuitDescription contains a vector of inputs (vector<bool> _inputs_vector) and a vector of pointers to LogicGate objects (vector<LogicGate *> _gates_vector).

Every LogicGate objects has its own input vector of pointers to bool (vector<bool *> _own_input_vector).

I want every cell of _own_input_vector to contain the address (should I say "reference"?) of a single cell of _inputs_vector, but when I try to assign the address I receive two errors:

- error: taking addres of temporary [-fpermissive]
- error: cannot convert 'std::vector<bool>::reference* {aka std::_Bit_reference*}' to 'bool*' in assigment

If I change every type from bool to int, for example, everything works well.

This is the part of .cpp (of the class CircuitDescription) in which I try to do the assignment

1
2
3
// 'i' is an integer

                         _own_input_vector.at(i) =  &_inputs_vector.at(i);


If you want, this is the whole function.
I have to know if I have to point to the _inputs_vector or to the another vector of inputs called "nets".
The function read from a vector of string in which the information is coded as "I3" "N2" "I0", where the letter determines the vector (input or net) and the number is the position in the vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

void CircuitDescription::set_input_vector(vector<string> &input_vector, LogicGate &theGate) 
{
    vector<string> *pointer_to_input_vector = &input_vector;
    vector<string>::iterator it_vector = pointer_to_input_vector -> begin();
    string cella;

    vector<bool *>::iterator it_input_cella = theGate._inputVector.begin();


        while (it_vector != pointer_to_input_vector -> end())
            {

                cella = *it_vector;

                    if (cella.at(0) == 'I')
                    {
                        *it_input_cella =  &_circuit_inputs_vector.at(cella.at(1));
                    }

                    if (cella.at(0) == 'N')
                    {
                        *it_input_cella = & _circuit_nets_vector.at(cella.at(1));
                    }


                it_input_cella++;
                it_vector++;
            }
}
Last edited on
std::vector<bool> need not (usually does not) store values of type bool.

std::vector<bool> behaves similarly to std::vector, but in order to be space efficient, it:
Does not necessarily store its elements as a contiguous array (so &v[0] + n != &v[n])
Exposes class std::vector<bool>::reference as a method of accessing individual bits. In particular, objects of this class are returned by operator[] by value.

http://en.cppreference.com/w/cpp/container/vector_bool
http://en.cppreference.com/w/cpp/container/vector_bool/reference

Use std::deque<bool> instead of std::vector<bool>.

Or, if the space-efficient std::vector<bool> must be used, something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>

int main()
{
    std::vector<bool> circuit_inputs_vector { true, false, true, false, true, true, false, false } ;

    using reference_to_bit = std::vector<bool>::reference ;
    std::vector<reference_to_bit> gates_vector ;
    for( reference_to_bit refbit : circuit_inputs_vector ) gates_vector.push_back(refbit) ;

    for( bool b : circuit_inputs_vector ) std::cout << b << ' ' ;
    std::cout << '\n' ;

    for( bool b : gates_vector ) std::cout << b << ' ' ;
    std::cout << '\n' ;

    for( auto& refbit : gates_vector ) refbit.flip() ;

    for( bool b : circuit_inputs_vector ) std::cout << b << ' ' ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/0b840f7c44787bfb
http://rextester.com/XFND28071
std::vector<bool> is special. See:

http://www.cplusplus.com/reference/vector/vector-bool/

It is not a good idea to hold a pointer to an element of the vector because it may become invalid. Rather store an index.
Thank you JLBorges and coder777! :D

_____

This is my first C++ course, so I don't know neither deque and this unconventional syntax to write 'for' cycles
"for( bool b : circuit_inputs_vector )".
But I will read those topics :-)

EDIT: understood the syntax ;)
_____

I used a pointer to a specific element of the vector because many LogicPorts have to point to that address and take informations from it, and it has to be updated during the runtime. Hence the need for the pointer to specific positions.
The program provides specific controls to stop itself in case of errors.
Last edited on
> I used a pointer to a specific element of the vector because many LogicPorts have to point to
> that address and take informations from it, and it has to be updated during the runtime.

We can also use an index into the circuit_inputs_vector to achieve the same effect.

A pointer is fine here; the number of inputs to the circuit (the size of the circuit_inputs_vector) would not be changing dynamically during a single run.
Topic archived. No new replies allowed.