简体   繁体   中英

Error request for member getName which is of non-class type 'char'

I'm trying to make a program involving files assign2.cpp , Player.h , Player.cpp , Team.h , Team.cpp which reads data from a txt file on player info (like hits, atBat, position, name and number) and displays it out into assign2.cpp . assign2.cpp is what contains int main() and is suppose to contain very little code because relies on the other files to do the work.

The error:

request for member getName which is of non-class type ‘char’...

Please help, I've been trying to find the issue and can never do so. The compilation failure :

In file included from Team.cpp:1:0:
Team.h:34:11: warning: extra tokens at end of #endif directive [enabled by default]
Team.cpp: In constructor ‘Team::Team()’:
Team.cpp:15:5: warning: unused variable ‘numPlayers’ [-Wunused-variable]
Team.cpp: In member function ‘void Team::sortByName()’:
Team.cpp:49:56: error: request for member ‘getName’ in ‘((Team*)this
-> Team::playerObject[(j + -1)]’, which is of non-class type ‘char’
Team.cpp:49:74: error: request for member ‘getName’ in ‘bucket’, which is of non-class type ‘int’
Team.cpp: In member function ‘void Team::print()’:
Team.cpp:63:18: error: request for member ‘print’ in ‘((Team*)this)-                >Team::playerObject[i]’, which is of non-class type ‘char’
make: *** [Team.o] Error 1

Team.h

#ifndef TEAM_H
#define TEAM_H
#include "Player.h"

class Team
{
  private:
     char playerObject[40];
     int numPlayers; // specifies the number of Player objects
                     //   actually stored in the array

     void readPlayerData();
     void sortByName();
  public:
     Team();
     Team(char*);

     void print();
};
#endif / *Team.h*  /

Team.cpp

#include "Team.h"
#include <cstring>
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <string.h>
#include <fstream>
#include <cstdlib>

using namespace std;

Team::Team()
{
    strcpy (playerObject,"");
    int numPlayers = 0;
}

Team::Team(char* newPlayerObject)
{
    strncpy(playerObject, newPlayerObject, 40);

    readPlayerData();
}

void Team::readPlayerData()
{
    ifstream inFile;

    inFile.open("gamestats.txt");
    if (!inFile){
        cout << "Error, couldn't open file";
        exit(1);
    }

    inFile.read((char*) this, sizeof(Team));

    inFile.close();
}

void Team::sortByName()
{
    int i, j;
    int bucket;

    for (i = 1; i < numPlayers; i++)
    {
        bucket = playerObject[i];

        for (j = i; (j > 0) && (strcmp(playerObject[j-1].getName(),   bucket.getName()) > 0); j--)
            playerObject[j] = playerObject[j-1];

        playerObject[j] = bucket;
    }
}

Player.h (incase anyone needs it)

#ifndef PLAYER_H
#define PLAYER_H
class Player
{
    // Data members and method prototypes for the Player class go here
    private:
        int number;
        char name[26];
        char position[3];
        int hits;
        int atBats;
        double battingAverage;

    public:
        Player();
        Player(int, char*, char*, int, int);
        char* getName();
        char* getPosition();
        int getNumber();
        int getHits();
        int getAtBats();
        double getBattingAverage();

        void print();
        void setAtBats(int);
        void setHits(int);
};
#endif

I'm very stuck, Thanks in advance.

In the Team constructor on this line

playerObject = newPlayerObject;

you're trying to assign a value of type char* to a member of type char[40] , which doesn't work, since they are two different types. In any case, you probably would need to copy the data from the input instead of just trying to hold the pointer internally. Something like

strncpy(playerObject, newPlayerObject, 40);

Generally, you will always be able to assign a char[N] to a char* , but not the other way around, but that's just because C++ will automatically convert the char[N] to a char* , they are still different types.

Your declaration is:

char playerObject[40];

And your constructor reads:

Team::Team(char* newPlayerObject)
{
     playerObject = newPlayerObject;

The error message you referenced in the title of this question obviously comes from here, and it is self explanatory. An array and a pointer are two completely different, incompatible types, when it comes to this kind of an assignment.

What you need to do depends entirely on what you expect to happen, and what your specifications are.

A) You could be trying to initialize the array from the character pointer, in which case you'll probably want to use strcpy() . Of course, you have to make sure that the string, including the null byte terminator, does not exceed 40 bytes, otherwise this will be undefined behavior.

Incidently, this is what you did in your default constructor:

Team::Team()
{
    strcpy (playerObject,"");
}

B) Or, your playerObject class member should, perhaps, be a char * instead, and should be either assigned, just like that, or perhaps strdup ()ed. In which case your default constructor will probably need to do the same.

Whichever one is the right answer for you depends entirely on your requirements, that you will have to figure out yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM