所以这里是我试图用指针调用数组的主要部分:
#include <iostream>
#include "Lottery.h"
#include "Player.h"
#include "LotteryData.h"
using namespace std;
int main()
{
Player player;
Lottery random;
LotteryData data;
player.Input();
random.setRandomNumber();
data.PassInfo(int (&Numbers)[6][6]);
}
显然“数字”是未定义的,即使包含标题,这里是与之相关的标题和.cpp文件。
LotteryData.h
#pragma once
#include <iostream>
#include <fstream>
#include "Lottery.h"
#include "Player.h"
using namespace std;
class LotteryData
{
private:
public:
LotteryData();
~LotteryData();
void PassInfo(int (&Numbers)[6][6]);
};
LotteryData.cpp
#include <iostream>
#include <fstream>
#include "LotteryData.h"
using namespace std;
LotteryData::LotteryData()
{
}
LotteryData::~LotteryData()
{
}
void LotteryData::PassInfo(int (&Numbers)[6][6])
{
int* ptr;
FILE *Numfile;
Numfile = fopen("C:/Num.txt", "wb");
ptr = &Numbers[6][6];
for (int i=0; i<36; i++)
{
fwrite(ptr, sizeof(int), 36*36, Numfile);
}
fclose(Numfile);
//ofstream out("Numbers.txt");
}
一切似乎都很好,我很困惑为什么主要的参考说数组是未定义的,任何想法?
编辑:道歉,错过了一些位
Player.h
#pragma once
#include <iostream>
#include <fstream>
using namespace std;
class Player
{
private:
public:
Player();
~Player();
void Input();
int Numbers[6][6];
};
Player.cpp
#include <iostream>
#include <fstream>
#include "Player.h"
using namespace std;
Player::Player()
{
}
Player::~Player()
{
}
void Player::Input()
{
int num(0);
int duplicate = 0;
int game = 0;
int NumberofGames = 0;
cout<<"How many games do you want to play for this weeks draw?"<<endl;
cin>>NumberofGames;
if (NumberofGames>6)
{
cout<<"Please enter an amount between 1 and 6"<<endl;
cin>>NumberofGames;
}
do
{
for (int i=0;i<6;i++)
{
cout<<"Enter Number "<< (i+1) <<endl;
cin>>num;
if (num > 0 && num <67)
{
Numbers[game][i]= num;
}
else
{
cout <<"Please enter number between 1 and 66"<<endl;
i = i-1;
}
}
game = game + 1;
NumberofGames = NumberofGames - 1;
}
while (NumberofGames=0);
}
答案 0 :(得分:2)
void PassInfo(int (&Numbers)[6][6]);
该行未声明数组 - 它声明了一个函数。你的类中没有数组声明(实际上,你的类中没有声明数据成员)。
如果要声明成员数组,则需要修改类定义:
class LotteryData
{
private:
int Numbers[6][6]; // this declares an array
public:
LotteryData();
~LotteryData();
void PassInfo(int (&arr)[6][6]); // this is still a function declaration
};
答案 1 :(得分:2)
仅仅因为你将函数的参数命名为Numbers
并不奇怪地意味着你的程序中有一个名为Numbers
的数组。
答案 2 :(得分:1)
在main()函数中,
data.PassInfo(int (&Numbers)[6][6]);
这是错误的。您应该简单地将引用传递给2D数组。
int (&Numbers)[6][6];
data.PassInfo(Numbers);
答案 3 :(得分:1)
所以Player
有一个名为“Numbers”的数组。
然后你会像这样使用它:
data.PassInfo(player.Numbers);