c++ use cout in class method

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; }

-------------Problems Reply------------

They are in the std namespace. Add these lines:

using std::cout;
using std::endl;
using std::cin;

Alternatively, each time you use them, call them by their full names, for example:

std::cout << "Select a direction: " << std::endl;

That gets tiresome very quickly and can make your code harder to read, too.

Some people use

using namespace std;

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.

They are members of the std namespace so you need to qualify them with std: std::endl, std::cout and std::cin.

Include using namespace std; after your #include statements.

endl, cin and cout are in the namespace std. You either need a using namespace std; near the top of your file, or to use std::endl, std::cin and std::cout.

you need to declare using namespace std; [after your #includes section] or use std::cout, std::cin, std::endl

Category:c# Views:0 Time:2012-02-22

Related post

  • Redirect cout from C++ dll to a textbox in C# 2012-01-30

    I'm trying to display the console output (cout) of a method in a dll to a textbox in C# program. Every time I call the method, the console output will be displayed in the Output Pane of Visual Studio. Is there a way to redirect the content of the Out

  • Sendto() socket method sends junk on short strings 2012-02-20

    I have server and client to send/receive a string through windows sockets. Client code (sender): string c = "Haha"; sprintf(temp.Buffer, c.c_str()); temp.len = c.size(); int sent = 0; if ((sent = sendto(s, temp.Buffer, temp.len + 1, 0, (SOCKADDR*)

  • Calling Java methods asynchronously using JNI 2012-04-03

    I've been trying to get familiar with JNI and its functionality. I am trying to figure out how could I possibly keep reference to the Java environment. I am first calling native method that stores creates "NewGlobalRef" from the jobject and it works

  • How to overload a method from a foreign package 2013-09-25

    Given the szenario that you got a base package which can represent certain stuff via a class and another package which wants to extend this ability. (defpackage :test (:use :cl) (:nicknames :test) (:export a-test-class method-a slot-a)) (in-package :

  • Using unrar library - extracting files into a filestream buffer 2010-02-24

    What I need is to be able to extract the files in a .rar file into streams. I'm creating a test case to get a sense of how to use unrar source. I've been searching and tinkering for a while, but I can't figure out how to use the library. I'm surprise

  • BOOST_FOREACH implicit use of reference? 2010-03-02

    I am wondering if there is any benefit to getting a reference to a vector prior to calling BOOST_FOREACH or whether a method call which returns a reference will be automatically used? For example which of the two following loops will be equivalent to

  • Problem separating C++ code in header, inline functions and code 2010-03-29

    I have the simplest code that I want to separate in three files: Header file: class and struct declarations. No implementations at all. Inline functions file: implementation of inline methods in header. Code file: normal C++ code for more complicated

  • VC6 Profiler Problem: Spurious Function Calls 2010-07-02

    I am running into the following issue while profiling an application under VC6. When I profile the application, the profiler is indicating that a simple getter method similar to the following is being called many hundreds of thousands of times: int S

  • C++: Copy containers efficiently 2010-07-31

    How do you copy your STL containers? // big containers of POD container_type<pod_type> source; container_type<pod_type> destination // case 1 destination = source; // case 2 destination.assign(source.begin(), source.end()); // case 3 assu

  • GNU GCC (g++): Why does it generate multiple dtors? 2011-07-07

    Developing environment: GNU GCC (g++) 4.1.2 While I'm trying to investigate how to increase 'code coverage - particularly function coverage' in unit testing, I've found that some of class dtor seems to be generated multiple times. Does some of you ha

  • QWindow Program on QT Creator won't run 2011-08-03

    I have made a program on Qt Creator which builds without errors. But when I try to run it, I get the following in my output pane. Can anyone please tell me what this means/how to fix this? I would appreciate any help. &"warning: GDB: Failed to se

  • C++ Linked List assignment: trouble with insertion and deletion 2011-10-10

    I am working on a linked list implementation in C++. I am making progress but am having trouble getting the insertion functionality and deletion functionality to work correctly. Below is list object in the C++ header file: #ifndef linkList_H #define

  • how to resolve AccessViolationException 2011-10-21

    Could you please help in resolving the below mentioned issue: Following are the code snippet: Managed code (VC++) - wrapper code: Method-1: void displayString(std::string abc) { std::string xyz=abc; std::cout<<xyz; } Method-2: void sendData(Sys

  • Can't get SFINAE to work in function detector 2012-01-18

    I've been working on some solutions for some questions that have been recently posted where the original questioners have been trying to query if a method exists in a given class. I've been trying to develop a solution using a SFINAE approach, but un

  • Could we have chain style setters in C++/Java just like jquery? 2012-03-19

    I'm using C++ and Java for a while and recently I'm on jquery. I noticed jquery's chain style functions. Then I think, if we could change setters in C++/Java just like jquery? For example, if our code is class Person { private String name; private in

  • Homework: Cout incorrectly handling a return value of 0.0 from a class method 2011-10-06

    First, this is homework, so I can not dynamically allocate memory for an array of any size, and I can not use a vector. I have a class which includes a double array holding 30 elements, and two other variables to keep track of how many elements have

  • Why does adding a cout line to this method stop my program from tripping a buffer overflow exception in Windows? 2011-12-23

    I have an object with the following method: int PathSubstitution::updateField(Field4memo &field, int record_id, int field_id) const { int type = field.type(); if((type == r4str) || (type == r4memo) || (type == r4unicode)) { string value = field.s

  • C++ cout does not work with the method 2013-10-14

    I'm trying to implement a hidden word finder game, it reads the puzzle from the text file and then tries to find out where the hidden word is. However, when I try to make a top top bottom search, nothing appears on the screen, even when I write a sim

  • Design: Java and returning self-reference in setter methods 2008-08-28

    I have already proposed this in my blog, but I find this place the most appropriate. For classes that have a long list of setters that are used frequently, I found this way very useful (although I have recently read about the Builder pattern in Effec

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.186 (s). 11 q(s)