Error de enlace al agregar una matriz a la clase de funciones comunes

I have a common functions class. Basic stuff, just has functions several classes in my project can use. But i am trying to add an array of structs so that several classes can use them for some things. Ive removed this array of structs from another Class (Class 1) and added them to my CommonFunctions class. They are only going to be read.

But Im getting errors:

CommonFunctions.obj : error LNK2005: "struct pup_file * pups" (?pups@@3PAUpup_file@@A) already defined in Class1.obj error LNK2005: "struct pup_file * pups" (?pups@@3PAUpup_file@@A) already defined in Class1.obj

error fatal LNK1169: se encontraron uno o más símbolos definidos de forma múltiple

Then for every other class in the project (which uses the CommonFunctions class, so all of them really) I get

error LNK2005: "struct pup_file * pups" (?pups@@3PAUpup_file@@A) already defined in main.obj

Anyone help me with whats going on here?

preguntado el 02 de febrero de 12 a las 11:02

Seeing some code would really help. I suspect you are defining the functions inside the header and not using "inline". -

I cant really post the code online unfortunately. But yeah i am trying to define it in the header. I just tried making it inline and it says: 'inline' not permitted on data declarations -

Fixed. I had to make the array static. Cos CommonFunctions was being used in loads of files it was being defined several times with each include. Making it static just does it the once. (At least thats what I'm gathering) -

Sure, if you declare a static array in header you will get rid of the linker errors. But this is probably not what you want because that way every .cpp file that includes this header will have its own private instance of that array. -

2 Respuestas

It looks like you didn't declare the puntero pups (it's not an array) inside the class definition.
If you put it in the header but outside the class, you would get errors like those.

La solución es asegurarse de que pups se declara dentro del CommonFunctions definición de clase.

Respondido 02 Feb 12, 15:02

After reading your comment on making it a static you probably want to define it as an extern rather than static as static means that everything that includes that definition gets its OWN copy (ie the copy is not shared between all of the files that include it).

Extern means that you will need to define it in a c/cpp file somewhere but you must only define it once and then any compilation unit that includes that extern will use the same structure.

Of course you might actually want each compilation unit to have its own copy of the variable and in which case static is what you want.

Respondido 02 Feb 12, 15:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.