buenas señores necesito de su ayuda ,tengo este problema tengo dos ficheros de un main uno .CPP y otro .H en el .H esta declarada una clase en C++ y en el .Cpp esta definida la clase creada en .H ahora como hago para poder linkear esas dos al main.Cpp gracias de antemano
Mira, te pongo un ejemplo, sacado del libro de Deitel, un poco de la clase "complejo" para el manejo de números complejos:
main.cpp:
#include <iostream>
using std::cout;
using std::endl;
#include "complejo.h"
int main(void)
{
Complejo x;
Complejo y(4.3, 8.2);
Complejo z(3.3, 1.1);
cout << "x : " << endl;
x.imprime();
cout << "y : " << endl;
y.imprime();
cout << "z : " << endl;
z.imprime();
x = y + z;
cout << " x = y + z : " << endl;
x.imprime();
x = y - z;
cout << "x = y - z : " << endl;
cout << x;
if(x == y)
cout << "Iguales" << endl;
else
cout << "Diferentes" << endl;
Complejo a;
Complejo b;
if(a != b)
{
cout << "Distintos" << endl;
} else {
cout << "Iguales" << endl;
}
return 0;
}
complejo.h
// complejo.h
#ifndef COMPLEJO_H
#define COMPLEJO_H
#include <iostream>
using std::ostream;
class Complejo
{
// Interfaz pública:
public:
friend ostream &operator<<(ostream &, const Complejo &);
friend ostream &operator>>(ostream &, const Complejo &);
Complejo(double = 0.0, double = 0.0);
Complejo operator+(const Complejo &) const;
Complejo operator-(const Complejo &) const;
bool operator==(const Complejo&) const;
bool operator!=(const Complejo&) const;
void imprime(void) const;
private:
double real; // Parte real
double imaginaria; // Parte imaginaria.
};
#endif
complejo.cpp:
// complejo.cpp
#include <iostream>
using std::cout;
#include "complejo.h"
Complejo::Complejo(double parteReal, double parteImaginaria) : real(parteReal), imaginaria(parteImaginaria)
{
// Parte vacía.
}
// Operador sobrecargado +
Complejo Complejo::operator+(const Complejo &operando2) const
{
return Complejo(real + operando2.real, imaginaria + operando2.imaginaria);
}
// Operador sobrecargado -
Complejo Complejo::operator-(const Complejo &operando2) const
{
return Complejo(real - operando2.real, imaginaria - operando2.imaginaria);
}
void Complejo::imprime(void) const
{
cout << '(' << real << ", " << imaginaria << "i)" << std::endl;
}
ostream &operator<<(ostream& salida, const Complejo& complejo)
{
salida << '(' << complejo.real << ", " << complejo.imaginaria << "i)" << std::endl;
}
// Operadores sobrecargados
bool Complejo::operator==(const Complejo &complejo) const
{
return (*this).real == complejo.real && (*this).imaginaria == complejo.imaginaria;
}
bool Complejo::operator!=(const Complejo &complejo) const
{
return !((*this).real == complejo.real && (*this).imaginaria == complejo.imaginaria);
}
Como ves, es sencillo.
ese ejemplo si lo tengo men tmb el libro de deitel :D mira me sale este error
C:\Proyectos DEV C++\pang\Bonus.cpp no match for 'operator+' in '((Bonus*)this)->Bonus::posicion + Vector2D::operator*(float)(t)'
que significa este error ............................... en el codigo que estoy ahciendo
Necesitas poner los códigos fuente.
// Bonus.cpp: implementation of the Bonus class.
//
//////////////////////////////////////////////////////////////////////
#include "Bonus.h"
#include <GL/glut.h>
#include <stdlib.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Bonus::Bonus()
{
//movimiento gravitatorio
//velocidad vertical inicial
aceleracion.y=-9.8;
velocidad.y=5;
lado=0.5f;
}
Bonus::~Bonus()
{
}
void Bonus::Dibuja()
{
glPushMatrix();
glTranslatef(posicion.x,posicion.y,0);
glRotatef(30,1,1,1);
glColor3f( rand()/(float)RAND_MAX,
rand()/(float)RAND_MAX,
rand()/(float)RAND_MAX);//color aleatorio
glutSolidCube(lado);
glPopMatrix();
}
void Bonus::Mueve(float t)
{
posicion=posicion + velocidad*t + aceleracion*(0.5f*t*t);
velocidad=velocidad+aceleracion*t;
}
void Bonus::SetPos(float ix, float iy)
{
posicion.x=ix;
posicion.y=iy;
}
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "Vector2D.h" // Added by ClassView
class Bonus
{
public:
void SetPos(float ix,float iy);
Bonus();
virtual ~Bonus();
void Mueve(float t);
void Dibuja();
private:
float lado;
Vector2D posicion;
Vector2D velocidad;
Vector2D aceleracion;
};
#endif // !defined(AFX_BONUS_H__0EE7EF41_0205_4320_8F97_4AFEB50418B8__INCLUDED_)
// Vector2D.cpp: implementation of the Vector2D class.
//
//////////////////////////////////////////////////////////////////////
#include "Vector2D.h"
#include <math.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Vector2D::Vector2D(float xv,float yv)
{
x=xv;
y=yv;
}
Vector2D::~Vector2D()
{
}
float Vector2D::modulo()
{
return (float)sqrt(x*x+y*y);
}
float Vector2D::argumento()
{
return (float)atan2(y,x);
}
Vector2D Vector2D::operator - (Vector2D &v)
{
Vector2D res;
res.x=x-v.x;
res.y=y-v.y;
return res;
}
Vector2D Vector2D::operator + (Vector2D &v)
{
Vector2D res;
res.x=x+v.x;
res.y=y+v.y;
return res;
}
float Vector2D::operator *(Vector2D &v)
{
return x*v.x+y*v.y;
}
Vector2D Vector2D::operator *(float f)
{
Vector2D res;
res.x=x*f;
res.y=y*f;
return res;
}
Vector2D Vector2D::Unitario()
{
Vector2D retorno(x,y);
float mod=modulo();
if(mod>0.00001)
{
retorno.x/=mod;
retorno.y/=mod;
}
return retorno;
}
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class Vector2D
{
public: //atributos
float x;
float y;
public: //métodos
Vector2D(float xv=0.0f,float yv=0.0f);
virtual ~Vector2D();
float modulo(); //modulo del vector
float argumento(); //argumento del vector
Vector2D Unitario(); //devuelve un vector unitario
Vector2D operator - (Vector2D &);//resta de vectores
Vector2D operator + (Vector2D &);//suma de vectores
float operator *(Vector2D &); //producto escalar
Vector2D operator *(float); //producto por un escalar
};
#endif // !defined(AFX_VECTOR2D_H__86ED85F9_9ED7_4672_9273_C1BB38271909__INCLUDED_)
el error me sale aca void Bonus::Mueve(float t)
Leo no te puedo pasar los archivos a tu corre a ver si me das una mano o por aka numas tu diras .................