Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
错误:否'__________';在类'中声明的成员函数_______'; 我把自己看作是一个新手C++程序员,我以前从未经历过这个错误。p>_C++ - Fatal编程技术网

错误:否'__________';在类'中声明的成员函数_______'; 我把自己看作是一个新手C++程序员,我以前从未经历过这个错误。p>

错误:否'__________';在类'中声明的成员函数_______'; 我把自己看作是一个新手C++程序员,我以前从未经历过这个错误。p>,c++,C++,我只是想为我的函数创建一个类,但是我的头文件中声明的所有std::前缀函数都无法识别 //comments //comments //comments //comments //comments //comments //comments //comments //comments //comments //comments #ifndef PERSON_H #define PERSON_H #include <string> class Person { public:

我只是想为我的函数创建一个类,但是我的头文件中声明的所有std::前缀函数都无法识别

//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
#ifndef PERSON_H
#define PERSON_H

#include <string>

class Person
{
    public:
        Person();
        std::string getName();  //return first name
        std::string getSurname();//return surname
        int getWeight();    //return weight
        int getBirthYear(); //return birthyear


    private:
//self explanatory member variables but need to be accessible to patient
        std::string m_name;
        std::string m_surname;
        int m_weight;
        int m_birthYear;
};

#endif      
主要

知道我做错了什么吗?这种完全相同的std::formatting以前也适用于我,但由于某些原因,现在在尝试创建简单Person类时,只有std::string函数无法识别。

来自注释:

正如chris指出的,在compile命令行中包含头文件是不寻常的。您可能在前面使用的调用稍有不同:


创建
Main.o
Person.o
,但也创建一个
Person.h.gch
预编译头。预编译头不会使用当前的build命令重新生成,但仍在使用,因此不会拾取对
Person.h
的更改。

头在build命令中的作用是什么?那么人们对构造函数初始值设定项列表有什么看法呢?不幸的是,这是我被教导编程的唯一方式,也是我的导师/标记人喜欢的方式,因为他们更容易浏览。你的导师在这方面是错的。要求退款。请提供无法编译的实际代码。第一条错误消息指的是第17行,但不是问题中的第17行。您的项目目录中是否有
.pch
文件?在build命令中包含头只针对预编译头,因此您可能只需要预编译较旧版本的
Person.h
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
#include "Person.h"

Person::Person()
{
    m_name = "name";
    m_surname = "surname";
    m_weight = 0;
    m_birthYear = 0;
    return;
}

//returns m_name
std::string Person::getName()   
{
    return m_name;
}

//returns m_surname
std::string Person::getSurname()
{
    return m_surname;
}

//returns persnon's weight
int Person::getWeight()
{
    return m_weight;
}   

//returns the person's birth year
int Person::getBirthYear()
{
    return m_birthYear;
}
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
//comments
#include "Person.h"
#include <iostream>

using namespace std;

int main()
{
//  Person matt;
//  cout << matt.getName() << endl;
//  cout << matt.getSurname() << endl;
//  cout << matt.getWeight() << endl;
//  cout << matt.getBirthYear() << endl;
    return 0;
}
g++ Main.cpp Person.h Person.cpp -o test
Person.cpp: In constructor ‘Person::Person()’:
Person.cpp:17:2: error: ‘m_name’ was not declared in this scope
Person.cpp:18:2: error: ‘m_surname’ was not declared in this scope
Person.cpp: At global scope:
Person.cpp:35:29: error: no ‘std::string Person::getName()’ member function declared in class ‘Person’
Person.cpp:41:32: error: no ‘std::string Person::getSurname()’ member function declared in class ‘Person’
g++ Main.cpp Person.h Person.cpp -o test
g++ -c Main.cpp Person.h Person.cpp