質問

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

役に立ちましたか?

解決

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

他のヒント

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.

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) {
    // ...
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top