2

I'm trying to iterate through ArrayPlayers to find the oldest player. The logic would be to have a final pointer Player *Oldest to the oldest player. Here is my code:

#include <stdio.h>
#include <tchar.h>
#include <iostream>
using namespace std;

class Player {
    int Age;
    char *Name;
public:
    Player() {}

    void setName(char *n) {
        Name = n;
    }

    void setAge(int a) {
        Age = a;
    }

    bool operator > (Player &P) {
        return Age > P.Age;
    }
};

int main()
{
    int NumPlayers;

    cout << "How many persons do you want to enter?\n";
    cin >> NumPlayers;

    Player *ArrayPlayers = new Player[NumPlayers];

    int age;
    char *name = new char;
    for (int i = 0; i < NumPlayers; i++) {
        cout << "Enter player name: ";
        cin >> name;
        cout << "Enter player age: ";
        cin >> age;
        ArrayPlayers[i].setName(name);
        ArrayPlayers[i].setAge(age);
    }

    //Find oldest player
    Player *Current = ArrayPlayers[0];
    Player *Previous = NULL;
    Player *Oldest = NULL;
    while (Current) {
        if (!Previous) {
            Oldest = Current;
            Current = Current + 1;
        } else {
            //Make comparison using overloading operator >
            if (Current > Oldest) { 
                Oldest = Current;
                Current = Current + 1;
            }
        }
    }

    return 0;
}

What happens is that the following instruction is not permitted:

Player *Current = ArrayPlayers[0];

error C2440: 'initializing' : cannot convert from 'Player' to 'Player *'

Basically here I wanted to set a pointer to the first element of the array.

Can you point the reason for this error please? Do you see any other issues?

Thanks for your help.

5
  • Avoid dynamic allocation (new). Read up on "const correctness", as your comparison operator wants write-access to the involved operands. Check out std::string. Commented Jan 24, 2015 at 19:28
  • Alternatively, make a std::vector<Player> then you can use std::max_element Commented Jan 24, 2015 at 19:29
  • 2
    Unrelated, but whenever you think "dynamic array" you should next think std::vector. Commented Jan 24, 2015 at 19:30
  • Unrelated, but using a plain char* for the string means that you have to manage the memory it is pointing to (which the code does not). Consider using std::string instead, which does that management for you. Commented Jan 24, 2015 at 19:35
  • Thanks for the tips. In this case I can't use std::string or std::vector though. Commented Jan 24, 2015 at 19:38

1 Answer 1

2

Replace

Player *Current = ArrayPlayers[0];

with

Player *Current = &ArrayPlayers[0];

If it was not a typo (which I don't believe since the error message was pretty informative)
consider reading this.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, it is working now. So basically the pointer should always hold a memory address.
Actually just found out that Oldest will be empty after the while loop. Is there any other flaws in the code?
@Sam32 define "empty"
Tried to debug and it's showing {Age=??? Name=???}. I needed to display the name of the player on the screen after the while loop.
@Sam32 In that case Oldest is probably null. Also Current > Oldest does not do what you think it does. Read the tutorial.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.