繁体   English   中英

main.cpp:22:34:错误:未在此>范围中声明'xxxxxxx'

[英]main.cpp:22:34: error: ‘xxxxxxx’ was not declared in this > scope

我遇到此错误:

main.cpp:22:34:错误:未在此范围内声明'getTotalSystemMemory'

#include <unistd.h>
#include <cstdlib>
#include <stdio.h>
#include <iostream>

using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {

    cout << "Hello World! \n";

    cout << getTotalSystemMemory();

    return 0;
}

long getTotalSystemMemory()
{
    long pages = sysconf(_SC_PHYS_PAGES);
    long page_size = sysconf(_SC_PAGE_SIZE);
    return pages * page_size;
}

我假设方法“ getTotalSystemMemory”在范围内,因为它在同一类中

使用该函数之前,您至少需要提供一个声明

long getTotalSystemMemory(); //declaration

int main(int argc, char** argv) {
    //...
    cout << getTotalSystemMemory();
    //...
}

long getTotalSystemMemory()
{
    //...
}

您必须先使用C / C ++声明该函数。

把这个放在main

long getTotalSystemMemory();

如果要在int main() {}下定义函数,则必须“原型化”该函数:

long getTotalSystemMemory();

int main() {
    /* ....... */
    getTotalSystemMemory();
}

long getTotalSystemMemory() {}

在c ++中,必须在使用该函数的函数之前编写该函数。

before. 如果您不想在顶部编写该函数,则必须先放置一个 原型看起来像这样:

<return-value> <function name> (<parameters type>);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM