Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Multiple Definitions Error, Please Help.

0 votes
asked Mar 5 by AliciaYPhoenix (120 points)

I am starting work on a little passion project with the hopes of having a high tech pokemon dnd campaign in the future (no idea when, just kind of working it out). I haven't programmed in a long time and this is giving me a bug I can't quite comprehend for some reason. So, here's my current code.

main.cpp ->

#include <stdio.h>
#include <math.h>
#include "calculations.c"

int main()
{
    int speedAtt = 45;
    int speedDef = 63;
    int accuracy = 100;
    int roll = findroll(speedAtt, speedDef, accuracy);
    printf("Attacker needs to roll a %d\n", roll);
    

    return 0;
}

calculations.c ->

#include <stdio.h>

#include <math.h>

#include "calculations.h"

int findroll (int speedA, int speedD, int acc)

{

  int roll;

  float hitperc = ((speedA / speedD) * acc) / 100;

  if (hitperc >= 1)

roll = 2;

  else

roll = round (20 - (hitperc * 20));

  return roll;

}

int dodamage (int attk, int def, int power, int atroll)

{

  return 0;

}

calculations.h ->

#ifndef CALCULATIONS_H_
#define CALCULATIONS_H_


int findroll (int, int, int);
int dodamage (int, int, int, int);

#endif

Then I get this series of errors:

/usr/bin/ld: /tmp/cc3PaZfu.o: in function `std::round(float)':
/home/calculations.c:7: multiple definition of `findroll(int, int, int)'; /tmp/ccFhCWEu.o:/home/calculations.c:7: first defined here
/usr/bin/ld: /tmp/cc3PaZfu.o: in function `dodamage(int, int, int, int)':
/home/calculations.c:19: multiple definition of `dodamage(int, int, int, int)'; /tmp/ccFhCWEu.o:/home/calculations.c:19: first defined here
collect2: error: ld returned 1 exit status
I can't find where the multiple definitions are and it is driving me up a wall. The calculations worked fine before I put it in it's own page.

1 Answer

0 votes
answered Mar 7 by Peter Minarik (88,130 points)

In main.cpp you do this:

#include "calculations.c"

You shouldn't include source files, but rather the header files:

#include "calculations.h"

Good luck with your project!

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...