我对一个正在编写的程序提出了一个非常具体的问题,这个程序是一个战舰游戏。我有提供给我的头文件,我必须在定义的头文件中实现函数定义。我将发布我的整个代码,但是,我只是在实现函数void ship::printShip() const
时遇到了问题。我需要它来访问类中的类的信息,并且我有一个函数提供我在位置void print() const
下使用,但是,它要我创建一个指针以访问打印下的信息。我将发布函数定义和头文件以及错误消息。
标题文件:
#ifndef BATTLESHIP_H_
#define BATTLESHIP_H_
// coordinates (location) of the ship and shots
class location{
public:
location(); // void constructor, assigns -1 to X
void pick(); // picks a random location
void fire(); // asks the user to input coordinates of the next shot
void print() const; // prints location in format "a1"
// returns true if the two locations match
friend bool compare(location, location);
private:
static const int fieldSize = 5; // the field (ocean) is fieldSize X fieldSize
int x; // 1 through fieldSize
char y; // 'a' through fieldSize
};
// contains ship's coordinates (location) and whether is was sunk
class ship{
public:
ship(); // void constructor, sets sunk=false
bool match(location&) const; // returns true if this location matches
// the ship's location
bool isSunk() const { return sunk; } // checks to see if the ship is sunk
void sink(); // sets "sunk" member variable of the ship to true
void setLocation(const location&); // deploys the ship at the specified location
void printShip() const; // prints location and status of the ship
private:
location loc;
bool sunk;
};
// contains the fleet of the deployed ships
class fleet{
public:
void deployFleet(); // deploys the ships in random locations
// of the ocean
bool operational() const; // returns true if at least
// one ship in the fleet is not sunk
bool isHitNSink(const location &); // returns true if there was a deployed
// ship at this location (hit) and sinks it
// otherwise returns false (miss)
void printFleet() const; // prints out locations of ships in fleet
private:
static const int fleetSize = 5; // number of battleships
int check(const location &); // returns index of the ship
// that matches location
// -1 if none match
ship ships[fleetSize]; // battleships of the fleet
};
#endif /* BATTLESHIP_H_ */
以下是我的功能定义:
#include "battleship.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int FLEET_SIZE = 5;
const int FIELD_SIZE = 5;
void location::pick(){
x = rand() % FIELD_SIZE;
if (x == 0){
x = 1;
}
y = rand() % FIELD_SIZE;
switch (y)
{
case 0:
y = 'A';
break;
case 1:
y = 'B';
break;
case 2:
y = 'C';
break;
case 3:
y = 'D';
break;
case 4:
y = 'E';
break;
}
}
void location::fire(void){
cout << "Enter the number of the grid to fire on ";
cin >>y;
cout << "Enter the letter of the grid to fire on ";
cin >> x;
}
void location::print() const{
cout << y << x;
}
bool compare(location one,location two){
if ((one.x == two.x)&&(one.y==two.y)){
return true;
}
return false;
}
location::location(){
x = -1;
}
void ship::setLocation(const location& location){
loc= location;
}
bool ship::match(location& location)const{
if (compare(loc, location)){
return true;
}
return false;
}
void ship::printShip()const{
cout << loc.print << endl;
cout << sunk << endl;
}
void ship::sink(){
sunk = true;
}
这是.cpp
#include "battleship.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
using std::cout; using std::cin; using std::endl;
/// this is main function
const int FLEET_SIZE = 5;
const int FIELD_SIZE = 5;
int main(){
srand(time(NULL));
//
// checking location object
//
location mySpot, userShot;
mySpot.pick(); // selecting a new random location
cout << "Randomly selected location is: "; mySpot.print();
cout << "Input location: ";
userShot.fire(); // having user input a location
if (compare(mySpot, userShot))
cout << "Random location matches user input.\n";
else
cout << "Random location does not match user input.\n";
//
// checking ship object
//
ship myShip;
myShip.setLocation(mySpot); // placing ship at mySpot location
if(myShip.match(userShot))
cout << "myShip\'s location matches user input.\n";
else
cout << "myShip's location does not match user input.\n";
if(!myShip.isSunk()){
cout << "myship is not sunk yet, sinking it.\n";
myShip.sink();
}
cout << "myShip\'s status is: "; myShip.printShip();
//
// checking fleet object
//
/* // uncomment this part once you are done debugging above code
fleet myFleet;
myFleet.deployFleet(); // fleet is deployed at random locations
if(myFleet.operational())
cout << "Some ships of myFleet are still up.\n";
if(myFleet.isHitNSink(userShot))
cout << "there was a ship at userShot location, no it is sunk.\n";
else
cout << "there was no ship at userShot location.\n";
cout << "myFleet\'s status is: "; myFleet.printFleet();
*/
}
以下是printShip() const
Error 1 error C3867: 'location::print': function call missing argument list; use '&location::print' to create a pointer to member
如果有人可以解释我需要编辑的内容,以便printShip() const
打印出所需的船舶位置。
答案 0 :(得分:0)
您只是错过了对location::print
的调用中的括号:
void ship::printShip() const {
cout << loc.print() << endl;
cout << sunk << endl;
}
它发生了。但是要学会识别来自编译器的消息:function call missing argument list
实际上意味着:“如果你想写一个函数调用,添加一个参数列表。”并且“使用'&amp; location :: print'创建指向成员的指针”试图告诉您替代方案,即,当您真正想要指向该方法的指针时,请使用&location::print
。
<强>后来强>
location::print()
不返回任何内容,因此您可能希望更改它。或者只是使用该调用,因为它现在打印到cout
。
void ship::printShip() const {
loc.print();
cout << endl;
cout << sunk << endl;
}