I have a method in foo class that needs to be initialized in my main.cpp and inside another method of foo.cpp.

basically my example has a mainPage then will allow to user to do addition or subtraction depends on their choice. after the addtional/subtraction method is done, it will go back to the main page again

here's how it goes

Senario 1
if I do this

foo.h

    #ifndef FOO_H
    #define FOO_H

    class foo {

    public:
           void mainPage();
           void add();
           void minus();
    private:
           int input;
    };

    #endif

foo.cpp

    #include "foo.h"
    #include <iostream>
    #include <string>

    void add() {
       //do addition formula
       //go back to mainPage after addition formula is done
       mainPage();
    }

    void minus() {
        //do subtraction formula
       //go back to mainPage after subtraction formula is done
       mainPage();
    }

    void foo::mainPage() {
    std::cout << " " << std::endl;
    std::cout <<"Welcome to main menu std::endl;
    std::cout << "1) Choice 1! ADD!" << std::endl
    std::cout << "1) Choice 2! Minus" << std::endl    
    std::cout << "\nEnter your choice" << std::endl;
    std::cin >> input;

    switch ( input ) {
        case 1:
               //go to addition function
               add();
            break;
        case 2:
              //go to subtraction function
              minus();
            break;

        default:
            std::cout << "Invalid choice!" << std::endl;
            break;
            std::cin.get();

    }

main.cpp

     #include foo.h
     int main() {
         foo Foo;
         Foo.mainPage();
     }

my netbeans will show a error code of 2
reason

    multiple definition of mainPage();

Senario 2
now if I do this

foo.h

    #ifndef FOO_H
    #define FOO_H

    class foo {

    public:
           void mainPage();
           void add();
           void minus();
    private:
           int input;
    };

    #endif

foo.cpp

    #include "foo.h"
    #include <iostream>
    #include <string>

    void add() {
       //do addition
       //go back to mainPage after addition is done
       mainPage();
    }

    void minus() {
        //do subtraction
       //go back to mainPage after subtraction is done
       mainPage();
    }

    inline void foo::mainPage() {
    std::cout << " " << std::endl;
    std::cout <<"Welcome to main menu std::endl;
    std::cout << "1) Choice 1! ADD!" << std::endl
    std::cout << "1) Choice 2! Minus" << std::endl    
    std::cout << "\nEnter your choice" << std::endl;
    std::cin >> input;

    switch ( input ) {
        case 1:
               //go to addition function
            break;
        case 2:
              //go to subtraction function
            break;

        default:
            std::cout << "Invalid choice!" << std::endl;
            break;
            std::cin.get();

    }

main.cpp

     #include foo.h
     int main() {
         foo Foo;
         Foo.mainPage();
     }

my netbeans will show a error code of 2
reason

        undefined reference of foo::mainPage()

what should I do? Please help

Recommended Answers

All 4 Replies

First code: You're calling mainPage() as if it belongs to the global scope. The way you've written your code, though, it actually belongs to your class foo. My best bet is, because of the context, it assumes you're declaring an implicitly-int function called mainPage() inside of both minus() and add(), and therefore complaining about multiple definitions.

You probably meant to have those functions be members for foo anyway, so try rewriting lines 5 and 11 as void foo::add() { and void foo::minus() { respectively.

Second code: When you have an inline function, it can't actually be linked like a normal function. As a result, any code that uses foo::mainPage() must be able to see its full source or you will get undefined refernece errors. In order to mitigate the errors, you would actually have to move your inline function to "foo.h".

On a side note, this code shows very bad flow. You're calling mainPage() from add() and you're calling add() from mainpage. You're basically causing your function to recurse, possibly indefinetely, based on user input. It may be a much better idea to use a real loop inside of mainPage() and just return normally from the other functions.

sorry but would you mind showing an example on how can I improve on my codes?

To be perfectly honest, the main way I would improve that code is to completely get rid of the foo class. It only has one actually peice of data in it (input), and the only place that is used is in foo::mainPage(), so it could easily be made local to that function. If we were working in Java, we would be forced to put this sort of stuff in a class, but because we're working in C++, that's just extra code that serves no purpose.

Here's what I would probably do.

foo.cpp:

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

//Use these parts of iostream so we don't have
//to litter the code with std::whatever.
using std::cout;
using std::endl;
using std::cin;

//Change the declarations of add() and minus() so they return
//an int, or whatever data type we want to work with.
int add() {
    //In a real program, this would have to take
    //some sort of input arguments, and then return
    //the addition of some values. In this example,
    //we'll just return 0 though.
    return 0;
}

int minus() {
    return 0;
}

void mainPage() {
    //Declare input here instead of in a class.
    int input = 0;

    //Loop this function until the user decides to exit, rather than
    //that wierd recursion thing we had going on before.
    while(input != 3) {
        cout << " " << endl;
        cout << "Welcome to main menu" << endl;
        cout << "1) Choice 1! ADD!" << endl;
        cout << "2) Choice 2! Minus" << endl;
        cout << "3) Exit." << endl;
        cout << "\nEnter your choice" << endl;
        cin >> input;

        switch(input) {
            case 1:
                add();
                break;

            case 2:
                minus();
                break;

            case 3:
                cout << "Exiting..." << endl;
                break;

            default:
                cout << "Invalid choice!" << endl;
                break;
        }
    }
}

foo.h:

#ifndef FOO_H
#define FOO_H

void mainPage();
int add();
int minus();

#endif

main.cpp:

#include "foo.h"

int main() {
    mainPage();
}

thank you

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.