简体   繁体   中英

access methods from other c++ files

I have implemented Customer, DVD, Receipt, classes and 2 files for main functions and menu functions, I included all the .h files, but when I use a function for example from the menu functions, it doesnt work, and says the functions isnt defined. I want to write the in a seperate .cpp file which is called main that is the driver of my application.

Here is my code:

#ifndef CUSTOMER_H
#include "Customer.h"
#endif

#ifndef DVD_H
#include "DVD.h"
#endif

#ifndef MAIN_FUNC_H
#include "MainFunctions.h"
#endif

#ifndef MENU_FUNC_H
#include "MenuFunctions.h"
#endif

#ifndef RECEIPT_H
#include "Receipt.h"
#endif

using namespace std;

int main () {
    intro();

    return 0;
}

and the error is:

Error 1 error C2129: static function 'void intro(void)' declared but not defined c:\\program files (x86)\\microsoft visual studio 10.0\\vc\\include\\xmemory 186 1 DVD_App

even though the function intro() is defined in the MenuFunctions.h file, and implemented in MenuFunctions.cpp

NOTE: The 2 files, MenuFunctions and MainFunctions dont have classes in them, just static functions I also tried it without the include guards, and again didnt work

If you mark function with static (note - function, not method), it is meant to be 'local' for the compilation unit (.cpp file). So it needs to be declared AND defined there, and is not visible from other cpp files. So, removing static means, that the function can be accessed across compilation units.

You are not compiling the MenuFunctions.cpp, in order for the compiler to know the definition of the intro() it needs the implementation code. Check your makefile if you have one, if not, include MenuFunctions.cpp before main(), or specify the MenuFunctions.cpp as well to the compiler.

Make sure the file has 文件具有

#include "MenuFunctions.h"

at the start of it. The error says it's declared, but not defined. Safeguard the #include "MenuFunctions.h" to prevent double include.

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