Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.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
Xcode:Singleton实现错误:";“类的重新定义”; 我试图在XCODE项目内部实现C++单件,但我得到了这个错误: Redefinition of class_C++_Ios_Xcode_Compiler Errors - Fatal编程技术网

Xcode:Singleton实现错误:";“类的重新定义”; 我试图在XCODE项目内部实现C++单件,但我得到了这个错误: Redefinition of class

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

这是我的代码(.hpp文件):

在这一行(在我的cpp文件上)

我得到了这个错误:

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;
}