Problem converting std::vector into a reference

This is a discussion on Problem converting std::vector into a reference within the C++ Programming forums, part of the General Programming Boards category; I've just recompiled some code and suddenly I'm getting this strange error: Code: 'bool DBI::query(std::vector<_Ty> &,std::string &)' : cannot convert ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    88

    Problem converting std::vector into a reference

    I've just recompiled some code and suddenly I'm getting this strange error:
    Code:
    'bool DBI::query(std::vector<_Ty> &,std::string &)' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'std::vector<_Ty> &'
    DBI::query() is being called inside a private class method (not from inside the DBI class); it is non-static and non-const, and neither is the function calling it. The first parameter is defined the line before the call to DBI::query(). Also, DBI::query() is being called through a non-static member of the calling class. The code below (I think) shows the general jist.

    I'm possibly being too vague (tell me if I am), but does this problem sound familiar to anyone?

    I'm using the free MSVC++ fyi.

    Code:
    class DBI { ... };
    
    class Caller {
        DBI& mDBH;
    public:
        Caller( DBI& dbh ) : mDBH( dbh ) {}
    
    void call() {
        std::vector<std::string> data;
        if( mDBH.query( data, "asdosah" ) )
            throw 1;
    }
        // ...
    };

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    What is the prototype for query?

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Code:
    bool DBI::query( std::vector<std::string>& data, std::string stmt );

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    That looks like a definition since it has the DBI::, and it looks like a prototype since it has the semicolon. I don't see anything wrong with the code as is. Try re-building all to make sure your compiler isn't confused. Then try to find the simplest example that demonstrates the problem. It is likely that the cause is somewhere else, so start by having an empty DBI class with just the exact same query prototype, then add the Caller class with just the information you've shown. Throw in a simple main and the problem should re-occur. If it doesn't, then you have to give more information and make sure you copy it as is from your program.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    Being non const, the reference parameter does not allow for any conversion to take place. That is why you are getting the error.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    This is strange. The compiler reports an error with:
    'bool DBI::query(std::vector<_Ty> &,std::string &)'
    but the prototype has the signature:
    'bool DBI::query(std::vector<_Ty> &,std::string)'

    Being non const, the reference parameter does not allow for any conversion to take place.
    What do you mean?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    The way I see it is that mDBH is a reference to DBI and thus query() is being called on behalf of a reference object
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No problem with the vector here, but for the statement, Mario is right. The compiler creates a temporary std::string from the string literal, but temporaries can't bind to non-const references.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    No problem with the vector here, but for the statement, Mario is right. The compiler creates a temporary std::string from the string literal, but temporaries can't bind to non-const references.
    The thing is, the prototype given by drrngrvy seems inconsistent with that reported by the compiler. The compiler should have reported a problem with the second parameter, not the first, assuming that drrngrvy made a typo error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    True. Of course, the compiler handling the difference in a weird way might be the reason why the error message is nonsense.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    Actually if I got it right, it was by mere chance. I only skimmed the error message and most of the post. At the time of my replies I didn't realize the error was reporting the first parameter.

    But I'm curious too now as to why the compiler is flagging the wrong(?) parameter which will lead us back to Daved first post and perhaps the OP needs to provide some more code.

    Also, on a not so related note, shouldn't the compiler at that point already have replaced the template arguments for the vector? I mean, I'm used to see this type of errors described as:

    bool DBI::query(std::vector<std::string> &, std::string &)
    Or is this typical of the MSVC++ implementation?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    I think the posted error might be the first line in a long error message that includes something like
    Code:
    where
    [
        _Ty = std::basic_string<char, blah blah blah>
    ]

  13. #13
    Registered User
    Join Date
    Oct 2005
    Posts
    88
    Thanks for the input, but the problem still remains. The actual prototype is:
    Code:
       bool query( std::vector< std::string >& data,
                    std::string& statement );
    The thing is that this function definitely works, since it has (in the same function, no less, plus it still works in many other similar functions atm) for quite a while. I just don't get why it suddenly has a problem. What Mario F said:
    Being non const, the reference parameter does not allow for any conversion to take place. That is why you are getting the error.
    Is completely new to me, and I think it could be the source of the problem: the only strange thing is that an overloaded function with the prototype:
    Code:
    std::string query( std::string& statement );
    (this is called in the same class as the problematic function) doesn't seem to care about converting things into references.

    What do you mean, Mario F. Why can't 'temporary' reference parameters (even though the relevant one persists for the life of the program) convert function parameters into references?

    ----------------------------------------------------------------------------------------
    NB. Changing DBI& mDBH into const DBI& mDBH just gives the following error. God, I wish MS knew the difference between subtle and obfuscated...
    Code:
    'DBI::query' : 3 overloads have no legal conversion for 'this' pointer
    Last edited by drrngrvy; 10-12-2006 at 07:09 PM.

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,467
    There is a recent thread on that matter. You may want to search for it. It's just 4 or 5 days old I reckon. But shortly, because of this:

    Code:
    double a = 13;  // take note a is a double
    int &b = a;
    Let us assume the above didn't give you a compile time error. What happened when b was initialized? The compiler created an int temporary and assigned it by copying a. Then the reference b is created and what is it assigned to? to that temporary. But... what would happen when you changed b? what is b a reference of? a? nope. Look a few phrases up. b is a reference to a temporary. not to a. The temporary would be changed. As such the compiler immediatly flags my code above as an error.

    By using const the problem goes away.

    Code:
    double a = 13;  // take note a is a double
    const int &b = a;
    Why? Because we are saing to the compiler we aren't going to change the value of b. As such all is well. It is safe to do the conversion.

    so for now change your function prototype to:

    Code:
       bool query( std::vector< std::string >& data,
                    const std::string& statement );
    And let's see if the error goes away. It might although it doesn't explain why the compiler flagged the 1st parameter as an error.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    The answer is still the same. You need to post the exact code, and you need to try to come up with a small example that is complete and that we can test ourselves. It shouldn't be too difficult with only a couple classes and a couple functions.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with call by reference
    By zephyrcat in forum C Programming
    Replies: 2
    Last Post: 02-28-2008, 08:04 PM
  2. string to char converting problem
    By bergziege in forum C++ Programming
    Replies: 2
    Last Post: 08-07-2007, 03:37 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. GCC - Strange networking functions error.
    By maththeorylvr in forum Windows Programming
    Replies: 3
    Last Post: 04-05-2005, 12:00 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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