友元函数无法识别
我有以下带有几个友元函数的类:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许你想要?
然后
you want perhaps?
then