开发者

C++ Error: explicit qualification

开发者 https://www.devze.com 2022-12-19 04:19 出处:网络
What do these errors mean? Vector.cpp:13: error: ISO C++ forbids declaration of ‘Vector’ with no type

What do these errors mean?

Vector.cpp:13: error: ISO C++ forbids declaration of ‘Vector’ with no type
Vector.cpp:13: erro开发者_如何学运维r: explicit qualification in declaration of ‘void Vector::Vector(double, double, double)’

The C++ (Line 13 is the Vector::Vector( ...):

#include <iostream>
using namespace std;

namespace Vector
{
    Vector::Vector( double x, double y, double z)
    {
        a = x;
        b = y;
        c = z;
    }
/*
    double Vector::dot(const Vector &v) const
    {
        return (a*v.a)+(b*v.b)+(c*v.c);
    }
*/
   Vector Vector::operator+(const Vector &v) const
   {
       Vector v1( a + v.a, b + v.b, c + v.c );
       return v1;
   }

   Vector Vector::operator-(const Vector &v) const
   {
       Vector v1( a - v.a, b - v.b, c - v.c );
       return v1;
   }

   bool Vector::operator==(const Vector &v) const
   {
       if( (a == v.a) && (b == v.b) && (c == v.c) )
       {
           return true;
       }
       else
       {
           return false;
       }
   }

   Vector Vector::operator*(const Vector &v) const
   {
       Vector v1( b*v.c - c*v.b, c*v.a - a*v.c, a*v.b - b*v.a );
       return v1;
   }

   ostream& operator<<(ostream &out, const Vector &v)
   {
       out << "<" << v.a << ", " << v.b << ", " << v.c << ">";
       return out;
   }

   istream& operator>>(istream &in, Vector &v)
   {
       in >> v.a;
       in >> v.b;
       in >> v.c;
       return in;
   }
/*
    double length( Vector v )
    {
        return sqrt( (v.a*v.a)+(v.b*v.b)+(v.c*v.c) );
    }
*/
} // end namespace Vector

The header file:

#ifndef _VECTOR_H
#define _VECTOR_H

#include <cstdlib>
#include <iostream>

using namespace std;

namespace Vector
{


class Vector
{
private:
    double a;
    double b;
    double c;

public:
    Vector( double x=0.0, double y=0.0, double z=0.0);

    double dot(const Vector &v) const;
    Vector operator+(const Vector &v) const;
    Vector operator-(const Vector &v) const;
    bool operator==(const Vector &v) const;
    Vector operator*(const Vector &v) const;
    friend ostream& operator<<(ostream &out, const Vector &v);
    friend istream& operator>>(istream &in, Vector &v);


}; // end Vector class

    double length(Vector v);

} //end namespace Vector
#endif  /* _VECTOR_H */


Constructors have no return type, not even void. Just remove the void and you're fine.


Looks like the major problem is that your cpp files didn't include your header file.


Include the header file in the cpp file. Also, the code has some design issues. operator+ should return const Vector and the same case with operator-.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号