好的,这是我的问题,我在一个类中声明了一个全局数组。我在该类中使用一个函数为数组赋值,这些值作为参数传递给该函数。 (这工作)然后我有另一个函数,它与数组和第一个函数在同一个类中,但是从另一个类调用;但是当从另一个类调用它时,全局数组全部为零。
This is a picture to the actual issue in the function that is not working
Here is the function that saves the data to the array, as you can see it works
以下是代码示例:
//
// Player.cpp
// Battleship
//
// Created by Cody Flies on 5/25/16.
// Copyright © 2016 Cody Flies. All rights reserved.
//
#include "Player.hpp"
#include <string.h>
#include "Utility.hpp"
std::string Player::getName(){
return name;
}
void Player::setName(std::string name){
this->name = name;
}
int Player::getNumberShipsLeft(){
return numbershipsleft;
}
void Player::setNumberOfShipsLeft(int numberOfShipsLost){
this->numbershipsleft -= numberOfShipsLost;
}
/// set the location of the ships and store them in the players variables
void Player::setCarrierLocation(int x, int y, int direction){
carrierLocation[0] = x;// <5 is x values
carrierLocation[5] = y;// >= 5 is y values
if(direction == 1){
for(int i = 0; i<5; i++){
carrierLocation[i] = i+x;
}
for(int i = 5; i<10; i++){
carrierLocation[i] = y;
}
}
if(direction == 0){
for(int i = 5; i<10; i++){
carrierLocation[i] = i+y-5;
}
for(int i = 0; i<5; i++){
carrierLocation[i] = x;
}
}
}
void Player::printContents(){
for (auto i : carrierLocation) std::cout<<i<<std::endl; }
/*
void setCruiserLocation(int pos[10][10]);
void setDestroyerLocation(int pos[10][10]);
void setPatrolBoatLocation(int pos[10][10]);*/
/// get the location of the ships
bool Player::isCarrierHit(int x, int y){
for(int i = 0; i<10; i++){
for(int j = 0; j<10; j++){
if(x==carrierLocation[i]&&y==carrierLocation[j])
return true; /// this is where the carrierLocatio
/// is not holding the info.
}
}
return false;
}
/*int* getCruiserLocation(){
}
int* getDestroyerLocation();
int* getPatrolBoatLocation();*/
这里是Player.hpp ---&gt;
class Player
{
/// variables name, shipsleft
private:
std::string name;
int numbershipsleft;
//int cruiserLocation[1];
int carrierLocation[10];
/*int destroyerLocation[10][10];
int patrolBoatLocation[10][10];
*/
/// getters and setters for name and shipsleft
///
public:
std::string getName();
void setName(std::string);
int getNumberShipsLeft();
void setNumberOfShipsLeft(int);
/// set the location of the ships and store them in the players variables
void setCarrierLocation(int, int, int);/*
void setCruiserLocation(int pos[10][10]);
void setDestroyerLocation(int pos[10][10]);
void setPatrolBoatLocation(int pos[10][10]);*/
/// get the location of the ships
bool isCarrierHit(int, int);/*
int* getCruiserLocation();
int* getDestroyerLocation();
int* getPatrolBoatLocation();*/
void printContents();
};
#endif /* Player_hpp */
这里是调用对象的地方。
//
// Board.cpp
// Battleship
//
// Created by Cody Flies on 5/25/16.
// Copyright © 2016 Cody Flies. All rights reserved.
//
#include "Board.hpp"
#include <string.h>
#include <iostream>
#include "Player.hpp"
using namespace std;
void Board::createBoard(){
int shipPlacement, startCol, startRow, shipSize;
char matrix[10][10];
//call method to get user information and set ship locations
userInput();
startCol = 2;
startRow = 3;
shipSize = 5;
string value;
for(int row = 0; row<10; row++){
for(int column = 0; column<10; column++){
matrix[row][column] = 126;
if(player1.isCarrierHit(row, column)){
matrix[row][column] = 'A';
}
}
}
string space = " ";
cout << " 1 2 3 4 5 6 7 8 9 10" << endl;
for(int i = 0; i<10; i++){
if(i == 9){
space = " ";
}
cout << i+1 << space;
for(int j = 0; j<10; j++){
cout << matrix[i][j] << " ";
}
cout << endl;
}
player1.printContents();
}
void Board::userInput(){
string playername;
cout << "how many players? " << endl;
int players;
cin >> players;
if(players == 1){
player2.setName("Computer");
cout << "what is your name" << endl;
cin >> playername;
player1.setName(playername);
placeShips(player1);
}
else if(players == 2){
cout << "what is your name player 1" << endl;
cin >> playername;
player1.setName(playername);
cout << "what is your name player 2" << endl;
cin >> playername;
player2.setName(playername);
placeShips(player1);
placeShips(player2);
}
cout << "your name is: " << player1.getName() << " and your opponents name is: " << player2.getName() << endl;
}
void Board::placeShips(Player player){
int coord[2];
int direction;
cout << "what coordinates would you like your Carrier to start"<<endl;
cin >> coord[0] >> coord[1];
cout << "what direction would you like your ship 0 for vertical 1 for horizontal" << endl;
cin >> direction;
player.setCarrierLocation(coord[0], coord[1], direction);
}
Board::Board(){
}
和Board.hpp
//
// Board.hpp
// Battleship
//
// Created by Cody Flies on 5/25/16.
// Copyright © 2016 Cody Flies. All rights reserved.
//
#ifndef Board_hpp
#define Board_hpp
#include <stdio.h>
#include "Utility.hpp"
#include "Player.hpp"
class Board
{
private:
Player player2, player1;
public:
// a method to create the board
void createBoard();
bool isShip(char, char, char, char);
void placeShips(Player);
int attachShip(int, int, int, int);
void userInput();
Board();
};
#endif /* Board_hpp */
和主文件
//
// main.cpp
// Battleship
//
// Created by Cody Flies on 5/25/16.
// Copyright © 2016 Cody Flies. All rights reserved.
//
#include <iostream>
#include "Board.hpp"
#include "Player.hpp"
using namespace std;
int main(int argc, const char * argv[]) {
//run BattleSpace Class
Board board;
board.createBoard();
//carrier 5
//battleship 4
//crusier 3
//sub 3
//destroyer 2
return 0;
}
此代码是我在Xcode中使用的代码。
答案 0 :(得分:3)
这个问题包含几个可能的问题:一些可疑的陈述和许多误导性的名称和评论。
类是用于创建对象的模板,对象是类的实例。因此,变量名aClass
具有误导性。
在课堂外定义全局变量。 成员变量是在类中定义的变量,并且该类的每个对象都有一个特定的实例。变量a::globalArray
是标准成员变量。 (还有静态成员变量,可以认为是 class-global 。但是,像a::globalArray
这样的非静态成员变量只是对象本地并且评论不能改变这一点。)
成员函数(方法)a::assignToArray(int number)
(当前正在编写)接受一个数字,并使用以number
开头的数字填充整个数组与number + 9
。这可能是理想的行为,但我肯定会以不同的方式命名方法,例如fillArray()
。
现在让我们关注非工作方法a::isInArray()
。应该将当前实现称为arrayBeginsWith()
。为什么? 它在for循环的第一轮之后返回!它只是查看数组中的第一项,将它与参数进行比较,如果它们相等则返回true
并{{1}否则。
那么真正的false
应该是什么样子?嗯,它并没有那么不同:
isInArray()
我刚刚在for循环之后移动了bool a::isInArray(int number){
for(int i = 0; i<10; i++){ // go through the whole array
if(globalArray[i] == number){// test for equality
return true; // return ONLY IF equals
}
}
return false; // for-loop has finished now without returning true
}
语句,因此在第一轮中没有调用它。我还重写了评论以更好地匹配代码。
并且不要忘记编写结束括号return false;
来关闭这两种方法!
好的,我们现在进行测试:http://ideone.com/rzes2b
答案 1 :(得分:0)
除了您的代码的所有其他问题,问题可能源于您的术语“全局数组”,这是误导性的。
class A {
int array[10];
};
array
这里是成员变量,A类的每个实例都有自己的array
离散副本。它不是全球性的,也不是共享的。结果,这是你的问题:
void Board::placeShips(Player player){
placeShips
通过值获取播放器,这意味着它会制作副本。对player
所做的更改将影响此播放器副本,直到函数结束,此时名为player
的实例将被销毁,从而使您传入的player
不受影响。< / p>
这里你需要的是通过引用传递,因为你想要改变你被调用的实例:
void Board::placeShips(Player& player){
实际演示:
#include <iostream>
int fValue(int i) { // is ia local variable
i = 10;
} // i destroyed
int fRef(int& i) {
i = 42;
}
int main() {
int i = 1;
std::cout << "main:i=" << i << "\n";
fValue(i);
std::cout << "after fValue i=" << i << "\n"; // unchanged
fRef(i);
std::cout << "after fRef i=" << i << "\n"; // prints 42
}