0

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
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
JohnJameson
  • 81
  • 1
  • 12
  • `secondClass` needs to either inherit from `firstClass`, or have a object of `firstClass` in it. – NathanOliver Jul 27 '21 at 21:00
  • 6
    You don't "run" class functions, you *call* function on *objects*. You need to create instances of the classes to be able to call the member functions. Or make them `static`. – Some programmer dude Jul 27 '21 at 21:02
  • I edited my code (once I send this I will edit) and now I get `multiple definition of first` and `multiple definition of second` What did I do wrong? – JohnJameson Jul 27 '21 at 21:04
  • `firstClass first;` is a bug and `secondClass second;` is also a bug. Don't define your variables in a header. – drescherjm Jul 27 '21 at 21:07
  • Will defining `firstClass first;` and `secondClass second;` in each cpp file fix the problem? @drescherjm (I edited my comment too) – JohnJameson Jul 27 '21 at 21:08
  • 1
    You need to make sure your `first` and `second` are declared as `extern` variables in header file, and than **define** them in cpp file. – SergeyA Jul 27 '21 at 21:09
  • 1
    Related: [https://stackoverflow.com/questions/11072244/c-multiple-definitions-of-a-variable](https://stackoverflow.com/questions/11072244/c-multiple-definitions-of-a-variable) – drescherjm Jul 27 '21 at 21:11
  • I still get errors when I type `extern firstClass first;` (and for the second class) in the header, I am defining them in the cpp as well, `firstClass first;` but it still doesn't work @drescherjm @SergeyA – JohnJameson Jul 27 '21 at 21:20
  • What errors do you get? It should not be multiple definition errors. – drescherjm Jul 27 '21 at 21:21
  • /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 @drescherjm I will update my code after I send this – JohnJameson Jul 27 '21 at 21:22
  • 2
    Instead of this *help me get this code working ASAP i need it for a friend* approach here, you should probably stick to what you know/understand. Then when you have the time, learn about classes and some of the basic OOP concepts with a good book/tutorial. – super Jul 27 '21 at 21:22
  • 2
    Get rid of `firstClass first;` and `secondClass second;` from `test.cpp`. These need to be defined in exactly 1 file not more than 1. – drescherjm Jul 27 '21 at 21:25
  • Another approach: If you make `secondClass` a child of `firstClass` and `runMe` virtual in `firstClass`, you could redefine `runMe` in `secondClass` to do what it does in `firstClass` and then add further behavior in `secondClass`. – Shaavin Jul 28 '21 at 01:34
  • Please don't change the question in such radical ways like you have done. That makes all posted comments and answers (if posted) wrong. If the comments and possible answers help you solve the current problem, leading to new problems then ask a *new* question for the new problems. – Some programmer dude Jul 28 '21 at 05:08
  • Also please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please take the [tour] and read about [ask] good questions. Lastly please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Jul 28 '21 at 05:09
  • LAstly please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](https://meta.stackoverflow.com/questions/326569/under-what-circumstances-may-i-add-urgent-or-other-similar-phrases-to-my-quest) TL;DR: The answer is *never*. – Some programmer dude Jul 28 '21 at 05:10

0 Answers0