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++;功能可达性 我在C++程序中有问题。_C++_Function - Fatal编程技术网

C++;功能可达性 我在C++程序中有问题。

C++;功能可达性 我在C++程序中有问题。,c++,function,C++,Function,我有6个文件:main.cpp、Helper.h、Data_1.h、Data_1.cpp、Data_2.h、Data_2.cpp 以下是每个文件的源代码: main.cpp: #include <iostream> #include <stdio.h> #include <stdlib.h> #include "Data_1.h" #include "Data_2.h"

我有6个文件:main.cpp、Helper.h、Data_1.h、Data_1.cpp、Data_2.h、Data_2.cpp

以下是每个文件的源代码:

main.cpp:

    #include <iostream>

    #include <stdio.h>
    #include <stdlib.h>

    #include "Data_1.h"
    #include "Data_2.h"

    using namespace std;

    int main()
    {
    Data_1 D1;
    Data_2 D2;

    D1.Print_Data_1();
    D2.Print_Data_2();

    cout << "Hello world!" << endl;
    
    return 0;
}
数据_1.h:

#ifndef DATA_1_H_INCLUDED
#define DATA_1_H_INCLUDED

#include <stdio.h>
#include <stdlib.h>
#include "Helper.h"

class Data_1: public My
{

public:

Data_1();


void Print_Data_1();

~Data_1();




};
#endif // DATA_1_H_INCLUDED
数据_2.h:

#ifndef DATA_2_H_INCLUDED
#define DATA_2_H_INCLUDED


#include <stdio.h>
#include <stdlib.h>

#include "Helper.h"


class Data_2: public My
{

public:

Data_2();

void Print_Data_2();

~Data_2();



};

#endif // DATA_2_H_INCLUDED
当我试图编译代码时,它编译成功

输出:

Hello 1
Helper
Hello 2
Helper
Hello world!
我决定删除Helper.h中的结构“My”,只保留函数Func(),还删除了Data_1.h和Data2_.h中的继承

这是删除结构后的Helper.h:

 #ifndef HELPER_H_INCLUDED
 #define HELPER_H_INCLUDED


void Func()
{
printf("Helper\n");
}

#endif // HELPER_H_INCLUDED
当我试图编译整个代码时,它失败了,编译器告诉我:

... Helper.h:10: multiple definition of `Func()' 
... Helper.h:10: first defined here
我的代码在删除结构之前已成功编译

我的问题:为什么代码使用结构“My”成功编译,为什么代码在编译后失败 删除结构


您能帮助我吗。

每当在头文件中定义函数时,必须将其声明为
内联
。否则,它的定义会复制到包含该头文件的所有位置,因此会出现“多个定义”错误。记住,包含C++中的头文件基本上意味着编译器在包含.< /P>的位置复制文件的内容。
在类的声明中定义的类方法被编译器认为是内联的。

每当在头文件中定义函数时,必须将其声明为
inline
。否则,它的定义会复制到包含该头文件的所有位置,因此会出现“多个定义”错误。记住,包含C++中的头文件基本上意味着编译器在包含.< /P>的位置复制文件的内容。
在类的声明中定义的类方法被编译器视为内联的。

Func()
inside
struct
被视为内联的,而
struct
之外的则不是。非常感谢。
Func()
内部
struct
被视为内联,而外部
struct
则不被视为内联。非常感谢。
Hello 1
Helper
Hello 2
Helper
Hello world!
 #ifndef HELPER_H_INCLUDED
 #define HELPER_H_INCLUDED


void Func()
{
printf("Helper\n");
}

#endif // HELPER_H_INCLUDED
... Helper.h:10: multiple definition of `Func()' 
... Helper.h:10: first defined here