I wrote a function, but the compiler shouts that there are errors. But I do not know what is wrong. Look ply. It screams on line 8 and on if

std::vector<std::vector<std::shared_ptr<GameObject>>> GroupGameObjectsByMaterial(std::shared_ptr<std::vector<std::shared_ptr<GameObject>>> gameObjects) { for (std::shared_ptr<GameObject> gameObject : *gameObjects) { std::sort(*gameObjects->begin(), *gameObjects->end(), Comp); } std::vector<std::vector<std::shared_ptr<GameObject>>> gameObjectsByMaterial; gameObjectsByMaterial.reserve(gameObjects->size); for (auto it = gameObjects->begin(); it != gameObjects->end(); ++it) { if (it != gameObjectsByMaterial[gameObjectsByMaterial.size - 1]) { gameObjectsByMaterial.push_back(it); } } return gameObjectsByMaterial; } 

Problem lines:

  if (it != gameObjectsByMaterial[gameObjectsByMaterial.size() - 1]) { gameObjectsByMaterial.push_back(*it); } 

Errors:

 Severity Code Description Project File Line Suppression State Error C2664 'void std::vector<std::vector<std::shared_ptr<GameObject>,std::allocator<_Ty>>,std::allocator<std::vector<_Ty,std::allocator<_Ty>>>>::push_back(const std::vector<_Ty,std::allocator<_Ty>> &)': cannot convert argument 1 from 'std::shared_ptr<GameObject>' to 'std::vector<std::shared_ptr<GameObject>,std::allocator<_Ty>> &&' GameLoader Severity Code Description Project File Line Suppression State Error (active) no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=std::vector<std::shared_ptr<GameObject>, std::allocator<std::shared_ptr<GameObject>>>, _Alloc=std::allocator<std::vector<std::shared_ptr<GameObject>, std::allocator<std::shared_ptr<GameObject>>>>]" matches the argument list GameLoader Severity Code Description Project File Line Suppression State Error (active) no operator "!=" matches these operands GameLoader 
  • and what a joke to put the author -1? Or are all the gurus here gathered?) - dimaAf
  • I do not put a minus yet, but the question requires clarification (for this there is a button to edit ). 1. Problem lines could at least be marked with a comment, or numbered everything, since you refer to the number. 2. How exactly does the compiler swear? give the error text (the text of course, not a screenshot) - rdorn
  • @rdorn threw in additions - dimaAf

1 answer 1

First of all, start by saying that size is a method of the vector class, and of any container. Therefore, it should be called:

 gameObjectsByMaterial.size 

It should be replaced by

 gameObjectsByMaterial.size() 

Secondly, in if you compare it , which is an iterator with the vector element gameObjectsByMaterial , which is of type std::vector<std::shared_ptr<GameObject>> . Of course, the operator != not defined for them. And even if you rename an iterator ( *it ), you get an object of type std::shared_ptr<GameObject> , which also cannot be compared with a vector of these elements. What do you really want to compare?


Third, look at the sorting:

 for (auto gameObject : *gameObjects) // заменил тип на auto { std::sort(*gameObjects->begin(), *gameObjects->end(), Comp); } 

You may notice that the gameObject is not used inside the loop body. That is, we call sorting from the same arguments several times. What for? One time is enough to sort. Moreover. Let's see which types of variables we pass there: *gameObjects->begin() . Priority -> above. So first we get an iterator of the object std :: vector <...>, and then for some reason we dereference it and get a shared_ptr<GameObject> , which we send to sort , but sort that the iterator wants. And you want to bring an iterator there too, right? What are you actually sorting?


Probably what you want is done like this in a couple of lines:

 auto sort_del_dup(std::shared_ptr<std::vector<std::shared_ptr<GameObject>>> gameObjects) { std::sort(gameObjects->begin(), gameObjects->end(), Comp); // отсортировали gameObjects->erase(std::unique(gameObjects->begin(), gameObjects->end(), [](auto lhs, auto rhs) { return *lhs == *rhs; }), gameObjects->end()); } 

first sort and then delete the same objects.

  • But to end up with such scary things as "std :: vector <std :: vector <std :: shared_ptr <GameObject >>>" should be put into typedef, but better by two :) - slav-yrich
  • I need that the values ​​do not repeat. For example, 11 22 456 I need to register for example, for example 123456 - dimaAf
  • and how then if to remake that the program worked? - dimaAf
  • @dimaAf You want to get a two-dimensional array as a result. Which one - retorta
  • @retorta, no, I have a vector with values. I want to sort and display them. but lest they repeat. therefore, if I make a check that the neighbors are not the same. so I create another vector and write down the already ostorted values ​​there and display it on the screen - dimaAf