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 was not declared in this scopeこのコードは、 main.cpp で追加のエラーを生成します。他のオーバーラップ関数 (他のファイルでオーバーロードされている) については、それらがクラス内のフレンド関数である場合に同様のエラーが発生します: error: no matching function for call to '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

テレポート.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

静的変数のない他のクラスは問題なく動作するため、静的変数がこれと関係があるのではないかと思います。

編集:実際には、静的変数は問題ではないようです。staticTeleport クラスの定義からキーワードを削除し、名前を付けてbool Teleport::veracity;;をコメントアウトしました。それにもかかわらず、私はまだ2つのエラーが発生します

4

1 に答える 1

1

あなたはおそらくしたいですか?

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();
于 2010-09-12T05:14:44.743 に答える