【发布时间】:2017-04-17 22:23:08
【问题描述】:
我正在尝试在 Xcode 项目中实现 C++ 单例,但出现此错误:
Redefinition of class
这是我的代码(.hpp 文件):
#ifndef DoingSomething_hpp
#define DoingSomething_hpp
#include <stdio.h>
#endif /* DoingSomething_hpp */
class DoingSomething {
public:
static DoingSomething *instance();
};
这是我的 .cpp 文件:
#include "DoingSomething.hpp"
class DoingSomething
{
static DoingSomething *shareInstance;
public:
int doSomething()
{
/*
*/
return 6;
}
static DoingSomething *instance()
{
if (!shareInstance)
shareInstance = new DoingSomething;
return shareInstance;
}
};
在这一行(在我的 cpp 文件上)
class DoingSomething
我收到此错误:
重新定义“DoingSomething”。
你们中的任何人都知道我做错了什么或者如何解决这个错误? 非常感谢您的帮助。
【问题讨论】:
-
.cpp 文件中的整个
class声明不属于那里。只有 实现 去那里。该错误是不言自明的。您已经在标题中定义了DoingSomething的外观。 C++ 中没有重做。
标签: c++ ios xcode compiler-errors