在BST中使用c++使用Object文件的函数
Using functions of an Object file using C++ in a BST
我正在制作一个二叉搜索树,我在一个。cpp文件中有以下函数:
void MovieTree::printMovieInventory(MovieNode* node)
{
if(node)
{
while(node->rightChild && node->leftChild)
{
std::cout<<"Movie:"<<node->title<<" "<<node->quantity<<std::endl;
if(node->rightChild)
{
printMovieInventory(node->rightChild);
}
if(node->leftChild)
{
printMovieInventory(node->leftChild);
}
}
}
else
{
std::cout<<"No movies in list!"<<std::endl;
}
我有点不确定我应该如何在我的main.cpp文件或"驱动程序文件"中引用这个函数。我在main中使用如下方式引用它:
case 3: //message is read in from file
{
MovieTree::printMovieInventory(node);
}
break;
然而,在引用this时,它只是抛出一个错误:
Driver.cpp:37:40: error: cannot call member function 'void MovieTree::printMovieInventory(MovieNode*) without object
MovieTree::printMovieInventory(node);
不知道这是什么意思。
完整main:
int main(int argc, char **argv)
{
bool quit = false;
string s_input;
int input;
// loop until the user quits
while (!quit)
{
MovieNode* node = new MovieNode;
printOptions();
// read in input, assuming a number comes in
getline(cin, s_input);
input = stoi(s_input);
switch (input)
{
// print all nodes
case 1: //rebuild network
break;
break;
case 3: //message is read in from file
{
MovieTree::printMovieInventory(node);
}
break;
case 4: // quit
quit = true;
cout << "Goodbye!"<< endl;
break;
default: // invalid input
cout << "Invalid Input" << endl;
break;
}
}
}
您需要一个MovieTree实例。在代码的某处,在switch语句的上方,您应该添加:
MovieTree movieTree;
,然后你的switch语句看起来像这样:
case 3: //message is read in from file
movieTree.printMovieInventory(node);
break;
或者,如果MovieTree没有状态(即。没有字段或它所依赖的其他函数),您可以将printMoviewTree
标记为static
,而不是上面的代码。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 解析 csv 文件,"malloc: *** error for object 0x7ffeeb4f4b80: pointer being freed was not allocated"出现此错误
- C++未生成'object'文件
- 错误 C2220:警告被视为驱动程序开发中生成的错误无'object'文件
- g++文件格式无法识别;将Object.o视为链接器脚本
- 为什么一个文件中没有错误,另一个文件中没有错误 - "Non Static Member Reference mush be relative to specific object"
- 在BST中使用c++使用Object文件的函数
- make OBJECT文件标志不工作
- 在python中打开共享对象文件时出错(OSError: cannot open shared object file:
- 获取COM-Object的dll文件,而不使用c++中的CLSID和注册表