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