views:

69

answers:

1

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

+1  A: 

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();
aaa
what do I put inside the definitions of `isTrue()` and `isNotTrue()` then? I want them as friend functions, not as functions part of `Teleport`
wrongusername
so do I create another `isTrue()` function? and return `isTrue()` back? then I get `redefinition of 'bool isTrue()'`
wrongusername
okay NVM that comment lol... now I just put those two functions there, and it works!!! but why? EDIT: and that trick works with my original program too! thanks so much! but why do I have to put that outside the class too? i'm confused about that part
wrongusername
@wrong you have to define functions before using them, friend does not actualy defines them. I can call Bob a friend but to use him, he must be exist
aaa
@aaa yes, but why do I have to declare them outside the class also?
wrongusername