I’m working on a bit of code for my C++ class, and I’m a bit stuck.
Here’s the bit of the assignment I’m working on:
Within the Rover class, specify the following member instance variables:
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)
Within the Rover class, specify the following methods:
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:
So right now I just want to get the base case to work. Here’s the entirety of my code:
main.cpp:
#include <iostream>
#include "Rover.h"
using namespace std;
int main() {
Rover rover1;
rover1.setDefaultValues();
cout<<rover1.getRoverData(rover1)<<endl;
return 0;
}
rover.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;
}
Rover.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 */
The error I’m currently getting is:
rover.cpp:64: error: no match for 'operator<<' in '"Data for " << Rover::getName()()'
The heck does that mean?
At this point, what I want the printout to say is:
Data for Default
X: 0
Y: 0
Direction: N
Speed: 0
Any thoughts?
Sorry if the code looks awful as I posted it…haven’t done this before.
UPDATE:
#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;
}
So, now the printout is:
Data for Default
X:
Y:
Direction: N
Speed:
So, it basically just won’t print out the zeroes. Thoughts?
The
<<is the stream insertion operator. There is no function anywhere that takes aconst char[]left hand side and astd::stringright hand side. Therefore, you’re getting errors.You may use this combination when using
cout, but it’s a bit deceptive. A call of:Is defined as:
The actual call is equivalent to:
The thing that makes it work is that the inside call returns the same
coutis was passed in so that it can be used with the other call. If you don’t use something likecoutthat exhibits this behaviour, it won’t work the same way. This is also why you can say:The 5 + 2 returns an
int, which is added with 3, and that returns theintassigned toi.To concatenate the string inside, you can just replace that with a plus, since addition for strings and char arrays is defined as part of std::string, or add another
appendcall:Update:
To answer your other inquiry, this is another feature of C++ that isn’t part of Java. I didn’t realize you were adding ints onto some of them. When you do that, the character array in the quotes actually decays into a pointer. Then, pointer arithmetic is used to add the integer. Not what you wanted at all. So basically you’re passing in the pointer to the beginning of the string literal, moved forward x characters, where x is the number you’re adding to it. Adding 0 means it just stays there, so the whole string is passed and the
+ 0is pretty much dead code.If you’re going to be adding ints on, you need a way to convert the integers to strings, and then add them on. C++11 has
stoi, but that’s the opposite of what you want. Instead, you can use a stringstream, among other methods: