C ++错误:显式限定

时间:2010-02-06 23:54:34

标签: c++

这些错误意味着什么?

Vector.cpp:13: error: ISO C++ forbids declaration of ‘Vector’ with no type
Vector.cpp:13: error: explicit qualification in declaration of ‘void Vector::Vector(double, double, double)’

C ++(第13行是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

标题文件:

#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 */

3 个答案:

答案 0 :(得分:4)

构造函数没有返回类型,甚至没有返回。只需删除void即可。

答案 1 :(得分:2)

看起来主要的问题是你的cpp文件没有包含你的头文件。

答案 2 :(得分:1)

在cpp文件中包含头文件。此外,代码有一些设计问题。 operator +应该返回const Vector和与operator相同的case - 。