Yam*_*iko 1 c++ io methods iostream class
尽管使用了cin cout,我endl仍然会收到未经申报的错误#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;
}
Run Code Online (Sandbox Code Playgroud)
Kat*_*ory 13
它们位于std命名空间中.添加以下行:
using std::cout;
using std::endl;
using std::cin;
Run Code Online (Sandbox Code Playgroud)
或者,每次使用它们时,请以全名调用它们,例如:
std::cout << "Select a direction: " << std::endl;
Run Code Online (Sandbox Code Playgroud)
这很快就会很烦人,也会让你的代码更难阅读.
有人用
using namespace std;
Run Code Online (Sandbox Code Playgroud)
相反,但你可能会得到不必要的副作用.您编写的类可能与std命名空间中的其他类具有相同的名称,并且您的over-broad using语句现在将导致冲突.这就是为什么你永远不应该using namespace std;在头文件中说.在一个.cpp文件中没关系,但我更喜欢自己的个人陈述.无论是谁从你所包含的标题中读取你正在使用的代码,它都能清楚地表明.
| 归档时间: |
|
| 查看次数: |
9615 次 |
| 最近记录: |