Search code examples
c++overloadingfriend-function

Friend functions not recognized


I have the following class with a couple friend functions:

class Teleport
{
public:
    Teleport();
    ~Teleport();
    void display();
    Location teleportFrom(int direction);

    friend bool overlap(Wall * wall, Teleport * teleport);
    friend bool overlap(Location location);
    friend bool overlap(Wall * wall);
    friend bool overlap();

    Location location;
    static vector<Teleport *> teleports;
private:

    int currentTeleport;
};

bool overlapT(vector<Wall *> walls); 

When I put the last function as a friend function inside the class like so:

class Teleport
{
public:
    //...same functions as before...
    friend bool overlapT(vector<Wall *> walls);
    //... same functions as before...
private:
    //... same functions as before...
}

The code produces an extra error overlapT was not declared in this scope in main.cpp. As for the other overlap functions (which are overloaded in other files), I get similar errors when they're friend functions in the class: error: no matching function for call to 'overlap()'. I used friend functions in what I believe to be the same way in another file, and have no compiler errors. What might be causing this strange error?

Okay, got a small program that exemplifies this error!

main.cpp

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

using namespace std;

int main()
{
    Teleport teleport;
    isTrue();
    isNotTrue();
    isTrue(1);
    return 0;
}

Teleport.h

#ifndef TELEPORT_H
#define TELEPORT_H


class Teleport
{
public:
    Teleport();
    virtual ~Teleport();
    friend bool isTrue();
    friend bool isNotTrue();
private:
    bool veracity;
};

bool isTrue(int a); //useless param, just there to see if anything happens

#endif // TELEPORT_H

teleport.cpp

#include "Teleport.h"

//bool Teleport::veracity;

Teleport::Teleport()
{
    veracity = true;
}

Teleport::~Teleport()
{
    //dtor
}

bool isTrue()
{
    return Teleport::veracity;
}

bool isNotTrue()
{
    return !Teleport::veracity;
}

bool isTrue(int a)
{
    if(isTrue())
        return true;
    else
        return isNotTrue();
}

Compile errors:

error: too few arguments to function 'bool isTrue(int)'
error: at this point in file
error: 'isNotTrue' was not declared in this scope

I suspect static variables may have something to do with this, as my other classes without static variables work just fine.

EDIT: Actually, it seems static variables are not the problem. I just deleted the static keyword from the Teleport class definition/whatever it's called?, and commented out bool Teleport::veracity;; nevertheless, I still get the two errors


Solution

  • you want perhaps?

    class Teleport
    {
    public:
        Teleport();
        virtual ~Teleport();
        bool isTrue();  // Teleport.isTrue
        bool isNotTrue(); // Teleport.isNotTrue
        friend bool isTrue();
        friend bool isNotTrue();
    private:
        static bool veracity;
    };
    

    then

    class Teleport
    {
    public:
        Teleport();
        virtual ~Teleport();
        friend bool isTrue();
        friend bool isNotTrue();
    private:
        bool veracity;
    };
    
    bool isNotTrue(); // dont forget args
    bool isTrue();