【发布时间】:2017-03-07 04:35:53
【问题描述】:
我为 std::string 创建了一个装饰器类,当它包含在项目中时,会导致数千个源自 cmath.h、cstring.h、xstring.h 等文件的构建错误,我不知道为什么.项目中所有需要字符串操作的文件都使用这个类而不是 std::string 以保持一致性。
我试图慢慢地注释掉装饰器类的某些部分,以试图理解实际发生的事情,但只有在整个类被注释掉之后,错误才开始变得有意义。错误太多,无法在此处一一列出,但其中的一小部分错误包括:
Error C2733 'abs': second C linkage of overloaded function not allowed
Error C2065 'allocator': undeclared identifier
Error C2974 'std::basic_string': invalid template argument for '_Alloc'
Error C2873 'strxfrm': symbol cannot be used in a using-declaration
Error C2535 'void std::basic_string<_Elem,_Traits,_Alloc>::_Construct(_Iter,_Iter)': member function already defined or declared
在多个标准库文件中存在数千个此类错误。这是字符串装饰器的头文件:
#pragma once
#include <string>
#include <vector>
namespace Framework
{
class String
{
private:
std::string Data;
public:
String();
String(const char*Init);
String(int Init);
friend String operator+(String &LHS, String &RHS);
friend String operator+(String &LHS, const char *RHS);
friend String operator+(String &LHS, const char RHS);
String& operator+=(String &RHS);
String& operator+=(const char *RHS);
String& operator+=(const char RHS);
friend bool operator==(String &LHS, String &RHS);
friend bool operator==(const String &LHS, const String &RHS);
friend bool operator==(String &LHS, const char *RHS);
friend bool operator==(const String &LHS, const char *RHS);
friend bool operator!=(String &LHS, String &RHS);
friend bool operator!=(const String &LHS, const String &RHS);
friend bool operator!=(String &LHS, const char *RHS);
String& operator=(const char * RHS);
char operator[](int Index);
size_t Length();
size_t IndexOf(String SubString, size_t Offset = 0);
bool Contains(String SubString, size_t Offset = 0);
String SubString(size_t Start, size_t Count = 0);
std::vector<String> Split(char Delimeter, bool KeepEmpty = false);
const char *ToCharString();
void Insert(int Position, String Text);
void RemoveAt(int Index);
int ToInt();
double ToDouble();
bool ToBoolean();
unsigned __int8 ToByte();
};
}
更令人费解的是,这个外观在另一个项目中工作得很好,只是在这个新项目中它似乎失败了。
【问题讨论】:
-
您发布的内容是使用 Visual Studio 为我编译的,但显然您发布的内容不是一个完整的示例,所以谁知道我们看不到代码中的内容。你能编译其他东西吗?
-
是的,这个项目很大而且很复杂,有很多活动部件。我能够编译使用相同装饰器类的其他项目。但在这种情况下,它的一个项目正在使用它并且它不会编译。
-
如果这段代码在另一个项目中工作,那么看起来这段代码不是问题,而是其他问题。
-
如果我不得不猜测:此代码不起作用的项目将
String(或您的课程中使用的其他名称)定义为宏。 -
让我认为这是问题的根源是,在我将文件添加到项目之前,在添加 String.h 和 String 之后,构建错误是关于缺少 Framework::String .cpp 文件,它开始吐出数千个奇怪的错误。
标签: c++ visual-c++ visual-studio-2015