Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda