我正在为我的 C++ 类编写一些代码,但我有点卡住了。
这是我正在处理的任务的一部分:
在 Rover 类中,指定以下成员实例变量:
name (String)
x position on a grid (integer)
y position on a grid (integer)
direction by compass – N, S, E, or W (String)
speed (0 – 5 meters per second, integer)
在 Rover 类中,指定以下方法:
No-arg constructor – set the rover’s position to (0,0), its speed to 0,
its direction to North,and its name to Default
Constructor that receives parameters to initialize all five
instance variables described above
Setter methods for each instance variable
Getter methods for each instance variable
getRoverData – returns a string that displays values for each instance
variable of the current rover object, placing each value on a separate line,
as follows:
所以现在我只想让基本案例发挥作用。这是我的全部代码:
主.cpp:
#include <iostream>
#include "Rover.h"
using namespace std;
int main() {
Rover rover1;
rover1.setDefaultValues();
cout<<rover1.getRoverData(rover1)<<endl;
return 0;
}
漫游者.cpp:
#include <iostream>
#include "Rover.h"
using namespace std;
//put functions in here
void Rover::setDefaultValues() {
name = "Default";
xcoord = 0;
ycoord = 0;
direction = "N";
speed = 0;
}
void Rover::constructRover() {
}
void Rover::setXcoord(int xcoord) {
this->xcoord = xcoord;
}
void Rover::setYcoord(int ycoord) {
this->ycoord = ycoord;
}
void Rover::setName(string name) {
this->name = name;
}
void Rover::setDirection(string direction) {
this->direction = direction;
}
void Rover::setSpeed(int speed) {
this->speed = speed;
}
int Rover::getXcoord() {
return xcoord;
}
int Rover::getYcoord() {
return ycoord;
}
string Rover::getName() {
return name;
}
string Rover::getDirection() {
return direction;
}
int Rover::getSpeed() {
return speed;
}
string Rover::getRoverData(Rover rover) {
string printStatement = "";
printStatement.append("Data for " << rover.getName());
printStatement.append("\nX: " << Rover::xcoord);
printStatement.append("\nY: " << Rover::ycoord);
printStatement.append("\nDirection: " << Rover::direction);
printStatement.append("\nSpeed: " << Rover::speed);
return printStatement;
}
漫游者.h:
/*
* File: Rover.h
* Author: Matthew
*
* Created on May 14, 2012, 8:10 PM
*/
#ifndef ROVER_H
#define ROVER_H
using namespace std;
class Rover
{
private:
string name;
int xcoord;
int ycoord;
string direction;
int speed;
public:
void setDefaultValues();
void constructRover();
//XCoordinates
void setXcoord(int xcoord);
int getXcoord();
//YCoordinates
void setYcoord(int ycoord);
int getYcoord();
//Name
void setName(string name);
string getName();
//Direction
void setDirection(string direction);
string getDirection();
//Speed
void setSpeed(int speed);
int getSpeed();
//Printout
string getRoverData(Rover rover);
};
#endif /* ROVER_H */
我目前得到的错误是:
rover.cpp:64: error: no match for 'operator<<' in '"Data for " << Rover::getName()()'
见鬼是什么意思?
在这一点上,我希望打印输出说的是:
Data for Default
X: 0
Y: 0
Direction: N
Speed: 0
有什么想法吗?
抱歉,如果我发布的代码看起来很糟糕......以前没有这样做过。
更新:
#include <iostream>
#include "Rover.h"
using namespace std;
//put functions in here
void Rover::setDefaultValues() {
name = "Default";
xcoord = 0;
ycoord = 0;
direction = "N";
speed = 0;
}
void Rover::constructRover() {
}
void Rover::setXcoord(int xcoord) {
this->xcoord = xcoord;
}
void Rover::setYcoord(int ycoord) {
this->ycoord = ycoord;
}
void Rover::setName(string name) {
this->name = name;
}
void Rover::setDirection(string direction) {
this->direction = direction;
}
void Rover::setSpeed(int speed) {
this->speed = speed;
}
int Rover::getXcoord() {
return xcoord;
}
int Rover::getYcoord() {
return ycoord;
}
string Rover::getName() {
return name;
}
string Rover::getDirection() {
return direction;
}
int Rover::getSpeed() {
return speed;
}
string Rover::getRoverData(Rover rover) {
string printStatement = "";
printStatement.append("Data for " + rover.getName());
printStatement.append("\nX: " + rover.getXcoord());
printStatement.append("\nY: " + rover.getYcoord());
printStatement.append("\nDirection: " + rover.getDirection());
printStatement.append("\nSpeed: " + rover.getSpeed());
return printStatement;
}
所以,现在打印输出是:
Data for Default
X:
Y:
Direction: N
Speed:
所以,它基本上不会打印出零。想法?