函数未在作用域中声明/如何将header.h、header.cpp和main.cpp一起使用? 我是C++初学者,在代码< > He/Cuff>文件中,我在函数中声明了函数,在 Health.CPP < /C>中定义,并在 Meal.CPP < /代码>中调用。我一直在寻找解决办法,但我所做的一切似乎都不管用。如果您能就如何解决这些问题提供一些意见,我将不胜感激

函数未在作用域中声明/如何将header.h、header.cpp和main.cpp一起使用? 我是C++初学者,在代码< > He/Cuff>文件中,我在函数中声明了函数,在 Health.CPP < /C>中定义,并在 Meal.CPP < /代码>中调用。我一直在寻找解决办法,但我所做的一切似乎都不管用。如果您能就如何解决这些问题提供一些意见,我将不胜感激,c++,oop,function-call,C++,Oop,Function Call,这是我的密码: main.cpp #include <iostream> #include "DLinkedList.h" int main() { getInfo(); //Running my function getInfo } #include <iostream> #include "DLinkedList.h" int getInfo() { //Defining my function getInfo int answer; std::co

这是我的密码:

main.cpp

#include <iostream>
#include "DLinkedList.h"

int main() {
  getInfo(); //Running my function getInfo
}
#include <iostream>
#include "DLinkedList.h"

int getInfo() { //Defining my function getInfo
  int answer;
  std::cout << "Input the integer you want to store in the node: ";
  std::cin >> answer;
  return answer;
}
DLinkedList.cpp

#include <iostream>
#include "DLinkedList.h"

int main() {
  getInfo(); //Running my function getInfo
}
#include <iostream>
#include "DLinkedList.h"

int getInfo() { //Defining my function getInfo
  int answer;
  std::cout << "Input the integer you want to store in the node: ";
  std::cin >> answer;
  return answer;
}
getInfo()
不是免费函数。它是类
节点
的成员函数。因此,需要像前面一样使用作用域解析运算符定义它所属的类名,即

int Node::getInfo()
{
    // ... body ...
}
并且,在
main
函数中,您需要在使用类的成员函数之前创建类的对象

例如:

int main()
{
    Node node;
    node.getInfo();
    return 0;
}

<> P>在编写代码之前,最好修改OOP概念以及它们是如何在C++中实现的。自由函数和成员函数是不同的。通过一本适当的书(或教程等)有助于为编写OOP代码奠定基础。祝你好运