【发布时间】:2019-03-25 23:27:37
【问题描述】:
我收到错误,例如 cout is undeclared identifier
我已经经历了很多次,我看不出我做错了什么!我知道所以请耐心等待。
我已经多次检查了所有内容,但这对我来说并没有。
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;
}
添加.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;
}
添加.h
#ifndef _IOSTREAM_
#define _IOSTREAM_
int function();
int functionTwo(int a, int b);
#endif
我正在使用 Microsoft Visual Studio,我正在尝试编译它。我确保制作新项目/ .cpp 文件和 .h 文件。我试过删除 pch.h 和 pch.cpp 文件,但我只是不明白我做错了什么。请帮我。我知道所以我很抱歉。
【问题讨论】:
-
我们不想知道错误是什么“像”。我们想知道错误“是什么”。
-
你说你在使用PCH?哪些头文件是预编译的,哪些文件用于创建 PCH?在就您收到的错误寻求帮助时,最好也发布您收到的实际错误……;-)
-
另外,请注意:这几乎肯定不会是您的问题的原因,但
_IOSTREAM_不是在 C++ 程序中使用的有效标识符。任何以_开头后跟一个大写字母的内容(也:任何包含双下划线的内容)都是不允许程序使用的保留名称。 -
除了其他 cmets,您不应该使用
_IOSTREAM_作为“add.h”的包含保护,这与iostream标头中使用的确切保护相同。使用__ADD_H__之类的东西,或者由于您使用的是 VS,您可以使用#pragma once -
@ChrisTaylor 哦,哇,你是对的。这可能是我第一次看到在包含保护中使用保留名称实际上使事情变得糟糕......
标签: c++