stl error

This is a discussion on stl error within the C++ Programming forums, part of the General Programming Boards category; Hi! dont spend more than 10 minutes reading this,I probably should not even be posting this. Any way I have ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    30

    stl error

    Hi!
    dont spend more than 10 minutes reading this,I probably should not even be posting this.
    Any way I have this piece of code which has some syntax error in the STL part and I cannot make any sense of the error message I get , below is the code just glance through it to see if u can find any glaring mistakes.
    thanks!

    -----------------------------------------------------------------------------------------------------------------
    Code:
    #define sz(a) int((a).size()) 
    #define pb push_back 
    #define all(c) (c).begin(),(c).end() 
    #define REP(i,n) for(int _n=n, i=0;i<_n;++i)//for loop
    #define max 1000001
    
    
    class KiloManX {
    public:
    	int leastShots(vector <string>, vector <int>);
    };
    
    int KiloManX::leastShots(vector <string> damageChart, vector <int> bossHealth) 
    {
    int n,m;
    n=sz(damageChart);
    int i,j;
    vector<int> damage[n];
    REP(i,n)
    REP(j,n)
    {
    	damage[i].push_back( ceil( bossHealth[j]/float(damageChart[i][j]-'0') ) );
    }
    
    vector<int> d(n,max);
    set<pair<int,int> > q;
    int shots=1;
    int start=bossHealth.begin()-find(all(bossHealth),min_element(all(bossHealth) ) );
    d[start]=*min_element(all(bossHealth));
    
    q.insert(make_pair(d[start],start));
    
    while(!q.empty())
    {
    	pair<int ,int> top;
    	top=*q.begin();
    	q.erase(q.begin());
    	int cost=top.first;
    	int index=top.second;
    	REP(j,n)
    	{
    		if(d[j]>damage[index][j]+cost)
    		{
    			if(d[j]!=max)
    			q.erase(q.find(make_pair(d[j],j)));
    			
    			d[j]=damage[index][j]+cost;
    			q.insert(make_pair(d[j],j));
    		}
    		
    	}
    	
    	
    }
    
    return *max_element(all(d));
    
    	
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Posting the error message would have been a good move if you want us to fix syntax errors.

    Edit: And an indication where the line is that is associated with the error.
    Last edited by MacGyver; 06-30-2008 at 02:57 AM.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    30
    Code:
    #include <vector>
    #include <list>
    #include <map>
    #include <set>
    #include <deque>
    #include <stack>
    #include <bitset>
    #include <algorithm>
    #include <functional>
    #include <numeric>
    #include <utility>
    #include <sstream>
    #include <iostream>
    #include <iomanip>
    #include <cstdio>
    #include <cmath>
    #include <cstdlib>
    #include <ctime>
    
    #define sz(a) int((a).size()) 
    #define pb push_back 
    #define all(c) (c).begin(),(c).end() 
    #define REP(i,n) for(int _n=n, i=0;i<_n;++i)
    #define FOREACH(it,c) for(typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
    #define max 1000001
    using namespace std;
    
    class KiloManX {
    public:
    	int leastShots(vector <string>, vector <int>);
    };
    
    int KiloManX::leastShots(vector <string> damageChart, vector <int> bossHealth) 
    {
    int n,m;
    n=sz(damageChart);
    int i,j;
    vector<int> damage[n];
    REP(i,n)
    REP(j,n)
    {
    	if(damageChart[i][j]!='0')
    	damage[i].push_back( ceil( bossHealth[j]/float(damageChart[i][j]-'0') ) );
    	else
    	damage[i].pb(max);
    }
    
    vector<int> d(n,max);
    set<pair<int,int> > q;
    int shots=1;
    int start=bossHealth.begin()-find(all(bossHealth),min_element(all(bossHealth) ) );
    d[start]=*min_element(all(bossHealth));
    
    q.insert(make_pair(d[start],start));
    
    while(!q.empty())
    {
    	pair<int ,int> top;
    	top=*q.begin();
    	q.erase(q.begin());
    	int cost=top.first;
    	int index=top.second;
    	REP(j,n)
    	{
    		if(d[j]>damage[index][j]+cost)
    		{
    			if(d[j]!=max)
    			q.erase(q.find(make_pair(d[j],j)));
    			
    			d[j]=damage[index][j]+cost;
    			q.insert(make_pair(d[j],j));
    		}
    		
    	}
    	
    	
    }
    
    return *max_element(all(d));
    
    	
    }
    //error code
    KiloManX.cpp:42: warning: passing `float' for converting 1 of `void std::vector<_Tp, _Alloc>:ush_back(const _Tp&) [with _Tp = int, _Alloc = std::allocator<int>]'
    KiloManX.cpp: In function `int main()':
    KiloManX.cpp:228: warning: converting to `int' from `double'
    KiloManX.cpp:232: warning: converting to `int' from `double'
    KiloManX.cpp:236: warning: converting to `int' from `double'
    KiloManX.cpp:240: warning: converting to `int' from `double'
    KiloManX.cpp:244: warning: converting to `int' from `double'
    C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_algo.h: In function `_RandomAccessIterator std::find(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, _Tp = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >]':
    C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_algo.h:314: instantiated from `_InputIterator std::find(_InputIterator, _InputIterator, const _Tp&) [with _InputIterator = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, _Tp = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >]'
    KiloManX.cpp:48: instantiated from here
    C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_algo.h:207: error: no match for 'operator==' in '(&__first)->__gnu_cxx::__normal_iterator<_Iterator, _Container>:perator* [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >]() == __val'
    C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_algo.h:211: error: no match for 'operator==' in '(&__first)->__gnu_cxx::__normal_iterator<_Iterator, _Container>:perator* [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >]() == __val'
    C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_algo.h:215: error: no match for 'operator==' in '(&__first)->__gnu_cxx::__normal_iterator<_Iterator, _Container>:perator* [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >]() == __val'
    C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_algo.h:219: error: no match for 'operator==' in '(&__first)->__gnu_cxx::__normal_iterator<_Iterator, _Container>:perator* [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >]() == __val'
    C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_algo.h:227: error: no match for 'operator==' in '(&__first)->__gnu_cxx::__normal_iterator<_Iterator, _Container>:perator* [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >]() == __val'
    C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_algo.h:231: error: no match for 'operator==' in '(&__first)->__gnu_cxx::__normal_iterator<_Iterator, _Container>:perator* [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >]() == __val'
    C:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_algo.h:235: error: no match for 'operator==' in '(&__first)->__gnu_cxx::__normal_iterator<_Iterator, _Container>:perator* [with _Iterator = int*, _Container = std::vector<int, std::allocator<int> >]() == __val'
    Compiling errors




    Ps:
    Can anyone tell me how to make sense of the error messages especially when most of the line numbers mentioned dont exist in the code.EG-235

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    KiloManX.cpp:42: warning: passing `float' for converting 1 of `void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = int, _Alloc = std::allocator<int>]'
    This is where your first issue is:

    Code:
    damage[i].push_back( ceil( bossHealth[j]/float(damageChart[i][j]-'0') ) );
    It appears damage[i] is a vector of ints, but you're passing it a float.

    You have similar issues elsewhere, but you didn't post the code for those. In general, fix the first and most obvious errors that the compiler complains about. Sometimes the later errors the compiler complains about don't really exist, but the compiler thinks they do because of the issues it has encountered up to that point.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    So your problem is probably here:
    damage[i].push_back( ceil( bossHealth[j]/float(damageChart[i][j]-'0') ) );
    damage is an vector<int>
    ceil() returns a double. So cast (I suppose)

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Look at the line that produces that error and then look up the return type of std::min_element (and the argument types of std::find).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    30
    hi all!
    Thanx for all the help.
    the line
    int start=bossHealth.begin()-find(all(bossHealth),min_element(all(bossHealth) ) );

    had the bug so I replaced it and now i am fine,but I still cant figure out whats wrong with it.
    We are allowed to subtract iterators like pointers right?
    1)find returns an iterator
    min_element returns an int

    I just cant figure it?
    cheers!

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    min_element returns an iterator, find expects an int as the third argument. You need to dereference the return value of min_element to pass it on to find (you also have to be sure that bossHealth is not empty, otherwise you can't dereference the iterator).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    30
    thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  3. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  4. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 06:17 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

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