Why is my bool function returning 12 and 24 (but only when called upon by another function)?

I'm trying to make an iterated prisoner's dilemma tournament to get more familiar with classes in C++. There are various classes that act as bots that interact with each other. I was trying to use polymorphism so I wouldn't need to explicitly call the bot's class. However, my react function works differently in different places.

It works fine when called from main but returns odd values when called by my fight function. I know it's not because of some other code because I've printed out the values of react in the function.

Base class:
1
2
3
4
5
class Prisoner{
public:
    bool virtual react(std::vector<bool>& opturns){};
    static PD_results fight(Prisoner p1, Prisoner p2, int total_turns);
}; //PD_results is one of my structs 


React function in a bot:
1
2
3
4
5
6
7
class T4T : public Prisoner {
public:
    bool react(std::vector<bool>& opturns) override{ //(opturns meaning opponent's turns
        if (opturns.size() == 0) return 1;
        return opturns.back();
    }
};

Fight function: (cout added for troubleshooting)
1
2
3
4
5
6
7
8
9
10
PD_results Prisoner::fight(Prisoner p1, Prisoner p2, int total_turns){
    std::vector<bool> p1_turns;
    std::vector<bool> p2_turns;
    for (int i = 0; i < total_turns; i++){
        bool new_move = p1.react(p2_turns);
        p2_turns.push_back(p2.react(p1_turns)); 
        p1_turns.push_back(new_move);
        std::cout<<p1.react(p2_turns)<<" | "<<p2.react(p1_turns)<<'\n';
    }
//of course this isn't the end of the function 


Result I recieve from that cout when fight is called:
1
2
3
4
5
6
7
8
9
28 | 24
28 | 24
28 | 24
28 | 24
28 | 24
28 | 24
28 | 24
28 | 24
28 | 24

The output is supposed to be ones and zeroes yet isn't. (People with questions similar to mine (bool returning 24) have been told that there are macro errors and differences in source files however I only have one source file and no macros. What am I doing wrong?

Last edited on
PD_results Prisoner::fight(Prisoner p1, Prisoner p2, int total_turns);
You are passing p1 and p2 by value, so p1 and p2 are copies of whatever you pass in. The important thing is that they Prisoner objects, regardless of whether or not your arguments are derived from Prisoner (like class T4T).

Since p1 and p2 are Prisoner instances, they call Prisoner::react(), which doesn't have a return statement, so the caller will pickup whatever some random garbage as the return value (technically it's undefined behavior and you can't rely on anything about the program from that point forward).
bool virtual react(std::vector<bool>& opturns){};

Change fight() to pass the Prisoners by reference. And change Prisoner::react() to be pure virtual, or have it return something meaningful.
fight() takes p1 and p2 by value, but to use polymorphism you have to use either references or pointers. The result is that you're constructing Prisoner instances out of T4T instances. p1.react() actually calls Prisoner::react(), which is a non-void function with no return statement, so its return value is undefined.

My advice: Prisoner::react() should probably be pure virtual, making Prisoner an abstract class. And of course p1 and p2 should be passed by reference.
Topic archived. No new replies allowed.