简体   繁体   中英

Passing Initiated Class To Function Without Defining Class?

I'm essentially trying to pass an initiated instance of a class to another class's method, and when I try to #include the file with the class (so that I can declare my parameter's type to be that class) I end up with a redefinition error (see below)

/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 */

I've tried using header guards but still no luck ;(

I would appreciate any help and let me know if I need to add any more information to this

As mentioned in the comment by SM it seems like you misunderstand what header files are for and how they are used.

To properly understand it, there are three concepts you need to know:

  • In C++ all symbols needs to be both declared and defined . The difference is that a declaration tells the compiler that a symbol exists somewhere , while the definition is the actual implementation or (indeed) definition of the symbol.

  • The C++ compiler doesn't really deal with source files, but with translation units . In short a translation unit is a single source file all included header files.

  • The build process, which is done in multiple steps:

    1. Edit source and header files
    2. Build source files (translation units) into object files (each object file represents a single translation unit)
    3. Link object files into the final executable

    Most of the work is done by the single compiler frontend program which can hide a lot of the complexities behind all this.

Now back to the header files and how they are used...

Header files are usually used for declarations of functions and variables, and the definition of namespaces, structures and classes.

The source files files contain the definition (implementation) of the functions, variables, and structure/class member functions.

You include the needed header files into the source files. Source files should not include other source files. The header files can include other header files, but should not include (or import) any source files.

Then you build the source files separately and link them into the single and final executable program.


Some simple example might be needed.

Header file 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

Source file 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";
}

Header file 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

Source file bar.cpp :

#include "bar.h"

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

And to bind it all together, the main.cpp file:

// 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();
}

To build this (assuming macOS and the clang++ compiler) you can use the following commands in a terminal (assuming the source and header files all in the current directory):

$ 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

For the compiler options:

  • -Wall enable more warnings, which is a good thing
  • -c tells the compiler frontend program to generate an object file from the translation unit (instead of attempting to create an executable)
  • -o to name the output file

If you run the program

$ ./my_example_program

then it should output

Hello from Foo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM