Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

127
Vistas
Random problems with definition

I am trying to compile my code. The compiler is giving random errors about 'multiple definition':

/tmp/cctnjqVc.o:(.bss+0x0): multiple definition of 'firstname'
/tmp/ccPgFuZw.o:(.bss+0x0): first defined here
/tmp/cctnjqVc.o:(.bss+0x200): multiple definition of 'resultName[abi:cxx11]'
/tmp/ccPgFuZw.o:(.bss+0x200): first defined here

I can assure you that there is no multiple definition at all in my code. My code is in multiple files so if you want to see them all:

http://www.github.com/calmunicorn/virtualsociety

but there are two files that I think are concerned:

FileStream.h

#ifndef FILESTREAM_H
#define FILESTREAM_H
#include <fstream>
#include <string>
#include <limits>
using namespace std;
static fstream logFile;
ofstream firstname;
void log(string argument);
string firstName_read(bool boyOrGirl);
string resultName;

#endif

FileStream.cpp

#include "FileStream.h"
void log(string argument)
{
    logFile.open ("log.txt", fstream::out | fstream::app);

    logFile << argument;

    logFile.close();
}

string firstName_read (bool boyOrGirl)
{

    if (boyOrGirl == true)
         {
              firstname.open("Name/FirstName_Male.txt", fstream::in);
              firstname.close();
              return resultName;
         }
    else
         {
              firstname.open("Name/FirstName_Female.txt", fstream::in);
              firstname.close();
              return resultName;
         }
}

I'm on Arch Linux if makes any difference.


edit :

Thank you for all the answer i did everything i was told to and now it work without any problems!

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You did violate ODR by defining (as opposed to just declaring) firstname and resultName in the header. What you need to do, to declare them, is to list them as extern in the header file:

extern ofstream firstname;
extern string resultName;

and have the definition (without extern) in the .cpp file.

BTW, headers should not use using namespace. Explicitly qualify everything in your header, instead.

over 4 years ago · Santiago Trujillo Denunciar

0

This line in FileStream.h

ofstream firstname;

will define a variable firstname in each .cpp file that includes the .h.

You probably want to change that to a declaration in the .h

extern ofstream firstname;

then define it in just one of the .cpp files

ofstream firstname;
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda