Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ function problem need help!

Started by MasterDario Feb 26, 2006 at 3:05 PM 5 replies 700+ views
Original Post
MasterDario
MasterDario
Hey everyone, in my program I have a function to find the index of the highest value in a vector. For some reason it's not letting me store anything that is inside of the vector into a temporary variable. ex: TempValue = Scores[0]; Its giving me an error saying: error C2440: cannot convert from std::vector<_TY> to 'double with [ _TY = double ]. Anyone know what might be causing this? My function code is below. Thanks int HighestElement(vector Scores[], int ElementCount) { double TempValue; int HighestValIndex; TempValue = Scores[0]; for(int x=1; x < ElementCount; x++) { if(Scores[x]>TempValue) { TempValue = Scores[x]; HighestValIndex = x; } } return HighestValIndex; }
n/a
poss74
poss74
Have you tried casting the result:

TempValue = (double)Scores[0];
If at first you don't succeed, call it version 1.0You don't stop playing because you get old; you get old when you stop playing.
jackalope
jackalope
I don't know why you get the error (im not too familiar with std::vector) but there's a function that does what you want. I pulled this out of MSDN:

    // Define a template class vector of int    typedef vector<int > IntVector ;    //Define an iterator for template class vector of strings    typedef IntVector::iterator IntVectorIt ;    IntVector Numbers(VECTOR_SIZE) ;    start = Numbers.begin();    end = Numbers.end();    // return the maximum element in the Numbers    location = max_element(start, end) ;    cout << "The maximum element in Numbers is: "        << *location << endl ;
SiCrane
SiCrane
The problem is probably in your function declaration:

int HighestElement(vector Scores[], int ElementCount)

should be:

int HighestElement(vector Scores, int ElementCount)

or even better:

int HighestElement(const vector & Scores, int ElementCount)
MasterDario
MasterDario
Thanks SiCrane, that fixed the problem. I didn't know that when you passed a vector through a function that the [] werent necessary. Thanks again

Dario
n/a
CipherCraft
CipherCraft
SiCrane is right on the money. That's your problem and the last suggested function signature is indeed the best option.

cu,
CipherCraft
Enigma
Enigma
Quote:
Original post by SiCrane
The problem is probably in your function declaration:

int HighestElement(vector Scores[], int ElementCount)

should be:

int HighestElement(vector Scores, int ElementCount)

or even better:

int HighestElement(const vector & Scores, int ElementCount)

Or even even better:
std::max_element(scores.begin(), scores.end());

This returns an iterator which must be dereferenced to get the maximum value and as such you must be careful if your vector is empty, but then you couldn't pass an empty vector to your original function anyway.

Σnigma

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.