非class类型'char'成员getName的错误请求

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

我正在尝试制作一个涉及文件 assign2.cppPlayer.hPlayer.cppTeam.hTeam.cpp 的程序,该程序从 txt 文件中读取数据播放器信息文件(如命中、atBat、位置、名称和编号)并将其显示到 assign2.cppassign2.cpp 是包含 int main() 的内容,并且应该包含很少的代码,因为依赖于其他文件来完成工作。

错误:

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

请帮忙,我一直在努力寻找问题所在,但一直找不到。编译失败:

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(以防万一有人需要)

#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

我很困惑,提前致谢。

Team构造器这一行

playerObject = newPlayerObject;

您正试图将类型 char* 的值分配给类型 char[40] 的成员,这不起作用,因为它们是两种不同的类型。在任何情况下,您可能都需要从输入中复制数据,而不是仅仅试图在内部保存指针。像

strncpy(playerObject, newPlayerObject, 40);

通常,您总是可以将 char[N] 分配给 char*,但不能反过来,但这只是因为 C++ 会自动将 char[N] 转换为achar*,还是不同的类型

您的声明是:

char playerObject[40];

并且您的构造函数读取:

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

你在这个问题的标题中引用的错误消息显然来自这里,而且是不言自明的。数组和指针是两种完全不同的、不兼容的类型,当涉及到这种赋值时。

您需要做什么完全取决于您期望发生什么,以及您的规格是什么。

A) 您可能会尝试从字符指针初始化数组,在这种情况下您可能希望使用 strcpy()。当然,你得保证这个字符串,包括空字节终止符,不超过40个字节,否则这就是未定义的行为。

顺便说一句,这是您在默认构造函数中所做的:

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

B) 或者,您的 playerObject class 成员也许应该是 char *,并且应该像那样分配,或者 strdup()编辑。在这种情况下,您的默认构造函数可能需要执行相同的操作。

哪个是您的正确答案完全取决于您的要求,您必须自己弄清楚。