关于C++ 基本常见问题 无法打开包括文件:“iostream.h”: No such file or directory” [Error] '::main' must return 'int

本文介绍了C++初学者在使用iostream时常见的两个错误:一是包含文件错误,解决方法是更改包含指令并声明命名空间;二是main函数返回类型错误,需要确保main函数返回int类型。


本人因为做UE4  虚幻引擎  要用到C++,所以才开始学习C++,肯定有很大不行的地方,还是希望各位大大  多多指点

   cout语句的一般格式:cont <<表达式1 << 表达式2 << ... <<表达式n;    cont代表显示器,执行cout<<x操作就相当于把X的值输出到显示器上

   cin语句的一般格式:cin>> 变量1 >>变量2 >> ... >>变量n;                  cin代表键盘,执行cin>>x就相当于把键盘输入的数据赋值给变量

1)通常编写一个普通语句的时候;例如

#include <iostream.h>

void main(){
	int ilnput;
	cout << "Please input a number:"<< endl;
	cin>> ilnput ;
	cout << "the number is :" << ilnput << endl;
	
} 
但是你会遇到俩个错误
A.  显示“无法打开包括文件:“iostream.h”: No such file or directory”

原因是:没有使用命名空间  using namespace std;

  应该改为:#include "iostream"
         using namespace std;

B,改完之后又发现报错    [Error] '::main' must return 'int'

   这个不是你代码的问题,而是编译器的问题,C语言的标准允许main函数为void类型,而按照C++的标准 main必须是int类型,但很多IDE或者编译器

不一定准守C++标准,比如VS,可能你用的是DevC++,这个严格的遵守C++的标准。

   因为只要把main函数改一下就行啦,C里面能这么写

  void main(){

   }

 但是C++得这样写

int main(){

     return 0;

}

您是第一种写法就错了



以下是错误log Checking whether 'gcc -O2 -I/mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/include -L/mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/lib ' works. Try: gcc Line: gcc -O2 -I/mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/include -L/mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/lib cmake_bootstrap_319609_test.c -o cmake_bootstrap_319609_test ---------- file ----------------------- #ifdef __cplusplus # error "The CMAKE_C_COMPILER is set to a C++ compiler" #endif #if defined(_AIX) && defined(__GNUC__) && !defined(_THREAD_SAFE) #error "On AIX with GNU we need the -pthread flag." #endif #if defined(__sun) && __STDC_VERSION__ < 199901L #error "On Solaris we need C99." #endif #if defined(__hpux) && !(defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 409) #error "On HP-UX we need GCC 4.9 or higher." #endif #include <stdio.h> int main(int argc, char* argv[]) { printf("%d%c", (argv != 0), (char)0x0a); return argc - 1; } ------------------------------------------ 1 Test succeeded Checking whether 'g++ -O2 -I/mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/include -L/mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/lib ' works. Try: g++ Line: g++ -O2 -I/mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/include -L/mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/lib cmake_bootstrap_319609_test.cxx -o cmake_bootstrap_319609_test ---------- file ----------------------- #include <iostream> #include <memory> #include <unordered_map> #if __cplusplus < 201103L #error "Compiler is not in a mode aware of C++11." #endif #if defined(_AIX) && defined(__GNUC__) && !defined(_THREAD_SAFE) #error "On AIX with GNU we need the -pthread flag." #endif #if defined(__SUNPRO_CC) && __SUNPRO_CC < 0x5140 #error "SunPro <= 5.13 mode not supported due to bug in move semantics." #endif #if defined(__hpux) && !(defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 409) #error "On HP-UX we need GCC 4.9 or higher." #endif #if __cplusplus > 201103L #include <iterator> int check_cxx14() { int a[] = { 0, 1, 2 }; auto ai = std::cbegin(a); int b[] = { 2, 1, 0 }; auto bi = std::cend(b); return *ai + *(bi - 1); } #else int check_cxx14() { return 0; } #endif #if (__cplusplus >= 201703L || defined(__INTEL_COMPILER) && defined(__cpp_deduction_guides)) #include <optional> template <typename T, typename std::invoke_result<decltype(&T::get), T>::type = nullptr> typename T::pointer get_ptr(T& item) { return item.get(); } int check_cxx17() { // Intel compiler do not handle correctly decltype inside invoke_result std::unique_ptr<int> u(new int(0)); get_ptr(u); std::optional<int> oi = 0; return oi.value(); } #else int check_cxx17() { return 0; } #endif class Class { public: int Get() const { return this->Member; } private: int Member = 1; }; int main() { auto const c = std::unique_ptr<Class>(new Class); std::cout << c->Get() << check_cxx14() << check_cxx17() << std::endl; return 0; } ------------------------------------------ In file included from /usr/include/c++/5/unordered_map:35:0, from cmake_bootstrap_319609_test.cxx:4: /usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options. #error This file requires compiler and library support \ ^ cmake_bootstrap_319609_test.cxx:7:2: error: #error "Compiler is not in a mode aware of C++11." #error "Compiler is not in a mode aware of C++11." ^ cmake_bootstrap_319609_test.cxx:70:16: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 int Member = 1; ^ cmake_bootstrap_319609_test.cxx: In function 'int main()': cmake_bootstrap_319609_test.cxx:74:14: error: 'c' does not name a type auto const c = std::unique_ptr<Class>(new Class); ^ cmake_bootstrap_319609_test.cxx:75:16: error: 'c' was not declared in this scope std::cout << c->Get() << check_cxx14() << check_cxx17() << std::endl; ^ Test failed to compile Checking whether 'g++ -O2 -I/mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/include -L/mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/lib -std=gnu++17 ' works. Try: g++ Line: g++ -O2 -I/mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/include -L/mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/lib -std=gnu++17 cmake_bootstrap_319609_test.cxx -o cmake_bootstrap_319609_test ---------- file ----------------------- #include <iostream> #include <memory> #include <unordered_map> #if __cplusplus < 201103L #error "Compiler is not in a mode aware of C++11." #endif #if defined(_AIX) && defined(__GNUC__) && !defined(_THREAD_SAFE) #error "On AIX with GNU we need the -pthread flag." #endif #if defined(__SUNPRO_CC) && __SUNPRO_CC < 0x5140 #error "SunPro <= 5.13 mode not supported due to bug in move semantics." #endif #if defined(__hpux) && !(defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 409) #error "On HP-UX we need GCC 4.9 or higher." #endif #if __cplusplus > 201103L #include <iterator> int check_cxx14() { int a[] = { 0, 1, 2 }; auto ai = std::cbegin(a); int b[] = { 2, 1, 0 }; auto bi = std::cend(b); return *ai + *(bi - 1); } #else int check_cxx14() { return 0; } #endif #if (__cplusplus >= 201703L || defined(__INTEL_COMPILER) && defined(__cpp_deduction_guides)) #include <optional> template <typename T, typename std::invoke_result<decltype(&T::get), T>::type = nullptr> typename T::pointer get_ptr(T& item) { return item.get(); } int check_cxx17() { // Intel compiler do not handle correctly decltype inside invoke_result std::unique_ptr<int> u(new int(0)); get_ptr(u); std::optional<int> oi = 0; return oi.value(); } #else int check_cxx17() { return 0; } #endif class Class { public: int Get() const { return this->Member; } private: int Member = 1; }; int main() { auto const c = std::unique_ptr<Class>(new Class); std::cout << c->Get() << check_cxx14() << check_cxx17() << std::endl; return 0; } ------------------------------------------ 100 Test succeeded Checking whether 'g++ -O2 -I/mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/include -std=gnu++17 -L/mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/lib ' supports 'make_unique'. Try: g++ Line: g++ -O2 -I/mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/include -std=gnu++17 -L/mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/lib /mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/build_dir/host/cmake-3.22.3/Source/Checks/cm_cxx_make_unique.cxx -o cmake_bootstrap_319609_test ---------- file ----------------------- #include <memory> int main() { std::unique_ptr<int> u = std::make_unique<int>(0); return *u; } ------------------------------------------ Test succeeded Checking whether 'g++ -O2 -I/mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/include -std=gnu++17 -L/mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/lib ' supports 'filesystem'. Try: g++ Line: g++ -O2 -I/mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/include -std=gnu++17 -L/mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/lib /mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/build_dir/host/cmake-3.22.3/Source/Checks/cm_cxx_filesystem.cxx -o cmake_bootstrap_319609_test ---------- file ----------------------- #include <filesystem> int main() { std::filesystem::path p0(L"/a/b/c"); std::filesystem::path p1("/a/b/c"); std::filesystem::path p2("/a/b/c"); if (p1 != p2) { return 1; } #if defined(_WIN32) std::filesystem::path p3("//host/a/b/../c"); if (p3.lexically_normal().generic_string() != "//host/a/c") { return 1; } std::filesystem::path p4("c://a/.///b/../"); if (p4.lexically_normal().generic_string() != "c:/a/") { return 1; } #endif return 0; } ------------------------------------------ /mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/build_dir/host/cmake-3.22.3/Source/Checks/cm_cxx_filesystem.cxx:2:22: fatal error: filesystem: No such file or directory compilation terminated. Test failed to compile Try: /mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/bin/ninja /mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/build_dir/host/cmake-3.22.3/bootstrap: 890: /mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/build_dir/host/cmake-3.22.3/bootstrap: /mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/bin/ninja: not found /mydisk/be65pro/prplos/board/qca_ipq53xx/sdk/12.2/staging_dir/host/bin/ninja does not work
最新发布
09-02
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值