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

This is a discussion on Error while passing a reference of a vector to a function. within the C++ Programming forums, part of the General Programming Boards category; First of all , I can't understand the error message. Code: error: invalid initialization of non-const reference of type ‘stack&’ ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,509

    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,531
    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 in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,509
    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 ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,531
    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 in reply to you, it is likely you deserve it. Suck it up, sunshine, 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, 07: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, 12:02 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21