将启动的类传递给函数而不定义类?

时间:2019-01-06 11:53:45

标签: c++ xcode macos oop clang

我实质上是在尝试将一个类的初始化实例传递给另一个类的方法,并且当我尝试用该类#include传递文件时(以便我可以将参数的类型声明为该类)我最终遇到了重新定义错误(见下文)

/Users/arya/Desktop/Arya/Coding/macOS/xx:xxxx/xx:xxxx/Source/Utility/Process/Main.cpp:4:7: Redefinition of 'Process'

/Users/arya/Desktop/Arya/Coding/macOS/xx:xxxx/xx:xxxx/Main.cpp:1:10: In file included from /Users/arya/Desktop/Arya/Coding/macOS/xx:xxxx/xx:xxxx/    Main.cpp:1:

/Users/arya/Desktop/Arya/Coding/macOS/xx:xxxx/xx:xxxx/Header.hpp:9:10: In file included from /Users/arya/Desktop/Arya/Coding/macOS/xx:xxxx/    xx:xxxx/./Header.hpp:9:

/Users/arya/Desktop/Arya/Coding/macOS/xx:xxxx/xx:xxxx/Source/Utility/Scanner/Main.cpp:1:10: In file included from
/Users/arya/Desktop/Arya/Coding/macOS/xx:xxxx/xx:xxxx/./Source/Utility/Scanner/Main.cpp:1:

/Users/arya/Desktop/Arya/Coding/macOS/xx:xxxx/xx:xxxx/Source/Utility/Scanner/Header.hpp:10:10: In file included from /Users/arya/Desktop/Arya/    Coding/macOS/xx:xxxx/xx:xxxx/./Source/Utility/Scanner/./Header.hpp:10:

/Users/arya/Desktop/Arya/Coding/macOS/xx:xxxx/xx:xxxx/Header.hpp:8:10:'/Users/arya/Desktop/Arya/Coding/macOS/xx:xxxx/xx:xxxx/./Source/Utility/    Scanner/../Process/Main.cpp' included multiple times, additional include site here

/Users/arya/Desktop/Arya/Coding/macOS/xx:xxxx/xx:xxxx/Source/Utility/Scanner/Header.hpp:10:10:'/Users/arya/Desktop/Arya/Coding/macOS/xx:xxxx/    xx:xxxx/./Source/Utility/Scanner

/../Process/Main.cpp' included multiple times, additional include site here
/Users/arya/Desktop/Arya/Coding/macOS/xx:xxxx/xx:xxxx/Source/Utility/Process/Main.cpp:4:7: Unguarded header; consider using #ifdef guards or #pragma once

FirstFile.cpp:

#include "./FirstFile.h"
class SomeClass {
    SomeClass() {
        // Do Stuff
    }
}

FirstFile.h:

#ifndef FIRSTFILE_HEADER_HPP
#define FIRSTFILE_HEADER_HPP

#import <iostream>
// import stuff

#endif /* FIRSTFILE_HEADER_HPP */

SecondFile.cpp:

#include "./SecondFile.h"
class SomeClassTwo {
    SomeClassTwo(Someclass * InitializedClass) {
        InitializedClass -> DoSomething()
    }
}

SecondFile.h:

#ifndef SECONDFILE_HEADER_HPP
#define SECONDFILE_HEADER_HPP

#import "./FirstFile.cpp"
// import stuff

#endif /* SECONDFILE_HEADER_HPP */

我已经尝试过使用标题保护,但是仍然没有运气;(

我将不胜感激,如果需要在此添加更多信息,请告诉我

1 个答案:

答案 0 :(得分:1)

如S.M.似乎您误解了头文件的用途和使用方式。

要正确理解它,您需要了解三个概念:

  • 在C ++中,所有符号都必须同时声明 定义。区别在于,声明会告诉编译器某处存在符号,而定义是符号的实际实现或(确实)定义。

  • C ++编译器实际上并不处理源文件,而是处理translation units。简而言之,翻译单元是包含所有头文件的单个源文件。

  • 构建过程,该过程分为多个步骤:

    1. 编辑源文件和头文件
    2. 将源文件(翻译单元)构建为目标文件(每个目标文件代表一个翻译单元)
    3. 将目标文件链接到最终可执行文件中

    大部分工作是由单个编译器前端程序完成的,该程序可以隐藏所有这些复杂的功能。

现在回到头文件及其使用方式...

头文件通常用于函数和变量的声明,以及命名空间,结构和类的定义。

源文件文件包含函数,变量和结构/类成员函数的定义(实现)。

您将所需的头文件包含在源文件中。源文件不应包含其他源文件。头文件可以包含其他头文件,但不应包含(或导入)任何源文件。

然后,您分别构建源文件,并将它们链接到单个和最终的可执行程序中。


可能需要一些简单的例子。

头文件foo.h

// Header include guard (to prevent multiple inclusion in a single translation unit)
#ifndef FOO_H
#define FOO_H

// Define the class Foo
class Foo
{
public:
    // Declare the function hello
    void hello();
};

// End of the header include guard
#endif

源文件foo.cpp

// Include the header files we need
#include <iostream>
#include "foo.h"

// Define (implement) the member function
void Foo::hello()
{
    std::cout << "Hello from Foo\n";
}

头文件bar.h

// Header include guard (to prevent multiple inclusion in a single translation unit)
#ifndef BAR_H
#define BAR_H

// We use the Foo class, so need to include the header file where that class is defined
#include "foo.h"

// Define the class Bar
class Bar
{
public:
    // The Bar default constructor, we define it inline
    Bar()
        : my_foo()    // Constructor initializer list, constructs and initializes the member variables
    {
        // Empty body
    }

    // Declare the function my_hello
    void my_hello();

private:
    Foo my_foo;
};

// End of the header include guard
#endif

源文件bar.cpp

#include "bar.h"

// Define the function
void Bar::my_hello()
{
    // Call function from other class
    my_foo.hello();
}

并将所有main.cpp绑定在一起:

// We will use the Bar class, so include the header file where it's defined
#include "bar.h"

int main()
{
    Bar bar;

    bar.my_hello();
}

要构建此文件(假设使用macOS和clang++编译器),可以在终端中使用以下命令(假设源文件和头文件都在当前目录中):

$ clang++ -Wall foo.cpp -c
$ clang++ -Wall bar.cpp -c
$ clang++ -Wall main.cpp -c
$ clang++ foo.o bar.o main.o -o my_example_program

对于编译器选项:

  • -Wall启用更多警告,这是一件好事
  • -c告诉编译器前端程序从翻译单元生成目标文件(而不是尝试创建可执行文件)
  • -o命名输出文件

如果您运行程序

$ ./my_example_program

然后它应该输出

Hello from Foo