[英]c++ function does not take 0 arguments
Why am I getting this error from the compiler about the function not taking 0 arguments? 为什么我会得到这个错误从编译器有关的函数不采取0参数呢? Is is because I declare the function after it has been called?
是因为我在调用函数后声明了该函数?
// 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;
}
Is is because I declare the function after it has been called?
是因为我在调用函数后声明了该函数?
Yes. 是。
By the time the compiler sees the call to swap()
, it doesn't know about your function yet. 到编译器看到对
swap()
的调用时,它还不知道您的函数。 You'd normally get an error along the lines of “call to undeclared function” in this case, were it not for std::swap
(which takes two arguments) that you've pulled into your name-space by the using namespace std
directive. 在这种情况下,通常情况下会出现“调用未声明的函数”的错误,如果不是因为
std::swap
(带有两个参数)被using namespace std
拖入名称空间中的话指示。
In order to fix: Move the definition of swap
above main
(as a function definition is always also a function declaration) or leave it where it is an put a dedicated declaration 为了解决问题:将
swap
的定义移到main
之上 (因为函数定义始终也是函数声明)或将其保留在put的专用声明中
int swap();
above main
. 以上
main
。 I'd also get rid of the using namespace std;
我还将摆脱
using namespace std;
as it, as you can see, might do you more harm than good and instead prefix all standard-library types and functions explicitly with std::
. 如您所见,这样做可能弊大于利,而是用
std::
显式为所有标准库类型和函数添加前缀。 But that's not mandatory and also not the root cause of your current issue. 但这不是强制性的,也不是当前问题的根本原因。
尝试在main之上定义函数或只在main之上声明。它现在从.net库调用swap
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.