Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: finding item in vector help please  (Read 1972 times)

0 Members and 3 Guests are viewing this topic.

xj76

  • Newbie
  • *
  • Posts: 1
    • View Profile
finding item in vector help please
« on: September 04, 2011, 12:11:38 am »
Hi, I'm having trouble accessing an item within a vector. what I wanted is to somehow get the id (perhaps) of the correct sprite that's been inserted into a vector. The second code is an attempt to this but produces the errors below.

The tiling procedures are within the first code box.

      
Code: [Select]

std::vector<std::vector<int> > mapTiles; // contains the id of the tiles
std::vector<sf::Sprite> map; // contains the tiles itself (3072)

sf::Image mapImage;
mapImage.LoadFromFile("map.bmp");
// Create tiles (3072 sprites)
sf::Image tileset;
tileset.LoadFromFile("land.jpg");
tileset.SetSmooth(false);

sf::Image tileset2;
tileset2.LoadFromFile("post.png");
tileset2.SetSmooth(false);

sf::Image tileset3;
tileset3.LoadFromFile("tree.png");
tileset3.SetSmooth(false);

sf::Image tileset4;
tileset4.LoadFromFile("dirt.jpg");
tileset4.SetSmooth(false);

sf::Image tileset5;
tileset5.LoadFromFile("boat.png");
tileset5.SetSmooth(false);

for (int y=0; y<41; y++)
{
for (int x=0; x<72; x++)
{
sf::Color Color = mapImage.GetPixel(x,y);
if(Color == sf::Color::Black)
{
sf::Sprite spriteLand;
spriteLand.SetImage(tileset);
spriteLand.SetPosition(x*10, y*10);
map.push_back(spriteLand);
}

if(Color == sf::Color(255,0,0))
{
sf::Sprite sprite;
sprite.SetImage(tileset2);
sprite.SetPosition(x*10, y*10);
map.push_back(sprite);
}

if(Color == sf::Color(0,0,255))
{
sf::Sprite sprite;
sprite.SetImage(tileset3);
sprite.SetPosition(x*10, y*10);
map.push_back(sprite);
}

if(Color == sf::Color(0,255,0))
{
sf::Sprite sprite;
sprite.SetImage(tileset4);
sprite.SetPosition(x*10, y*10);
map.push_back(sprite);
}
if(Color == sf::Color(0,255,255))
{
sf::Sprite sprite;
sprite.SetImage(tileset5);
sprite.SetPosition(x*10, y*10);
map.push_back(sprite);
}
}
}

   This is what's causing the error.
   Sprite is related to this code:

     sf::Image Image;
     if (!Image.LoadFromFile("player.png"))
         return EXIT_FAILURE;
     sf::Sprite Sprite(Image);
     Sprite.SetPosition(320.0f,240.0f);
     Image.SetSmooth(false);

   
Code: [Select]

std::vector<sf::Sprite>::iterator p;
p = find(map.begin(),map.end(),(Sprite.GetPosition().x*10)*(Sprite.GetPosition().y*10));





error
Code: [Select]


g++ -Wall -c "untitled.cpp" (in directory: /home/casper/Desktop/SFML)
untitled.cpp: In function ‘int main()’:
untitled.cpp:195: warning: comparison between signed and unsigned integer expressions
untitled.cpp:217: error: no matching function for call to ‘Collision::BoundingBoxTest(__gnu_cxx::__normal_iterator<sf::Sprite*, std::vector<sf::Sprite, std::allocator<sf::Sprite> > >&, sf::Sprite&)’
Collision.cpp:153: note: candidates are: static bool Collision::BoundingBoxTest(const sf::Sprite&, const sf::Sprite&)
In file included from /usr/include/c++/4.4/algorithm:62,
                 from /usr/local/include/SFML/Graphics/Rect.hpp:31,
                 from /usr/local/include/SFML/Graphics/Glyph.hpp:32,
                 from /usr/local/include/SFML/Graphics/Font.hpp:34,
                 from /usr/local/include/SFML/Graphics.hpp:34,
                 from untitled.cpp:2:
/usr/include/c++/4.4/bits/stl_algo.h: In function ‘_RandomAccessIterator std::__find(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<sf::Sprite*, std::vector<sf::Sprite, std::allocator<sf::Sprite> > >, _Tp = float]’:
/usr/include/c++/4.4/bits/stl_algo.h:4224:   instantiated from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator<sf::Sprite*, std::vector<sf::Sprite, std::allocator<sf::Sprite> > >, _Tp = float]’
untitled.cpp:215:   instantiated from here
/usr/include/c++/4.4/bits/stl_algo.h:174: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = sf::Sprite*, _Container = std::vector<sf::Sprite, std::allocator<sf::Sprite> >]() == __val’
/usr/include/c++/4.4/bits/stl_algo.h:4224:   instantiated from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator<sf::Sprite*, std::vector<sf::Sprite, std::allocator<sf::Sprite> > >, _Tp = float]’
untitled.cpp:215:   instantiated from here
/usr/include/c++/4.4/bits/stl_algo.h:178: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = sf::Sprite*, _Container = std::vector<sf::Sprite, std::allocator<sf::Sprite> >]() == __val’
/usr/include/c++/4.4/bits/stl_algo.h:182: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = sf::Sprite*, _Container = std::vector<sf::Sprite, std::allocator<sf::Sprite> >]() == __val’
/usr/include/c++/4.4/bits/stl_algo.h:186: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = sf::Sprite*, _Container = std::vector<sf::Sprite, std::allocator<sf::Sprite> >]() == __val’
/usr/include/c++/4.4/bits/stl_algo.h:194: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = sf::Sprite*, _Container = std::vector<sf::Sprite, std::allocator<sf::Sprite> >]() == __val’
/usr/include/c++/4.4/bits/stl_algo.h:198: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = sf::Sprite*, _Container = std::vector<sf::Sprite, std::allocator<sf::Sprite> >]() == __val’
/usr/include/c++/4.4/bits/stl_algo.h:202: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = sf::Sprite*, _Container = std::vector<sf::Sprite, std::allocator<sf::Sprite> >]() == __val’
Compilation failed.

Haikarainen

  • Guest
finding item in vector help please
« Reply #1 on: September 04, 2011, 08:40:23 am »
You posted really unrelevant code. Do you read your errors? Google them and you shall learn!



Code: [Select]
untitled.cpp:217:[b] error: no matching function for call to ‘Collision::BoundingBoxTest[/b](__gnu_cxx::__normal_iterator<sf::Sprite*, std::vector<sf::Sprite, std::allocator<sf::Sprite> > >&, sf::Sprite&)’
[b]Collision.cpp:153: note: candidates are: static bool Collision::BoundingBoxTest(const sf::Sprite&, const sf::Sprite&) [/b]


Basically you use the function BoundingBoxTest() wrong, it should have 2 sprites and not some vector stuff.

Dont wanna look up all your other errors when i see no related code, but this is how you iterate a vector, with a custom class example; https://legacy.sfmluploads.org/code/79

You would find this information if you learn to google!

std::vector iterate - http://www.google.se/search?q=std%3A%3Avector+iterate&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

std::vector - http://www.google.se/search?q=std%3A%3Avector+iterate&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a#sclient=psy&hl=sv&client=firefox-a&hs=RK7&rls=org.mozilla:en-US%3Aofficial&source=hp&q=std:%3Avector&pbx=1&oq=std:%3Avector&aq=f&aqi=&aql=&gs_sm=e&gs_upl=38760l39197l0l39347l3l2l0l0l0l0l99l171l2l2l0&bav=on.2,or.r_gc.r_pw.&fp=47a1a6b6d84e321d&biw=1920&bih=1003

Also, you find a lot of information on http://www.cplusplus.com/reference/stl/vector/

Edit; Btw , you might wanna replace those dots with ->

 

anything