I’m getting cin cout and endl as undeclared errors despite using #include <iostream>
#include "navigation.h"
#include <iostream>
Navigation::Navigation()
{
xPos=0;
yPos=0;
}
void Navigation::Move()
{
//get direction
int dir;
cout << "Select a direction: " << endl;
cout << "1) North 3) South" << endl;
cout << "2) East 4) West " << endl;
cin >> dir;
//move
switch(dir)
{
case 0://north
yPos++;
break;
case 1://east
xPos++;
break;
case 2://south
yPos--;
break;
case 3://west
xPos--;
break;
default:
cout << "Invalid entry" << endl;
}
}
void Navigation::Position(int &x, int &y)
{
x = xPos;
y = yPos;
}
They are in the std namespace. Add these lines:
Alternatively, each time you use them, call them by their full names, for example:
That gets tiresome very quickly and can make your code harder to read, too.
Some people use
instead, but you may get unwanted side-effects from that. A class you have written may have the same name as something else in the std namespace and your over-broad using statement will now cause a collision. This is why you should NEVER say
using namespace std;in a header file. In a .cpp file it’s ok, but I prefer individual statements myself. It makes it clear to whoever reads your code what you are using from the headers you have included.