如何为类c++; < >我在C++中创建了一个名为命令(文件名命令.CPP)的类。
我已经将其放入一个命令数组(文件名test.cpp) 我想知道的是如何调用Commands类中的函数 例如,我在Commands类中有一个函数 叫 我尝试调用函数的方法是 编辑如何为类c++; < >我在C++中创建了一个名为命令(文件名命令.CPP)的类。,c++,C++,我已经将其放入一个命令数组(文件名test.cpp) 我想知道的是如何调用Commands类中的函数 例如,我在Commands类中有一个函数 叫 我尝试调用函数的方法是 编辑 Class test{ int CmdCount; // number of commands in the array int MaxCmds; // max amount of commands allowed command* cmds; Public:
Class test{
int CmdCount; // number of commands in the array
int MaxCmds; // max amount of commands allowed
command* cmds;
Public:
int get_command_count() const{
return CmdCount;
}
int readfile(const char fname[]){
char line[161];
FILE* fp;
fp = fopen(fname, "r");
if(fp){
for(int i = 0; 1 == fscanf(fp, "%160[^\n]\n", line; i++){
cmds[get_command_count()].init(line);
CmdCount += 1;
}
}
fclose(fp);
}
};
我只想知道如何调用void命令::init(char data[])
有什么建议吗
谢谢。听起来您的数组包含类的实例。在这种情况下,您希望对数组中的单个条目调用该方法:
my_array[i].someMethod();
其中
my_array[i]
是您的类的一个实例。您有什么问题?即,cmds[get_command_count()]init(line)
是类中的一个公共方法,如果是的话,只要行是字符*
并且cmds
正确填充并且get_command_count()
返回一个有效的数组索引get_command_count
听起来好像给出了有效命令的总数,在这种情况下,第一个索引不是有效的命令。@Robᵩ 这是我认为它应该工作的东西,除非我宣布我的数组错误,但它似乎是正确的。我一直遇到分段错误。@keety我尝试了char*但它在我的命令类中不起作用。它必须是一个字符。还有,“正确填充”是什么意思?谢谢
my_array[i].someMethod();