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


Keywords:c++ 


Question: 

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.


2 Answers: 

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.