简体   繁体   中英

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

I came across this error:

main.cpp:22:34: error: 'getTotalSystemMemory' was not declared in this scope

#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;
}

I assumed the the method 'getTotalSystemMemory' is in scope since it is within the same class

You need to provide at least a declaration before you use the function:

long getTotalSystemMemory(); //declaration

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

long getTotalSystemMemory()
{
    //...
}

You have to declare the function first with C/C++.

Put this before main

long getTotalSystemMemory();

You must "prototype" the function if you are going to be defining it below int main() {} :

long getTotalSystemMemory();

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

long getTotalSystemMemory() {}

in c++ you must write the function before the functions that use it.

if you don't want to write the function in the top, you must put a prototype before. prototypes look like that:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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