简体   繁体   English

我是编程新手。 编译包含 ah 文件中包含的 class 定义的代码后,我不断收到错误消息

[英]I am new in programming. I keep getting an error message after compiling my code that contains a class definition included in a .h file

I am trying to call a member function of a custom class, but I keep getting error messages:我正在尝试调用自定义 class 的成员 function,但我不断收到错误消息:

expected primary-expression before First_name 
expected primary-expression before Middle_name 
expected primary-expression before Surname

This is the code for main cpp :这是main cpp的代码:

#include <iostream >
#include <People.h>
auto NAME = P.Name_Input( std:string First_name, std::string Middle_name, std::string Surname) ;
std::cout<<NAME<<std::endl;

This is the header file:这是 header 文件:

class Person{
   public:
   std::string Name_Input(std::string First_name, std::string Middle_name, std::string Surname);
} ;

The header file compiles well, and has been link to a .cpp file to define the method, and that section also works perfectly. header 文件编译良好,并已链接到.cpp文件来定义方法,该部分也可以完美运行。 My problem is with the main.cpp file.我的问题是main.cpp文件。

Neither main.cpp nor People.h are defining std::string , so the compiler doesn't know what it is. main.cppPeople.h都没有定义std::string ,所以编译器不知道它是什么。

You need to add #include <string> to People.h :您需要将#include <string>添加到People.h

#ifndef People_H
#define People_H

#include <string> // <-- add this

class Person{
   public:
      std::string Name_Input(std::string First_name, std::string Middle_name, std::string Surname);
};

#endif

A general rule of thumb is that any source file that needs to use a type defined in another source file should #include that other file (except in cases where using forward declarations will suffice, such as when breaking circular references).一般的经验法则是,任何需要使用在另一个源文件中定义的类型的源文件都应该#include该其他文件(除非使用前向声明就足够了,例如在破坏循环引用时)。 SeeIs it good practice to rely on headers being included transitively?请参阅依赖传递包含标题的好习惯吗? . . Which means that main.cpp should have an #include <string> statement as well, even if People.h (or any other header main.cpp uses) already has its own #include <string> statement.这意味着main.cpp也应该有一个#include <string>语句,即使People.h (或任何其他 header main.cpp使用)已经有自己的#include <string>语句。

Also, your use of std::string in main() is all wrong.此外,您在main()中使用std::string都是错误的。 Don't include the type name when passing a variable to a function or class method.将变量传递给 function 或 class 方法时不要包含类型名称。 Use the type name only in the declaration of a variable or function parameter.仅在变量或 function 参数的声明中使用类型名称。

Try this instead:试试这个:

#include <iostream>
#include <string> // <-- add this
#include "People.h"

int main()
{
    Person P;
    std::string First_name;
    std::string Middle_name;
    std::string Surname;

    ...

    auto NAME = P.Name_Input(First_name, Middle_name, Surname);
    std::cout << NAME << std::endl;

    ...

    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我总是收到错误消息或构建错误消息? - WHy am I keep getting an error message or build error message? 当包含标题时,为什么在代码中出现“使用未定义类型”错误? - Why am I getting an “use of undefined type” error in my code, when I have the header for it included? 为什么我在编译时收到关于在我的代码中丢弃限定符的 g++ 错误? - Why am I getting a g++ error about discarding qualifiers in my code when compiling? 当我包含windows.h时,为什么会出现错误“在此范围内未声明WM_MENUCOMMAND”? - Why am I getting the error “WM_MENUCOMMAND was not declared in this scope” when I included windows.h? 为什么在编译时出现Mammal.h类错误 - why am I getting errors with the Mammal.h class when compiling 我的代码不断收到 SIGTSTP 错误,但我无法弄清楚我的代码在哪里越界 - I keep getting SIGTSTP error for my code but I am not able to figure out where is my code going out of bounds 我正在为我祖母的生日编写 C++ 程序,但是我不断收到错误代码:错误 C2451 - I am working on a C++ program for my grandmother's birthday, however I keep getting error code: error C2451 为什么我在C ++中不断收到错误消息“在&#39;{&#39;标记之前不允许在这里进行功能定义”? - Why do I keep getting error message “a function-definition is not allowed here before '{' token” in C++? 在编译我的程序的C ++控制台时,我在包含的向量头文件中收到错误,可能吗? - in compiling c++ console my program, i get an error in the included vector header file, is it possible? 我收到未声明的标识符错误,但我已包含 header 文件? - I am getting an undeclared identifier error but I have included the header file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM