Thread: Error while passing a reference of a vector to a function.

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Error while passing a reference of a vector to a function.

    First of all , I can't understand the error message.
    Code:
    error: invalid initialization of non-const
     reference of type ‘stack&’ from a temporary of
     type ‘std::vector<sub, std::allocator<sub> >
    The function call looks like:
    Code:
    flag = operate(&stack,&temp);
    //where stack is a vector<sub> 
    //and temp is an object of class 'sub'
    And the declaration of the function looks like:
    Code:
    bool operate(std::vector<sub>&,sub&);
    I am literally at my wits end.. But this works fine if the whole stack is passed and returned from the function , which would have a large overhead in case the stack is big enough.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    In the function call of operate() the ampersands are not necessary. The arguments the function expects are references, not pointers.

    Depending on what standard headers you have #include'd (including if they #include each other) you may have a problem as std::stack is a templated type in the standard library. Particularly if you have a "using namespace std;" or some other using directive active that makes the compiler give a match to std::stack when it sees the name "stack".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Then ..would std::stack be a better way to implement stacks ?..the name seems to suggests so...and does it behave identically to std::vector ?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It would conventionally be considered a good bet to assume that a standard library class named "stack" implements a stack.

    std::stack provides member functions like push(), pop(), and top() which are conventionally associated with stacks and not with vectors. std::stack (by default) delegates to a std::vector to hold the data.

    So, no, a stack does not behave identically to a vector. It provides a different interface - a stack does not provide the methods (aka member functions) that vector does, and stack also provides some methods that vector does not.

    As to whether you are better off using stack or vector: that depends on need of your program.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Passing an array as reference to a function?
    By Kylecito in forum C++ Programming
    Replies: 10
    Last Post: 03-11-2006, 02:25 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Problem with OpenGL tutorial
    By 2Biaz in forum Windows Programming
    Replies: 18
    Last Post: 09-16-2004, 11:02 AM

Tags for this Thread