Estoy creando un juego, necesito un struct que almacene el id del objeto con sus caracteristicas. he creado un header y accediendo solo desde el main funciona bien pero cuando intento acceder a el struct desde otro .cpp falla:
idobjeto.h
int ObjetosTotales = 0;
struct Objeto{
float x, y, z;
GLuint modelo;
float t_x, t_y, t_z;
int ang_x;
void Nuevo(GLuint vmodelo, float vx,float vy,float vz,int vang_x,float vt_x,float vt_y,float vt_z) {
modelo = vmodelo;
ang_x = vang_x;
x = vx; t_x = vt_x;
y = vy; t_y = vt_y;
z = vz; t_z = vt_z;
ObjetosTotales++;
}
}Objeto[5000];
Como hago para poder acceder a el desde cualquier .cpp? uso vc++2008
Gracias de antemano porfavor las respuestas lo mas detalladas posibles :)
Declara el arreglo en el otro cpp usando el modificador extern de la siguiente manera y no debería darte problemas.
extern struct Objecto Objeto[5000];
No se como usarlo, porfavor ayuda:
Main.cpp
#include "header.h"
#include "struct.h"
using namespace std;
int Comprobar();
int main(){
cout << "asdasd" << endl;
Objeto[ObjetosTotales+1].Nuevo(69,1,1,1,1,1,1,1);
getch();
Comprobar();
getch();
return 0;
}
Struct.h
#ifndef STRUCTOBJETO_H
#define STRUCTOBJETO_H
int ObjetosTotales = 0;
struct Objeto{
float x, y, z;
int modelo;
float t_x, t_y, t_z;
int ang_x;
void Nuevo(int vmodelo, float vx,float vy,float vz,int vang_x,float vt_x,float vt_y,float vt_z) {
modelo = vmodelo;
ang_x = vang_x;
x = vx; t_x = vt_x;
y = vy; t_y = vt_y;
z = vz; t_z = vt_z;
ObjetosTotales++;
}
};
extern Objeto Objeto[5000];
#endif
añadir.cpp
#include "header.h"
#include "struct.h"
using namespace std;
extern struct Objeto Objeto[5000];
int Comprobar(){
if(Objeto[ObjetosTotales].modelo == 69){
return 1;}
return 0;
}
Resultado:
Vinculando...
anadir.obj : error LNK2005: ya se definió "int ObjetosTotales" (?ObjetosTotales@@3HA) en main.obj
struct.obj : error LNK2005: ya se definió "int ObjetosTotales" (?ObjetosTotales@@3HA) en main.obj
main.obj : error LNK2001: símbolo externo "struct Objeto * Objeto" (?Objeto@@3PAU0@A) sin resolver
anadir.obj : error LNK2001: símbolo externo "struct Objeto * Objeto" (?Objeto@@3PAU0@A) sin resolver
Borrá esta línea
Citar
#include "header.h"
#include "struct.h"
using namespace std;
extern struct Objeto Objeto[5000];
int Comprobar(){
if(Objeto[ObjetosTotales].modelo == 69){
return 1;}
return 0;
}
sigue fallando. Msn: blackm4ster@gmail.com
Da el mismo error
dejá la anterior y borrá la otra. No se puede las 2
mm no te entiendo, de que archivo borro la linea extern struct Objeto Objeto[5000];
dahh le pusiste el mismo nombre que la clase? no se puede eso
Objeto objeto[5000];
ponelo así, ves la minúscula en el nombre?
tenés que declararlo en algún archivo .cpp y luego en algún archivo de inclusión .h lo ponés como extern.
Esto significa que cuando el objeto se use en un archivo.cpp se hace una referencia al objeto que fue declarado en otro archivo .cpp.
a ver sigue sin funcionar:
struct.h
#ifndef STRUCTOBJETO_H
#define STRUCTOBJETO_H
int ObjetosTotales = 0;
struct Objeto{
float x, y, z;
int modelo;
float t_x, t_y, t_z;
int ang_x;
void Nuevo(int vmodelo, float vx,float vy,float vz,int vang_x,float vt_x,float vt_y,float vt_z) {
modelo = vmodelo;
ang_x = vang_x;
x = vx; t_x = vt_x;
y = vy; t_y = vt_y;
z = vz; t_z = vt_z;
ObjetosTotales++;
}
};
extern Objeto objeto[5000];
#endif
main.cpp
#include "header.h"
#include "struct.h"
using namespace std;
int Comprobar();
int main(){
cout << "asdasd" << endl;
objeto[ObjetosTotales+1].Nuevo(69,1,1,1,1,1,1,1);
getch();
Comprobar();
getch();
return 0;
}
anadir.cpp
#include "header.h"
#include "struct.h"
using namespace std;
int Comprobar(){
if(objeto[ObjetosTotales].modelo == 69){
return 1;}
return 0;
}
Errores:
Vinculando...
anadir.obj : error LNK2005: ya se definió "int ObjetosTotales" (?ObjetosTotales@@3HA) en main.obj
struct.obj : error LNK2005: ya se definió "int ObjetosTotales" (?ObjetosTotales@@3HA) en main.obj
main.obj : error LNK2001: símbolo externo "struct Objeto * objeto" (?objeto@@3PAUObjeto@@A) sin resolver
anadir.obj : error LNK2001: símbolo externo "struct Objeto * objeto" (?objeto@@3PAUObjeto@@A) sin resolver
C:\Documents and Settings\isc\Mis documentos\Visual Studio 2008\Projects\Structs\Debug\Structs.exe : fatal error LNK1120: 1 externos sin resolver
Advertencia - mientras estabas escribiendo, fueron publicadas 3 respuestas. Probablemente desees revisar tu mensaje.
Ejemplo sencillo:
archivo1.cpp
#include <iostream>
struct Objeto {
int a,b,c,d;
} Objeto[5000];
int main() {
cout << "Hello world!" << endl;
return 0;
}
archivo2.cpp
extern struct Objeto {
int a,b,c,d;
} Objeto[5000];
void funcion() {
// ...
}
el objeto debe ser declarado antes de usar 'extern'.
declaralo en algún archivo .cpp que incluya el acceso a la clase o struct.
después podés usar así:
extern Objeto objeto[5000];
en otros archivos, para referenciar a un objeto existente en otro archivo.
pero en la declaración no se puede hacer esto:
Objeto Objeto[5000];
tiene el mismo nombre que la clase o struct, por eso ponelo así:
Objeto objeto[5000];
No consigo que funcione. Solo quiero tener un header con el struct y usar los datos desde cualquier .cpp
Con este code no da error de struct pero si de objetostotales
struct.h
#ifndef STRUCTOBJETO_H
#define STRUCTOBJETO_H
int ObjetosTotales = 0;
struct Objeto{
float x, y, z;
int modelo;
float t_x, t_y, t_z;
int ang_x;
void Nuevo(int vmodelo, float vx,float vy,float vz,int vang_x,float vt_x,float vt_y,float vt_z) {
modelo = vmodelo;
ang_x = vang_x;
x = vx; t_x = vt_x;
y = vy; t_y = vt_y;
z = vz; t_z = vt_z;
ObjetosTotales++;
}
};
extern Objeto objeto[5000];
#endif
main.cpp
#include "header.h"
#include "struct.h"
using namespace std;
struct Objeto objeto[5000];// Declaración, no va con extern!
int Comprobar();
int main(){
cout << "asdasd" << endl;
objeto[ObjetosTotales+1].Nuevo(69,1,1,1,1,1,1,1);
getch();
Comprobar();
getch();
return 0;
}
anadir.cpp
#include "header.h"
#include "struct.h"
using namespace std;
int Comprobar(){
if(objeto[ObjetosTotales].modelo == 69){
return 1;}
return 0;
}
Errores:
Vinculando...
anadir.obj : error LNK2005: ya se definió "int ObjetosTotales" (?ObjetosTotales@@3HA) en main.obj
struct.obj : error LNK2005: ya se definió "int ObjetosTotales" (?ObjetosTotales@@3HA) en main.obj
C:\Documents and Settings\isc\Mis documentos\Visual Studio 2008\Projects\Structs\Debug\Structs.exe : fatal error LNK1169: se encontraron uno o más símbolos definidos simultáneamente
claro el error era el mismo nombre de la clase o struct con el del registro o variable. Ahora deberías usar extern con esa variable que decís también.
struct.h
extern int ObjetosTotales;
main.cpp
int ObjetosTotales = 0;
como ves, la asignación de valor no se incluye cuando se usa 'extern'.
MUCHISIMAS GRACIAAAS YA LO HE CONSEGUIDO GRACIAS !!!!!
un link a el juego que estoy creando a ver que te parecec jaaja: https://www.dropbox.com/s/0m9mhmrgmhmt7an/Juego%20Isc.rar