首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >朋友功能未被识别

朋友功能未被识别
EN

Stack Overflow用户
提问于 2010-09-12 04:45:03
回答 1查看 2.2K关注 0票数 0

我有以下课程,有几个朋友函数:

代码语言:javascript
复制
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); 

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

代码语言:javascript
复制
class Teleport
{
public:
    //...same functions as before...
    friend bool overlapT(vector<Wall *> walls);
    //... same functions as before...
private:
    //... same functions as before...
}

代码在overlapT was not declared in this scope中生成一个额外的错误main.cpp。至于其他重叠函数(在其他文件中重载),当它们是类中的朋友函数时,我会得到类似的错误:error: no matching function for call to 'overlap()'。我认为朋友函数在另一个文件中的使用方式是相同的,并且没有编译器错误。是什么导致了这个奇怪的错误?

好的,有一个小程序证明了这个错误!

main.cpp

代码语言:javascript
复制
#include <iostream>
#include "Teleport.h"

using namespace std;

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

Teleport.h

代码语言:javascript
复制
#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

代码语言:javascript
复制
#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();
}

编译错误:

代码语言:javascript
复制
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;**;,但是我仍然得到了这两个错误**

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-09-12 05:14:44

也许你想要?

代码语言:javascript
复制
class Teleport
{
public:
    Teleport();
    virtual ~Teleport();
    bool isTrue();  // Teleport.isTrue
    bool isNotTrue(); // Teleport.isNotTrue
    friend bool isTrue();
    friend bool isNotTrue();
private:
    static bool veracity;
};

然后

代码语言:javascript
复制
class Teleport
{
public:
    Teleport();
    virtual ~Teleport();
    friend bool isTrue();
    friend bool isNotTrue();
private:
    bool veracity;
};

bool isNotTrue(); // dont forget args
bool isTrue();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3693781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档