【问题标题】:C++ main file has undeclared identifier that's declared in the header file (Xcode)C++ 主文件具有在头文件 (Xcode) 中声明的未声明标识符
【发布时间】:2017-05-17 04:42:56
【问题描述】:

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

【问题讨论】:

  • 如果不查看 Person.hpp 的内容,很难判断出什么问题。
  • 我添加了头文件
  • stdDev 是一种方法。它需要在Person 的实例上调用。 cout &lt;&lt; "Standard deviation is: " &lt;&lt; whole_class.stdDev(more missing stuff goes here) &lt;&lt; endl; 之类的东西,否则编译器会认为你有一些变量,名为 stdDev,它找不到。
  • stdDev 在哪里定义?有一个同名的方法,但你可能不是这个意思,如果你这样做了 - 你需要回到基础。

标签: c++ xcode undeclared-identifier


【解决方案1】:

stdDev 被声明为非静态成员函数。为了使用它,您需要在具有适当参数的对象上调用它,例如:

std::cout << whole_class.stdDev(personArray, 3)

这是语法修复。

不过,它听起来确实很适合成员函数。从函数的名称来看,您似乎打算计算数组中Persons 列表的年龄标准差。

使其成为非成员函数。

然后,您可以将其用作:

std::cout << stdDev(personArray, 3)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    相关资源
    最近更新 更多