简体   繁体   中英

I'm having trouble calling a function in my main from another class

I have a main class in which Im trying to call on a function to create the menu but I keep getting this error:

error LNK2019: unresolved external symbol "public: static int __cdecl Controller::menu(void)" (?menu@Controller@@SAHXZ) referenced in function _main

This is my main class.

#include "Main.h"

using namespace std;

int main () 
{
Control:: menu();
return 0;
}

this is the Main.h

#pragma once
#include "Control.h"
class Main:
{
public:
Main(void);
~Main(void);
int main();

};

the Control.h:

#pragma once
#include <iostream>

class Control
{
public:
Control(void);
~Control(void);
 static int menu ();
};

and finally the control cpp file:

#include "Control.h"
using namespace std; 

static int menu () 
{
  bunch of menu code
 return 0;
}

I think it's something simple but I just cant figure it out. I tried removing static as well as changing the function to a void function, but neither worked.

static int menu () 
{
  bunch of menu code
 return 0;
}

should be

int Control::menu () 
{
  bunch of menu code
 return 0;
}

That's the proper way of defining members.

The static function with its prototype should be this way.

int Control :: menu()
{
   //bunch of menu code
   return 0 ;
}

While you are implementing the class in another file, you have to use the class name with scope resolution operator as well.

You are also having an extra colon at the end of class Main resulting in syntax error.

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