[英]c++ function does not take 0 arguments
为什么我会得到这个错误从编译器有关的函数不采取0参数呢? 是因为我在调用函数后声明了该函数?
// HelloWorld.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!\n";
cout << "Game over!\n";
swap();
system("pause");
return 0;
}
int swap()
{
int on = 1;
int off = 0;
int temp = on;
on = off;
off = temp;
return 0;
}
是因为我在调用函数后声明了该函数?
是。
到编译器看到对swap()
的调用时,它还不知道您的函数。 在这种情况下,通常情况下会出现“调用未声明的函数”的错误,如果不是因为std::swap
(带有两个参数)被using namespace std
拖入名称空间中的话指示。
为了解决问题:将swap
的定义移到main
之上 (因为函数定义始终也是函数声明)或将其保留在put的专用声明中
int swap();
以上main
。 我还将摆脱using namespace std;
如您所见,这样做可能弊大于利,而是用std::
显式为所有标准库类型和函数添加前缀。 但这不是强制性的,也不是当前问题的根本原因。
尝试在main之上定义函数或只在main之上声明。它现在从.net库调用swap
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.