C++;类对象作为其他类函数中的参数 我在VisualStudio 2015中有一个C++项目。 GameManager.h和Input.h都给了我一个语法错误:标识符“Player”。这是因为我想将一个对象类型为“播放器”作为一个参数< /强>,以在这两个头文件及其相应的C++文件中运行。 我该如何解决这个问题?有关更多信息,我已提供了我的代码。 main.cpp: #include "GameManager.h" #include "Input.h" #include "Player" #include <iostream> #include <string> using namespace std; const int maxPlayerCnt = 10; static Player p1, p2, morePlayers[maxPlayerCnt]; int main() { GameManager game; game.Game(p1, p2, morePlayers); return 0; }
GameManager.cpp:C++;类对象作为其他类函数中的参数 我在VisualStudio 2015中有一个C++项目。 GameManager.h和Input.h都给了我一个语法错误:标识符“Player”。这是因为我想将一个对象类型为“播放器”作为一个参数< /强>,以在这两个头文件及其相应的C++文件中运行。 我该如何解决这个问题?有关更多信息,我已提供了我的代码。 main.cpp: #include "GameManager.h" #include "Input.h" #include "Player" #include <iostream> #include <string> using namespace std; const int maxPlayerCnt = 10; static Player p1, p2, morePlayers[maxPlayerCnt]; int main() { GameManager game; game.Game(p1, p2, morePlayers); return 0; },c++,function,class,object,parameter-passing,C++,Function,Class,Object,Parameter Passing,GameManager.cpp: #include "GameManager.h" void GameManager::Game(Player p1, Player p2, Player morePlayers[]) { int playerCnt = 0; Input input; input.getPlayerDetails(playerCnt, p2); input.getMorePlayerDetails(playerCnt, morePlayers);
#include "GameManager.h"
void GameManager::Game(Player p1, Player p2, Player morePlayers[])
{
int playerCnt = 0;
Input input;
input.getPlayerDetails(playerCnt, p2);
input.getMorePlayerDetails(playerCnt, morePlayers);
}
一旦我解决了这个问题,它将创建一个Input类型的对象来使用更多的函数,并将获得更多的代码。然后使用从main.cpp获取的特定参数调用函数
投入.h:
#pragma一次
#包括“Player.h”
#包括
#包括
使用名称空间std;
类输入
{
公众:
输入();
void getPlayerDetails(int&playerNum,playerP);
void getMorePlayerDetails(int&playerNum,playerP[]);
};
它包括Input.cpp需要的所有内容并初始化函数
Input.cpp:
#include "Input.h"
void Input::getPlayerDetails(int &playerNum, Player p)
{
playerNum++;
string currentName;
char currentSymbol;
cout << "Player " << playerNum << ", what is your name?\n";
cin >> currentName;
p.setName(currentName);
cout << currentName << " what is your symbol?\n";
cin >> currentSymbol;
p.setSymbol(currentSymbol);
}
void Input::getMorePlayerDetails(int &playerNum, Player p[])
{
int plNum = playerNum;
if (playerNum >= 12)
cout << "You can't get another player!\n";
else
{
//getPlayerDetails(p[playerNum - 2], (plNum - 2));
}
}
#包括“Input.h”
无效输入::getPlayerDetails(int&playerNum,playerP)
{
playerNum++;
字符串currentName;
字符符号;
cout=12)
你能不能显示Player.h
?你可能需要将Player
声明为类Player
,编译器在哪里抱怨它。此外,你可能想通过引用传递Player
,所以使用Player&p
而不是Player p
一个猜测是你包含了一些ot她的头在Player.h中,创建了一个循环包含链。我试图通过引用传递Player。但首先,我不能通过引用传递数组,即使使用普通的Player p1和p2,我也无法解决这个问题。我知道这不是解决方案,但也许你应该使用std::vector作为morePlayers变量。并通过常量引用传递它(常量标准::向量和vp)。您可以显示main.cpp includes吗?您可以显示Player.h
吗?您可能需要将Player
声明为class Player
,编译器对此进行投诉。此外,您可能希望通过引用传递Player
,因此请使用Player&p
而不是Player p
一次猜测在Player.h中包含一些其他头,并创建一个循环包含链。我尝试通过引用传递Player。但首先,我无法通过引用传递数组,即使使用普通的Player p1和p2,我也无法解决这个问题。我知道这不是解决方案,但也许您应该使用std::vector for morePlayers变量。并通过const引用(const std::vector&vp)传递它。您能显示main.cpp包含的内容吗?
#pragma once
#include "Player.h"
#include <iostream>
#include <string>
using namespace std;
class Input
{
public:
Input();
void getPlayerDetails(int &playerNum, Player p);
void getMorePlayerDetails(int &playerNum, Player p[]);
};
#include "Input.h"
void Input::getPlayerDetails(int &playerNum, Player p)
{
playerNum++;
string currentName;
char currentSymbol;
cout << "Player " << playerNum << ", what is your name?\n";
cin >> currentName;
p.setName(currentName);
cout << currentName << " what is your symbol?\n";
cin >> currentSymbol;
p.setSymbol(currentSymbol);
}
void Input::getMorePlayerDetails(int &playerNum, Player p[])
{
int plNum = playerNum;
if (playerNum >= 12)
cout << "You can't get another player!\n";
else
{
//getPlayerDetails(p[playerNum - 2], (plNum - 2));
}
}
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Player
{
private:
string _name;
char _symbol;
public:
Player();
void getName();
void setName(string name);
void setSymbol(char symbol);
};
#include "Player.h"
Player::Player()
{
}
void Player::getName()
{
cout << "I have no name!\n";
}
void Player::setName(string name)
{
_name = name;
}
void Player::setSymbol(char symbol)
{
_symbol = symbol;
}