when you write code in the main function if you put functions in the file all you do is nameoffunction(); to call it. now my question is this lets say you have a menu in a class. call it bank. in the main to call that function . ive tried calling it the same way but that wont work because function isnt in main file. i thought that if you declared a class object .. bank account; then account.nameofFunction(); it would call it but it says there is a link error .. how do i call a function holding a menu from my main is actually my question ive gotten myself lost

You lost me but.. If the class is declared in a different file, you must include that header file in your main by doing #include "HeaderFile.hpp" or whatever extension you saved it as.

If it is declared in main like so then you just create an instance of that class and call the function using the dot. If you created an instance through a pointer then you need to use the -> operator (I forgot this operator's name :l). OR if it is a pointer you can use the *. technique which is the same as the -> operator.

class Bank
{
    public:
        void SomeFunction();
};

Menu::SomeFunction()
{
    std::cout<<"Calling Function In Bank Class"<<std::endl;
}


int main()
{
    Bank BMO;

    BMO.SomeFunction();

    Bank* BankPtr = new Bank();

    BankPtr->SomeFunction();

    //OR

    (*BankPtr).SomeFunction();
}

what code are you using?

I am using c++,, i had tried what the first responder said it didnt work i went back and declared the function a friend and it worked that way thanks for the help

You almost certainly don't need to make anything a friend of anything else to do whatever it is that you're trying to do. Post the code that you have so far and we can have a better look at it...

In C++, every cpp file is compiled separately, and then linked together. When the functions appear in the same cpp file as the main() function, then it is compiled together and doesn't need linking. When the functions appear in a different cpp file, then all the compiled cpp files (compiles to .o or .obj files), including the one with the main() function, must be linked together. In order to be able to use the functions from any cpp file, a function needs to be declared in the cpp file in which it is used, usually through the inclusion of a header file that declares a set of functions.

Here is a simple example (hello world):

In hello.h:

#ifndef HELLO_H
#define HELLO_H

// This is a declaration of the function:
void printHelloWorld();

#endif

In hello.cpp:

#include "hello.h"
#include <iostream>

// This is the definition of the function:
void printHelloWorld() {
  std::cout << "Hello World!" << std::endl;
};

In main.cpp:

// include the header file for the function:
#include "hello.h"

int main() {

  // call the function:
  printHelloWorld();

  return 0;
};

Then, you must compile both cpp files and put them together. In an IDE (like Visual Studio or CodeBlocks), you need to add both cpp files to the "project" and build it. If using a compiler like GCC using the command-line, you need to do:

$ gcc -Wall -o main main.cpp hello.cpp

which has the effect of doing the following steps:
compile main.cpp into main.o:

$ gcc -Wall -c -o main.o main.cpp

compile hello.cpp into hello.cpp:

$ gcc -Wall -c -o hello.o hello.cpp

and linking both object files into a program:

$ ld -o main main.o hello.o -lc

When you deal with a class, the situation is the same:

In hello.h:

#ifndef HELLO_H
#define HELLO_H

class HelloWorldPrinter {
  public:
    void printHelloWorld();  // declare member functions.
};

#endif

In hello.cpp:

#include "hello.h"
#include <iostream>

// This is the definition of the member function:
void HelloWorldPrinter::printHelloWorld() {
  std::cout << "Hello World!" << std::endl;
};

In main.cpp:

// include the header file for the function:
#include "hello.h"

int main() {

  // Create an object:
  HelloWorldPrinter printer; 

  // call the function on it:
  printer.printHelloWorld();

  return 0;
};

The rules for compilation are the same, you need to compile both cpp files and link them into one program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.