使用字符串时 C++ 未知覆盖说明符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34514550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:23:54  来源:igfitidea点击:

C++ unknown override specifier when using strings

c++string

提问by Odai Mohammed

I've trying to solve this problem for a long time now:

我已经尝试解决这个问题很长时间了:

I have the following class (Doctor):

我有以下课程(博士):

Doctor.h:

医生.h:

    #pragma once
    class Doctor
    {
    private:
         string firstName;
    public:
        Doctor();
        ~Doctor();
    };

Doctor.cpp:

医生.cpp:

    #include "stdafx.h"
    #include "string"
    #include "Doctor.h"

    using namespace std;

    Doctor::Doctor()
    {
    }


    Doctor::~Doctor()
    {
    }

and my main function:

和我的主要功能:

   #include "stdafx.h"
   #include <string>
   #include "Doctor.h"

   using namespace std;

   int main()
   {
       return 0;
   }

Error C3646 'firstName': unknown override specifier

错误 C3646“firstName”:未知的覆盖说明符

Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int

错误 C4430 缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int

Error C3646 'firstName': unknown override specifier

错误 C3646“firstName”:未知的覆盖说明符

Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int

错误 C4430 缺少类型说明符 - 假定为 int。注意:C++ 不支持 default-int

all at line 5 of Doctor.h where I declare a string variable

全部在 Doctor.h 的第 5 行,我在其中声明了一个字符串变量

回答by Abhishek Kumar

Perhaps

也许

<string> header is missing.

Try including it.

尝试包括它。

Doctor.h

医生.h

#ifndef DOCTOR_H_
#define DOCTOR_H_

#include <string>

class Doctor{
    std::string name;
public:
    Doctor();
    ~Doctor();
};
#endif

Doctor.cpp

医生.cpp

#include "Doctor.h"

Doctor::Doctor()
{
}

Doctor::~Doctor()
{
}

Main.cpp

主程序

#include "Doctor.h"
int main()
{
    return 0;
}

It is working fine. I have tested.

它工作正常。我已经测试过了。

回答by Cory Kramer

You need to #include <string>in Doctor.h, and you need the standard namespace std::string.

您需要#include <string>in Doctor.h,并且需要标准命名空间std::string