C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-30-2008, 02:53 AM   #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));

	
}
eklavya8 is offline   Reply With Quote
Old 06-30-2008, 02:55 AM   #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.
MacGyver is offline   Reply With Quote
Old 06-30-2008, 03:04 AM   #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
eklavya8 is offline   Reply With Quote
Old 06-30-2008, 03:11 AM   #4
Deathray Engineer
 
MacGyver's Avatar
 
Join Date: Mar 2007
Posts: 3,211
Quote:
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.
__________________
MacGyver is offline   Reply With Quote
Old 06-30-2008, 03:12 AM   #5
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,134
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)
C_ntua is offline   Reply With Quote
Old 06-30-2008, 07:34 AM   #6
The larch
 
Join Date: May 2006
Posts: 3,082
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.

Quote:
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).
anon is online now   Reply With Quote
Old 06-30-2008, 11:15 AM   #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!
eklavya8 is offline   Reply With Quote
Old 06-30-2008, 11:53 AM   #8
The larch
 
Join Date: May 2006
Posts: 3,082
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.

Quote:
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).
anon is online now   Reply With Quote
Old 06-30-2008, 12:01 PM   #9
Registered User
 
Join Date: Jan 2008
Posts: 30
thanks a lot!
eklavya8 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:05 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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