Xcode指出,在我的主文件中,“ stdDev”是未声明的标识符,但已在头文件中声明。我完全烧毁了解决方法。我将不胜感激!
#include <iostream>
#include <string>
#include <cmath>
#include "Person.hpp"
using namespace std;
int main()
{
Person personRob("Rob", 95);
Person personBob("Bob", 89);
Person personGob("Gob", 99);
Person personArray[] = {personRob, personBob, personGob};
Person whole_class;
cout << "Standard deviation is: " << stdDev /* where Xcode is saying that stdDev is an undeclared identifier */ << endl;
return 0;
}
person.hpp:
#include <iostream>
#include <string>
#ifndef PERSON_HPP
#define PERSON_HPP
class Person
{
private:
std::string name;
double age;
public:
Person(std::string = " ", double = 0.0);
std::string getName();
double getAge();
double stdDev(Person personArray[], int size);
};
#endif
最佳答案
stdDev
被声明为非静态成员函数。为了使用它,您需要在带有适当参数的对象上调用它,例如:
std::cout << whole_class.stdDev(personArray, 3)
这就是语法修复。
但是,听起来确实适合成员函数。从函数的名称看来,您打算计算数组中
Person
列表的年龄的标准差。使其成为非成员函数。
然后,您可以将其用作:
std::cout << stdDev(personArray, 3)
关于c++ - C++主文件具有未声明的标识符,该标识符在头文件(Xcode)中声明,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44015774/