Visual c++ VisualC++——“函数已经有了一个体”
这是我的声明,我甚至使用了include-guard: 编辑:如果这有助于回答任何其他问题,我将包括整个标题 一个人可能会有问题Visual c++ VisualC++——“函数已经有了一个体”,visual-c++,Visual C++,这是我的声明,我甚至使用了include-guard: 编辑:如果这有助于回答任何其他问题,我将包括整个标题 一个人可能会有问题 #ifndef STRING_H #define STRING_H #include<iostream> class String { public: String(const char * s = ""); String(const String & s); String operator = (const Strin
#ifndef STRING_H
#define STRING_H
#include<iostream>
class String
{
public:
String(const char * s = "");
String(const String & s);
String operator = (const String & s);
char & operator [] (int index);
int size();
String reverse();
int indexOf(char c);
int indexOf(String pattern);
bool operator == (String s);
bool operator != (String s);
bool operator > (String s);
bool operator < (String s);
bool operator >= (String s);
bool operator <= (String s);
String operator + (String s);
String operator += (String s);
void print(std::ostream & out);
void read(std::istream & in);
static int strLen(const String &s);
static String strCpy(const String &s, int length);
static String strDup(const String &s);
static bool strCmp(const String &s, const String &t);
~String();
private:
bool inBounds(int i)
{
return i >= 0 && i < len;
}
char * buf;
int len;
};
#endif
我一直在犯这样的错误:
>c:\users\omive_000\documents\visual studio 2013\projects\string\string\string.h(183): error C2084: function 'String String::operator =(const String &)' already has a body
1> c:\users\omive_000\documents\visual studio 2013\projects\string\string\string.h(11) : see previous definition of '='
有人能解释一下为什么会发生此错误吗?定义通常不属于头文件 您可以在include保护中内联声明和定义函数 您可以使用cpp文件
也就是说,您的代码看起来可疑。它不做它看起来做的事情。没有分配给这个或它的变量发生。但这是一个错误,而不是编译器错误。请注意,声明名为String的类会带来麻烦。也许您可以在这里找到答案:编译器错误似乎表明String.h中的行数比您在此处发布的要多。你能发布整个文件吗?你读到实际的错误信息了吗?它准确地说明了问题所在。@sop这不是一个模板。我想从编译器的角度来看,唯一的错误是include保护程序实际上并没有保护整个头文件,而只是保护声明。
>c:\users\omive_000\documents\visual studio 2013\projects\string\string\string.h(183): error C2084: function 'String String::operator =(const String &)' already has a body
1> c:\users\omive_000\documents\visual studio 2013\projects\string\string\string.h(11) : see previous definition of '='