-1

I have the code:

// class declaration
class Vector3D;

// class declaration and definition
class Point3D { 
    // ...

    // function declaration (only needs class declarations)
    Point3D operator+(const Vector3D &);
};

// class definition
class Vector3D {
    // ...
};

// function definition (needs class definitions)
inline Point3D Point3D::operator+(const Vector3D &vector) {
    // ...
}

But I get errror: 'Graphic::Point3D::operator +' : redefinition; different type modifiers

PolGraphic
  • 3,233
  • 11
  • 51
  • 108
  • 3
    Remove inline from function definition – Blood Jul 02 '12 at 16:23
  • `operator+` should really be a const method.# – Puppy Jul 02 '12 at 16:25
  • What compiler are you using? For what line does the compiler emit the error? Have you perhaps defined the function twice, by mistake? – James McNellis Jul 02 '12 at 16:50
  • @PolGraphic: I suspect that you posted code that is considerably different from what you are trying to compile. The error message suggests that most likely there's an issue with `const` placement in your actual code, which is not reflected in the snippet you posted. `inline` is not an issue here. – AnT stands with Russia Jul 02 '12 at 17:00
  • @DeadMG: Sorry, my bad. I gave this code as the answer to [another question](http://stackoverflow.com/a/11294328/204847), and forgot the `const`. – Mike Seymour Jul 02 '12 at 18:12

3 Answers3

3

The code in your question is well-formed. The Visual C++ 2012 Release Candidate accepts the code without error (I mention this because the text of your error is identical to that of Visual C++ error C2373).

Either your compiler has a bug, or the code you present in your question is not the same as the code you are compiling.


In any case: an operator+ does not need to be a member function. It would be simpler to use a nonmember function (or two, to handle different operand orderings):

Point3D operator+(Point3D  const& lhs, Vector3D const& rhs);
Point3D operator+(Vector3D const& lhs, Point3D  const& rhs);

If you do keep your operator+ member function(s), they should be const-qualified so that they can be called with a const-qualified left-hand argument:

Point3D operator+(const Vector3D &) const;
                                    ^ const required
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Yes. That was that. Thank you a lot. – PolGraphic Jul 02 '12 at 16:24
  • Is this something new/resolved in C++11? I remember discussions about this taking place for many years already and, if I remember correctly, the consensus has always been that specifying `inline` in the declaration is not necessary, i.e. the original version of the code is legal. And since when did `inline` become a *type modifier* (referring to the error message)? – AnT stands with Russia Jul 02 '12 at 16:36
  • @AndreyT: Actually, you are correct. C++11 says: "An inline member function (whether static or non-static) may also be defined outside of its class definition provided either its declaration in the class definition or its definition outside of the class definition declares the function as inline" (§9.3/3). Operative word: _either_. `inline` is a _function specifier_. C++ does not use the term _modifier_ for any grammatical element. – James McNellis Jul 02 '12 at 16:41
  • @James McNellis: Eaxctly, I just found it myself. The original OP's code is perfectly correct with regard to `inline` placement at least. If that's the real reason for the problem, then the compiler must be broken. – AnT stands with Russia Jul 02 '12 at 16:42
  • @AndreyT: That would indeed be shocking: a C++ compiler with bugs! ;-) I'm researching the issue; will report back. – James McNellis Jul 02 '12 at 16:44
  • @James McNellis: I find it surprising (or even "shocking") simply because this `inline` matter has been a completely mundane non-issue for decades already. And suddenly some compiler decided to give it enough attention to even develop a bug?! How can something like that happen? – AnT stands with Russia Jul 02 '12 at 16:49
1

The code you posted has no problems whatsoever (assuming that inline function definition is placed in the correct file, and aside from the missing return statement at the and of operator+ and possible design issues). It compiles without any errors of the reported nature on several compilers.

If your compiler has issues with the fact that the function is not declared inline in the class, it is a bug in your compiler. In C++ language it is completely up to you to decide where you want to place that inline: in the class, in the function definition or in both. All are legal and all have the same effect.

The fact that the compiler seems to refer to inline as "type modifier" is another telltale sign that there's something wrong with it. inline is not a type modifier.

However, it's even more likely that the compiler is perfectly OK and the error message is actually caused by some other error: an incorrectly placed const specifier, for example, which is either not shown or misrepresented in the code snipped you posted.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

Try this:

// class declaration
class Vector3D;

// class declaration and definition
class Point3D { 
    // ...

    // function declaration (only needs class declarations)
    inline Point3D operator+(const Vector3D &);
};

// class definition
class Vector3D {
    // ...
};

// function definition (needs class definitions)
inline Point3D Point3D::operator+(const Vector3D &vector) {
    // ...
}
Rontogiannis Aristofanis
  • 8,883
  • 8
  • 41
  • 58
  • 1
    The `inline` specifier can go on any declaration, including the function definition (which is also a declaration). And I hope no compiler would say "type modifier" when it's talking about a "function specifier". – Mike Seymour Jul 02 '12 at 18:19