Using vectors and references - Cannot convert from vector to &

Posted on 2007-07-24
Last Modified: 2010-08-05

void setBit(BIT &aBit, const bool aValue){

int main(){
      vector<BIT> bitVec[3];
      setBit(bitVec[0], false);
      setBit(bitVec[1], false);
      setBit(bitVec[2], false);
      return false;

I get errors about not being able to convert from a std::vector to 'BIT &'?
Question by:Unimatrix_001
    LVL 3

    Accepted Solution

    Change vector<BIT> bitVec[3]; to vector<BIT> bitVec; (the way it is currently you are making an array of 3 vector<BIT> and bitVec[0] accesses a vector, not a BIT)  You will then need to use bitVec.push_back(someBIT) where someBIT is of type BIT inorder to add elements to the array.
    LVL 3

    Author Comment

    Thanks. I knew it would be something silly... Time to sleep I think. :)
    LVL 53

    Expert Comment

    Actually, you'd better use :

            vector<BIT> bitVec(3);

    (to reserve space for 3 BITs in the vector) because immediately after you try to access the vector using bitVec[0], bitVec[1] and bitVec[2].

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now