gpt4 book ai didi

c++ - 非类类型成员getName请求错误 'char'

转载 作者:行者123 更新时间:2023-11-28 06:25:59 24 4
gpt4 key购买 nike

我正在尝试制作一个涉及文件 assign2.cppPlayer.hPlayer.cppTeam 的程序.h, Team.cpp 从 txt 文件中读取玩家信息数据(如命中、atBat、位置、名称和号码)并将其显示到 assign2.cpp assign2.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

团队.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* /

团队.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] 转换为 char*,它们仍然是不同的类型。

关于c++ - 非类类型成员getName请求错误 'char',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28522871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com