[Przeładowanie operatorów] operator <<

0

witam,
przy przeładowaniu operatora << dostaje takie błędy :

\mymath\include\vector.h(62): error C2143: syntax error : missing ';' before '&'
\mymath\include\vector.h(62): error C2433: 'ostream' : 'friend' not permitted on data declarations
\mymath\include\vector.h(62): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
\mymath\include\vector.h(62): error C2061: syntax error : identifier 'ostream'
\mymath\include\vector.h(62): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
\mymath\include\vector.h(62): error C2805: binary 'operator <<' has too few parameters
\mymath\mymath\vector.cpp(121): warning C4309: 'initializing' : truncation of constant value
\mymath\mymath\vector.cpp(162): warning C4309: '=' : truncation of constant value
\mymath\mymath\vector.cpp(212): error C2143: syntax error : missing ';' before '&'
\mymath\mymath\vector.cpp(212): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
\mymath\mymath\vector.cpp(212): error C2086: 'int ostream' : redefinition
\mymath\include\vector.h(62) : see declaration of 'ostream'
\mymath\mymath\vector.cpp(212): error C2065: 'out' : undeclared identifier
\mymath\mymath\vector.cpp(212): error C2059: syntax error : 'const'
\mymath\mymath\vector.cpp(213): error C2143: syntax error : missing ';' before '{'
\mymath\mymath\vector.cpp(213): error C2447: '{' : missing function header (old-style formal list?)
\mymath\mymath\vector.cpp(300): error C2065: 'cout' : undeclared identifier
\mymath\mymath\vector.cpp(305): error C2065: 'cout' : undeclared identifier
\mymath\mymath\vector.cpp(307): error C2065: 'cout' : undeclared identifier
\mymath\mymath\vector.cpp(336): warning C4309: 'initializing' : truncation of constant value

a kod programu :

ostream & operator<<(ostream& out, const Vector& v)
{
	out << "[" << v.x << "," << v.y << "," << v.z << "]" ;
	return out;
}

ktoś może pomóc ?

0

A masz użyte

using namespace std;

?

0

tak

0

To podaj więcej kodu, bo problem raczej nie jest z samym operatorem

0
#ifndef VECTOR_H
#define VECTOR_H

#include "point.h"

namespace MyOGLProg2
{

	const double M_PI = 3.14159265358979323846;
	const double M_HALF_PI = 1.57079632679489661923;

	const double RAD = (M_PI/180.0);
	const double PIOVER2 = (3.14159265358979323846 / 2);
	#define DEG2RAD(x) ((x)*M_PI/180.0)
	#define RAD2DEG(x) ((x)*180.0/M_PI)
}

class Vector
{
public:
		Vector(void);
		Vector(float x, float y, float z);
		Vector(Position& start, Position& end);
		~Vector(void){;}
		
		float getMagnitude(void) const;
		void setMagnitude(const float m);

		Vector setX(float newX);				//set parameters to specific value
		Vector setY(float newY);
		Vector setZ(float newZ);

		float getAngleBetween2(const Vector& second);

		bool checkDirection(const Vector& other); // check if vectors point the same direction
		
		void normalise(void);		

		Vector addTo(const Vector &other) const;			//adding and substraction with use of functions
		Vector addToAndDraw(const Vector &other, bool showSteps, float startX, float startY) const;	
		Vector subtractFrom(const Vector &other) const;

		Vector operator+ (const Vector&);					//and operators
		Vector operator- (const Vector&);
		Vector operator* (const float &x);					// scaling the vector
		Vector crossProduct(const Vector& other);
		float getDotProduct(const Vector &other) const;
		float getDotProduct(float x, float y, float z) const;

		friend ostream &operator<<(ostream & out, const Vector& v);

		static Vector vectorFromPoints(Point& start, Point& end);
		static Vector vectorFromPoints(Point& start, float endx, float endy, float endz);
		static Vector vectorFromPoints(float startx, float starty, float startz, Point& end);
		static Vector vectorFromPoints(float startx, float starty, float startz, float endx, float endy, float endz);

private:
		float x;
		float y;
		float z;
};
#endif
#include "vector.h"
#include "math.h"

#include <iostream>

#include <math.h>
#include "windows.h"

#include <glut.h>             // OpenGL utilties

using namespace MyOGLProg2;
using namespace std;

Vector::Vector(void)
{
    this->x = this->y = this->z = 0.0;
}

Vector::Vector(float x, float y, float z)
 {
    this->x = x; this->y = y; this->z = z;
}

Vector::Vector(Position& start, Position& end)
{
    this->x = end.x - start.x;
    this->y = end.y - start.y;
    this->z = end.z - start.z;
 }

Vector Vector::addToAndDraw(const Vector &other, bool showSteps, float startX, float startY) const
{
    Vector result;
    //your code here -----------------
    result.x = this->x + other.x;
    result.y = this->y + other.y;
    result.z = this->z + other.z;

	this->drawArrow(startX,startY,0,0);
	other.drawArrow(this->x,this->y,0,0);

	glColor3f(1,1,0);
	result.drawArrow(0,0,0,1,3);
     return result;
}

Vector Vector::addTo(const Vector &other) const
{
    Vector result;
    //your code here -----------------
    result.x = this->x + other.x;
    result.y = this->x + other.y;
    result.z = this->x + other.z;
     return result;
}

Vector Vector::subtractFrom(const Vector &other) const
{
    Vector result;
    result.x = other.x - this->x;
    result.y = other.y - this->y;
    result.z = other.z - this->z;
     return result;
}
        
float Vector::getMagnitude(void) const
{
    float result = 0;
    //your code here -----------------------
    result = sqrt( (this->x * this->x) + (this->y * this->y) + (this->z * this->z) );
     return result;
}

void Vector::setMagnitude(const float m)
{
    //your code here ------------------------
    this->x *= m / this->getMagnitude();
    this->y *= m / this->getMagnitude(); 
     this->z *= m / this->getMagnitude(); 
}
    
float Vector::getDotProduct(const Vector &other) const
{
    float result;
    //your code here -------------------------
    result  = (this->x * other.x) + (this->y * other.y) + (this->z * other.z);
     return result;
}

float Vector::getDotProduct(float x, float y, float z) const
{
    float result;
    //your code here -------------------------
    result  = (this->x * x) + (this->y * y) + (this->z * z);
     return result;
}

void Vector::normalise(void)
{
    //your code here
    this->x /= this->getMagnitude();
    this->y /= this->getMagnitude();
    this->z /= this->getMagnitude();

}


Vector Vector::operator+ (const Vector& other)
{
	this->x += other.x;
	this->y += other.y;
	this->z += other.z;
	return *this;
}

Vector Vector::operator- (const Vector& other)
{
	this->x -= other.x;
	this->y -= other.y;
	this->z -= other.z;
	return *this;
}

Vector Vector::operator* (const float &x)
{
	this->x *= x;
	this->y *= x;
	this->z *= x;
	return *this;
}

ostream & operator<<(ostream& out, const Vector& v)
{
	out << "[" << v.x << "," << v.y << "," << v.z << "]" ;
	return out;
}

Vector Vector::crossProduct(const Vector& other)
{
	return Vector(this->y*other.z - this->z*other.y,this->z*other.x-this->x*other.z,this->x*other.y-this->y*other.x);
}

Vector Vector::setX(float newX)
{
	this->x = newX;
	return *this;
}

Vector Vector::setY(float newY)
{
	this->y = newY;
	return *this;
}

Vector Vector::setZ(float newZ)
{
	this->z = newZ;
	return *this;
}

float Vector::getAngleBetween2(const Vector& second)
{
	float alpha = RAD2DEG(acos( this->getDotProduct(second) / ( this->getMagnitude()*second.getMagnitude() )));
	return alpha;
}

bool Vector::checkDirection(const Vector& other)
{
	float firstDirection = 0, secondDirection = 0;
	if ( this->getDotProduct(1,0,0) >= 0 )
		firstDirection = 1;

	if (  other.getDotProduct(1,0,0) >= 0 )
		secondDirection = 1;

	if( firstDirection == secondDirection )
		return 1;
	else
		return 0;
}


Vector Vector::vectorFromPoints(Point& start, Point& end)
{
	Vector g(end.getX()-start.getX(),end.getY()-start.getY(),end.getZ()-start.getZ() );
	return g;
}

Vector Vector::vectorFromPoints(Point& start, float endx, float endy, float endz)
{
	Vector g(endx-start.getX(),endy-start.getY(),endz-start.getZ() );
	return g;
}

Vector Vector::vectorFromPoints(float startx, float starty, float startz, Point& end)
{
	Vector g(end.getX()-startx,end.getY()-starty,end.getZ()-startz );
	return g;
}

Vector Vector::vectorFromPoints(float startx, float starty, float startz, float endx, float endy, float endz)
{
	Vector g(endx-startx,endy-starty,endz-startz );
	return g;
}

błędy zaczęły dopiero wyskakiwać jak dodałem operator << przed tym wszystko było OK.

0

Dołącz pliki nagłówkowe (szczególnie iostream) i using namespace std na górę vector.h, a nie vector.cpp. Po prostu przy definicji klasy jeszcze nie wie co to jest ostream, a dalej się reszta krzaczy (od momentu deklaracji przyjaźni).

0

dzięki wielkie wszystko działa :)

0

jeszcze jedno pytanie mi się od razu nasuwa: mam klasę point i klase vector(która dziedziczy po poincie) a chcialbym zrobić funkcję w klasie point która zwracała by vector jak to można zrobić ?

0

Powinna wystarczyć deklaracja zapowiadająca klasę vector, ale jakoś dziwnie to tak wygląda, żeby klasa bazowa miała informacje o klasie pochodnej. Wektor też raczej nie jest specjalnym przypadkiem punktu.

1 użytkowników online, w tym zalogowanych: 0, gości: 1