从指针向量调用函数
Posted
技术标签:
【中文标题】从指针向量调用函数【英文标题】:Calling Functions from a Pointer Vector 【发布时间】:2014-09-05 23:31:08 【问题描述】:我刚接触 C++,所以请耐心等待。在我的班级MyArena
中,我创建了另一个班级Fighter
的指针向量fighters
。使用这个向量,我正在收集 Fighter 指针并为它们调用函数。但我不断收到fighters
的错误。
#include <vector>
using namespace std;
class Fighter;
class MyArena
vector<Fighter*> fighters;
int current_size = 0;
bool MyArena :: addFighter(string info)
for (int i = 0; i < 11; i++)
if (fighters[i]->getName() == n) //error that "point to incomplete pass type is not allowed?"
isLegit = false;
fighters.push_back(new Fighter(n, t, mH, st, sp, m)); //"Fighter" is incomplete type?
return true;
bool removeFighter(string name)
for (int i = 0; i < 11; i++)
if (fighters[i]->getName() == name)//error that "point to incomplete pass type is not allowed?"
fighters[i] = NULL;
;
我应该如何处理这个问题?
【问题讨论】:
Fighter
未定义。将成员函数定义移出标头并放入 .cpp
文件中。
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers
见:How to create a Minimal, Complete, and Verifiable example.
【参考方案1】:
你在这里转发声明类:
class Fighter;
但您从未包含该文件。把它放在你的其他包含之后
#include "Fighter.h"
如果它位于不同的文件夹中,则为到达Fighter.h
的相对路径。
【讨论】:
是的,我忘了#include 类。以上是关于从指针向量调用函数的主要内容,如果未能解决你的问题,请参考以下文章