简体   繁体   中英

no known conversion for templated vs const non-templated vector

In my actual code, I included a library, and as soon as I did that, it started crashing. I managed to sort of extract some of that code into this minimal example, that demonstrates the same kind of error:

// g++ -std=c++11 -g -o test-classcall.exe test-classcall.cpp

#include <iostream>
#include <vector>
#include <stdio.h>

class Cat
{
  public:
    int Age;
    Cat() : Age(0) {}
};

std::vector<Cat> myPCats;

typedef std::vector<Cat> TDVectCats;
TDVectCats myTDCats;

void loopSomeCats() {
  printf("this function just to cause searching for matching calls\n");
}

void loopSomeCats(TDVectCats& incats) {
  std::vector<Cat>::iterator iter;
  for(iter = incats.begin(); iter != incats.end(); iter++) {
    printf("hm\n");
  }
}

const std::vector<Cat> & getSomeCats() {
  return myPCats;
}

void doSomething() {
  loopSomeCats(getSomeCats());
}

int main() {
  myTDCats.push_back(Cat());
  myTDCats.push_back(Cat());
  myPCats.push_back(Cat());

  doSomething();
  std::cout << "Hello World! " << std::endl;
  return 0;
}

The result is:

$ g++ -std=c++11 -g -o test-classcall.exe test-classcall.cpp
test-classcall.cpp: In function ‘void doSomething()’:
test-classcall.cpp:36:29: error: no matching function for call to ‘loopSomeCats(const std::vector<Cat>&)’
   loopSomeCats(getSomeCats());
                             ^
test-classcall.cpp:36:29: note: candidates are:
test-classcall.cpp:20:6: note: void loopSomeCats()
 void loopSomeCats() {
      ^
test-classcall.cpp:20:6: note:   candidate expects 0 arguments, 1 provided
test-classcall.cpp:24:6: note: void loopSomeCats(TDVectCats&)
 void loopSomeCats(TDVectCats& incats) {
      ^
test-classcall.cpp:24:6: note:   no known conversion for argument 1 from ‘const std::vector<Cat>’ to ‘TDVectCats& {aka std::vector<Cat>&}’

What especially confuses me, is the last "no known conversion for argument 1 from 'const std::vector<Cat>' to 'TDVectCats& {aka std::vector<Cat>&}'", as if it cannot convert a vector of something, into the vector of the same something, just because of typedef ? Or it maybe has to do with the const - but I simply cannot see what I need to change, in order to have a call like loopSomeCats(getSomeCats()); succeed...

You can't pass a reference to a const object to a non-const reference.

loopSomeCats takes a std::vector<Cat>& as argument, and you want to pass a const std::vector<Cat>& to it, but that's not possible.

The const would mean that you don't want anyone to modify the return value, but if you pass it to a function which just takes a non-const reference, then theoretically the function can modify the reference, and you don't want that.

You should drop the const if you want the return value to be modified.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM