友元函数无法识别

发布于 2024-09-18 09:25:20 字数 2284 浏览 8 评论 0原文

我有以下带有几个友元函数的类:

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); 

当我将最后一个函数作为友元函数放入类中时,如下所示:

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

代码会产生额外的错误 overlapT 未在此范围内声明 in main. cpp。至于其他重叠函数(在其他文件中重载),当它们是类中的友元函数时,我会遇到类似的错误:错误:没有调用“overlap()”的匹配函数。我在另一个文件中以相同的方式使用了友元函数,并且没有编译器错误。可能是什么导致了这个奇怪的错误?

好的,有一个小程序来说明这个错误!

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();
}

编译错误:

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

我怀疑静态变量可能与此有关,因为我的其他没有静态变量的类工作得很好。

编辑:实际上,静态变量似乎不是问题所在。我刚刚从 Teleport 类定义/无论它叫什么?中删除了 static 关键字,并注释掉了 bool Teleport::veracity;;尽管如此,我仍然收到两个错误

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏了南城 2024-09-25 09:25:20

也许你想要?

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

然后

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

bool isNotTrue(); // dont forget args
bool isTrue();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文