How can I get one class to run another class' function and vice versa C++?
Example: main.h (Updated/edited)
#ifndef MAIN_H
#define MAIN_H
#include <iostream>
#include "test.h"
class firstClass
{
public:
void runMe();
};
extern firstClass first;
#endif
main.cpp (Updated/edited)
#include "main.h"
firstClass first;
secondClass second;
void firstClass::runMe()
{
std::cout << "I\'m in firstClass\n";
}
int main()
{
second.runMe(); // should output "I'm in secondClass" then "I'm in firstClass"
return 0;
}
test.h (Updated/edited)
#ifndef TEST_H
#define TEST_H
#include <iostream>
#include "main.h"
class secondClass
{
public:
void runMe();
};
extern secondClass second;
#endif
test.cpp (Updated/edited)
#include "test.h"
firstClass first;
secondClass second;
void secondClass::runMe()
{
std::cout << "I\'m in secondClass\n";
first.runMe(); // Now we output the other classs runMe, saying "I'm in firstClass"
}
I really need help, and badly! I'm self taught and really need this program to work, its for a friend who needs it soon.
Edit: I changed the code, and now I get
/tmp/test-034790.o:(.bss+0x1): multiple definition of `first'
/tmp/main-097c28.o:(.bss+0x1): first defined here
/tmp/test-034790.o:(.bss+0x2): multiple definition of `second'
/tmp/main-097c28.o:(.bss+0x2): first defined here