Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.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++_Xcode_Macos_Oop_Clang - Fatal编程技术网

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

C++ 将启动的类传递给函数而不定义类?,c++,xcode,macos,oop,clang,C++,Xcode,Macos,Oop,Clang,我实际上是在尝试将一个类的初始实例传递给另一个类的方法,当我尝试#将文件包含在该类中(以便我可以将参数的类型声明为该类)时,我会出现一个重新定义错误(见下文) FirstFile.cpp: #include "./FirstFile.h" class SomeClass { SomeClass() { // Do Stuff } } #ifndef FIRSTFILE_HEADER_HPP #define FIRSTFILE_HEADER_HPP #impor

我实际上是在尝试将一个类的初始实例传递给另一个类的方法,当我尝试
#将
文件包含在该类中(以便我可以将参数的类型声明为该类)时,我会出现一个重新定义错误(见下文)

FirstFile.cpp:

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

#import <iostream>
// import stuff

#endif /* FIRSTFILE_HEADER_HPP */
#include "./SecondFile.h"
class SomeClassTwo {
    SomeClassTwo(Someclass * InitializedClass) {
        InitializedClass -> DoSomething()
    }
}
#ifndef SECONDFILE_HEADER_HPP
#define SECONDFILE_HEADER_HPP

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

#endif /* SECONDFILE_HEADER_HPP */
FirstFile.h:

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

#import <iostream>
// import stuff

#endif /* FIRSTFILE_HEADER_HPP */
#include "./SecondFile.h"
class SomeClassTwo {
    SomeClassTwo(Someclass * InitializedClass) {
        InitializedClass -> DoSomething()
    }
}
#ifndef SECONDFILE_HEADER_HPP
#define SECONDFILE_HEADER_HPP

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

#endif /* SECONDFILE_HEADER_HPP */
SecondFile.h:

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

#import <iostream>
// import stuff

#endif /* FIRSTFILE_HEADER_HPP */
#include "./SecondFile.h"
class SomeClassTwo {
    SomeClassTwo(Someclass * InitializedClass) {
        InitializedClass -> DoSomething()
    }
}
#ifndef SECONDFILE_HEADER_HPP
#define SECONDFILE_HEADER_HPP

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

#endif /* SECONDFILE_HEADER_HPP */
我尝试过使用头球后卫,但仍然没有运气;(


如有任何帮助,我将不胜感激,并告知我是否需要添加更多信息,如S.M.评论中所述。您似乎误解了头文件的用途和使用方式

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

    在C++中,所有符号都需要声明和定义。不同之处在于,一个声明告诉编译器,某个符号存在于某个地方,而该定义是符号的实际实现或(实际上)定义。

    《C++编译器》并没有真正处理源文件,但简单地说,翻译单元是一个包含所有头文件的单个源文件。
  • 构建过程分多个步骤完成:

  • 编辑源文件和头文件
  • 将源文件(翻译单元)构建到目标文件中(每个目标文件代表一个翻译单元)
  • 将对象文件链接到最终可执行文件中
  • 大部分工作是由单个编译器前端程序完成的,这可以隐藏所有这些背后的许多复杂性

现在回到头文件以及如何使用它们

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

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

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

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


可能需要一些简单的例子

头文件
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
// 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
源文件
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";
}
#include "bar.h"

// Define the function
void Bar::my_hello()
{
    // Call function from other class
    my_foo.hello();
}
源文件
bar.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";
}
#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++
编译器),您可以在终端中使用以下命令(假设源文件和头文件都在当前目录中):

对于编译器选项:

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

$ ./my_example_program
然后它应该输出

Hello from Foo 福先生你好
读这篇请读,特别是和。还有和。最后,请学习如何创建一个。@Someprogrammerdude我更新了我原来的帖子,对不起,当我最初发布这篇文章时,我已经筋疲力尽了,因为我一直在努力解决这个问题this@S.M.刚更新了原来的帖子,我试着使用头球后卫,但我没有运气他们再次感谢你们花时间帮助新手C++开发人员完全误解了使用什么头文件。我建议现在就看看这个。