What did I do wrong?

Really new at this. I tried to write a program just to pass a variable into a class and cout it from main. When I finished the cout command wasn't rebonised. I had included iostream....I captioned out all the rest of the program....I copied and pasted the cout command from my "hello world" program. It still wouldn't work. At first it said cout not identifier and then said it wasn't part of Std (I'm paraphrasing)....I started a new project....wrote the same code and it worked perfectly. Any ideas what happened? I'm using virtual studio 2017
You either need to call it std::cout or you need to use
 
using namespace std;

after the includes.
I did that. I tried both methods.
can you post your code so that we can see better?
did you accidentally choose C# project or some other type of project ? It can be easy to mis-click in the new project stuff when learning the tool.

alternately, no offense, but there are at least 2 typos in your short post, did you happen to mis-spell something critical?
Last edited on
Are you doing everything in one file or do you have multiple files? If you have multiple files, make sure they have #include <iostream> in them, as well. If you are including "stdafx.h", include iostream after it.

Other than that, we can only guess: post the code.
Last edited on
Heres the code guys....you can see where I've captioned out the rest of the program...and it is a .cpp file and yes this is just one file.


#include "stdafx.h"
#include <string>

/*
using namespace std;

class myclass {
public:
void setval(int a, int b) {
valuetoset = a + b;
}
int getvalue() {
return valuetoset;
}
private:
int valuetoset;
};
*/
int main()
{
std::cout << "";

/* myclass obj;
obj.setval(13, 19);
*/

return 0;
};
you should have to include<iostream> to get cout ... visual sometimes has include chains that would pull it in externally and it would work without but you should include it.



Try re-reading (did you even read it?) this!

Ganado wrote:
make sure they have #include <iostream> in them, as well. If you are including "stdafx.h", include iostream after it.


std::cout is declared in the header file <iostream>.
Sorry guys....I didn't paste and copy the #include <iostream> ...it is there....ah but it is above the stdafx.h ...that matters does it? Right...thanks for the help folks...May my learning continue.
Last edited on
stdafx is a visual studio / microsoft thing and it behaves oddly. It is best practice to make it the first thing, but that isnt tied to the c++ language directly, its just a nuance of that IDE and how it does things. Back when I used VS I actually immediately removed this line from all programs and turned off the settings that used it (precompiled headers and such) because they caused nonstop problems (this was a LONG time ago, prob VS 2008 or so).

Topic archived. No new replies allowed.