-1

Here is my declaration in question, I even use include guards: Edit: I'm including the entire header if this will help answer any additional questions one might have.

#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

And here is my definition:(starting form line 183)

String String::operator = (const String & s)
{
    String t(s);
    return t;
}

And I keep getting this error:

>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 '='

can anyone offer me an explanation as to why this error occurs?

leppie
  • 115,091
  • 17
  • 196
  • 297
VanyaS
  • 33
  • 9

1 Answers1

4

Definitions normally don't belong into header files.

  • You can declare and define your function inline, inside your include guards
  • You can use a cpp file

That said, your code looks fishy. It does not do what it seems to do. There is no assignment to this or it's variables happening. But that's a bug and not a compiler error.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • I guess the only thing that's wrong from the compilers perspective is the include guards not actually guarding the *whole* header file, but just the declaration. – nvoigt Nov 05 '14 at 08:41