-4

I am receiving errors like cout is undeclared identifier

I have gone through MULTIPLE times and there is nothing I can see that I have done wrong! I am knew so please be patient.

I have checked everything multiple times and this just does not make since to me.

Main.cpp

#include <iostream>
#include <limits>
#include "add.h"


int main()
{
    std::cout << "this will call function which will have you type in numbers which when done will call the second function as well as giving the second function answers to what it needs. " << '\n';

    int numOne{ function() };
    int numTwo{ function() };
    std::cout << "With those two numbers we can add them! and our answer is!" << functionTwo(numOne, numTwo) << '\n';
    std::cout << "we now sent functionTwo the numbers we made using function for simplness. Now functionTwo can have the numbers it needs to do what it needs to do." << '\n';


    // This code will make it not close automatically.
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin.get();

    return 0;
}


add.cpp

#include <iostream>
#include "add.h"

int functionTwo(int a, int b)

{
    std::cout << " fdsaf dasf dsa" << '\n';


    std::cout << 2 * a << '\n';
    std::cout << 2 + b << '\n';
    int c{ 2 + b };
    int d{ 2 * a };



    return c + d;

}

add2.cpp


#include <iostream>
#include "add.h"
int function()

{
    int a{ 0 };
    std::cin >> a;
    return a;

}

add.h

#ifndef _IOSTREAM_
#define _IOSTREAM_

int function();



int functionTwo(int a, int b);



#endif


I am using Microsoft Visual studio and I am trying to compile this. I made sure to make new items / the .cpp files and the .h file. I have tried deleteing the pch.h and the pch.cpp files but I just don't understand what I am doing wrong. Please help me. I am knew so I am sorry for that.

Annonymous
  • 23
  • 1
  • 6
  • 3
    We don't want to know what the errors are "like". We want to know what the errors "are". – jarmod Mar 25 '19 at 23:35
  • 1
    You say you're using PCH? Which headers are precompiled and which files are used to create the PCH? When asking for help with errors you're receiving, it would seem like a good idea to also post the actual errors you're receiving… ;-) – Michael Kenzel Mar 25 '19 at 23:36
  • 2
    Also, just a minor note: This will almost certainly not be the cause of your issues, but `_IOSTREAM_` is not a valid identifier to use in a C++ program. Anything that starts with an `_` followed by an uppercase letter (also: anything that contains a double underscore) is a reserved name that programs are not allowed to use. – Michael Kenzel Mar 25 '19 at 23:39
  • 3
    In addition to the other comments, you should not be using `_IOSTREAM_` as your include guard for "add.h", that is the exact guard used in the `iostream` header. Uses something like `__ADD_H__` or since you are using VS you can use `#pragma once` – Chris Taylor Mar 25 '19 at 23:40
  • 1
    If Visual Studio PCH, where are your stdafx.h includes? – Dave S Mar 25 '19 at 23:40
  • 2
    @ChrisTaylor oh wow, you're right. This may be the first time I've ever see the use of a reserved name in an include guard to actually have made things blow up… – Michael Kenzel Mar 25 '19 at 23:44
  • im recieving errors like – Annonymous Mar 25 '19 at 23:44
  • 1
    Further explanation of what Chris Taylor and Michael Kenzel are talking about: [What are the rules about using an underscore in a C++ identifier?](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – user4581301 Mar 25 '19 at 23:47
  • Very poor question as you don't tell us what errors you get. – Phil1970 Mar 25 '19 at 23:48
  • @Annonymous don't even waste your time trying to get the error messages to not looks like jumbled garbage in a comment. Paste the error messages into the question and surround it with `
    `. You will also have an easier time getting the error messages from the Output tab. The Error List leaves out too much information and doesn't format to text well at all.
    – user4581301 Mar 25 '19 at 23:49
  • 1
    Just as another note on @ChrisTaylor's comment: You also shouldn't use `__ADD_H__` as that is a reserved name as well. Personally, I use something like, e.g., `INCLUDED_ADD_H`. Having all include guards follow the same naming scheme has the added advantage of not cluttering your autocomplete suggestions all over the place… – Michael Kenzel Mar 25 '19 at 23:56
  • Why people have this mystical belief in the powers of the underscore, I will never know - my further thoughts: https://punchlet.wordpress.com/2009/12/01/letter-the-second/ –  Mar 26 '19 at 00:03
  • Underscores taste *amazing* with an artichoke dip. – user4581301 Mar 26 '19 at 00:17
  • 1>c:\users\cde\desktop\consoleapplication1\consoleapplication1\add.cpp(8): error C2039: 'cout': is not a member of 'std' 1>c:\users\cde\desktop\consoleapplication1\consoleapplication1\predefined c++ types (compiler internal)(256): note: see declaration of 'std' – Annonymous Mar 26 '19 at 00:33
  • there are a ton more like this. – Annonymous Mar 26 '19 at 00:33
  • can someone help? – Annonymous Mar 26 '19 at 00:37
  • And you get these errors with the order of `#include` directives exactly like you showed in your code above? – Michael Kenzel Mar 26 '19 at 02:03

1 Answers1

-1

What you do is add:

#include "pch.h"
#include "add.h"

so in each one that uses the header add.h make sure that above it you also use the pch.h otherwise it will tell you did you forget to use it and it will not work. Simply adding #include "pch.h" fixed it all.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Annonymous
  • 23
  • 1
  • 6