【发布时间】:2019-01-06 11:53:45
【问题描述】:
我实际上是在尝试将一个类的已启动实例传递给另一个类的方法,当我尝试 #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 */
我尝试过使用标头保护,但仍然没有运气;(
如果我需要为此添加更多信息,我将不胜感激。
【问题讨论】:
-
@Someprogrammerdude 我更新了我原来的帖子,对不起,当我最初发布这个帖子时,我已经筋疲力尽了,因为我一直在努力解决这个问题
-
@S.M.刚刚更新了原始帖子,我尝试过使用标题保护,但我没有运气,再次感谢您花时间帮助一个新手 C++ 开发人员
-
你完全误解了头文件的用途。我建议看看这个stackoverflow.com/questions/388242/… 现在请看看stackoverflow.com/questions/333889/… 和stackoverflow.com/questions/1305947/…