Xcode:Singleton实现错误:";“类的重新定义”; 我试图在XCODE项目内部实现C++单件,但我得到了这个错误: Redefinition of class
这是我的代码(.hpp文件): 在这一行(在我的cpp文件上) 我得到了这个错误:Xcode:Singleton实现错误:";“类的重新定义”; 我试图在XCODE项目内部实现C++单件,但我得到了这个错误: Redefinition of class,c++,ios,xcode,compiler-errors,C++,Ios,Xcode,Compiler Errors,这是我的代码(.hpp文件): 在这一行(在我的cpp文件上) 我得到了这个错误: Redefinition of class 对“做某事”的重新定义 你们中有谁知道我做错了什么,或者如何纠正这个错误? 非常感谢您的帮助。您在同一个翻译单元DoingSomething.cpp中声明了两次类,即一次声明在您包含的头文件中,另一次声明在cpp文件本身中。 将类声明放在头文件中,将实现放在.cpp-文件中: 标题,即做某事.hpp #ifndef DoingSomething_hpp #defin
Redefinition of class
对“做某事”的重新定义
你们中有谁知道我做错了什么,或者如何纠正这个错误?
非常感谢您的帮助。您在同一个翻译单元
DoingSomething.cpp
中声明了两次类,即一次声明在您包含的头文件中,另一次声明在cpp
文件本身中。
将类声明放在头文件中,将实现放在.cpp
-文件中:
标题,即做某事.hpp
#ifndef DoingSomething_hpp
#define DoingSomething_hpp
#include <stdio.h>
class DoingSomething {
public:
int doSomething();
static DoingSomething *instance();
};
#endif /* DoingSomething_hpp */
#include "DoingSomething.hpp"
int DoingSomething ::doSomething() {
return 6;
}
DoingSomething *DoingSomething::instance() {
if (!shareInstance)
shareInstance = new DoingSomething;
return shareInstance;
}
.cpp文件中的整个
类
声明不属于那里。只有实现才能做到这一点。这个错误是不言自明的。您已经在标题中定义了DoingSomething
的外观。C++中没有做过的事情。
#ifndef DoingSomething_hpp
#define DoingSomething_hpp
#include <stdio.h>
class DoingSomething {
public:
int doSomething();
static DoingSomething *instance();
};
#endif /* DoingSomething_hpp */
#include "DoingSomething.hpp"
int DoingSomething ::doSomething() {
return 6;
}
DoingSomething *DoingSomething::instance() {
if (!shareInstance)
shareInstance = new DoingSomething;
return shareInstance;
}