Question

So I'm in a basic programming class, and we're learning how to link files together. The problem is I've run into an error that no one seems able to fix. I've already been to my professor, student assistants, and the programming assistance lab on campus, no luck.

I've also searched through at least 10 different posts here relating to multiple definition errors, but in every case it's either:

  1. An attempt to #include a .cpp file or

  2. Function definitions are inside the header file.

Neither of these cases apply here, as you can see (I have removed all comments to make the code easier to read):

Header file: square.h:

#ifndef SQUARE_H
#define SQUARE_H

class Square
{
    private:
            float side;
    public:
            void setSide(float);
            float findArea();
            float findPerimeter();
};

#endif

Function definitions file: square.cpp

#include "square.h"
#include <iostream>
#include <cstdlib>

using namespace std;

void Square::setSide(float length)
{
  side = length;
}

float Square::findArea()
{
  return side * side;
}

float Square::findPerimeter()
{
  return 4 * side;
}

Program file: test1.cpp

#include "square.h"
#include <iostream>

using namespace std;


int main()
{
    Square  box;
    float   size;

        cout << "\n\nHow long is a side of this square?: ";
        cin >> size;

        box.setSide(size);

        cout << "\n\nThe area of the square is " << box.findArea();

        cout << "\n\nThe perimeter of the square is " << box.findPerimeter();

        cout << endl << endl;

        return 0;
}

And finally the error whenever I try to compile:

/tmp/ccVaTqTo.o: In function `Square::setSide(float)':
square.cpp:(.text+0x0): multiple definition of `Square::setSide(float)'
/tmp/cc4vruNq.o:test1.cpp:(.text+0x0): first defined here
/tmp/ccVaTqTo.o: In function `Square::findArea()':
square.cpp:(.text+0xe): multiple definition of `Square::findArea()'
/tmp/cc4vruNq.o:test1.cpp:(.text+0xe): first defined here
/tmp/ccVaTqTo.o: In function `Square::findPerimeter()':
square.cpp:(.text+0x20): multiple definition of `Square::findPerimeter()'
/tmp/cc4vruNq.o:test1.cpp:(.text+0x20): first defined here
collect2: ld returned 1 exit status

Compile command, just for reference g++ test1.cpp square.cpp -o testfile

Using a makefile does not seem to make any difference, I just end up staring at the exact same compile error. I've tried using two different makefiles:

Makefile 1

square: test1.cpp square.cpp
        g++ test1.cpp square.cpp -o square

Makefile 2

square: test1.o square.o
        g++ test1.o square.o -o square

test1.o: test1.cpp
        g++ -c test1.cpp

square.o: square.cpp
        g++ -c square.cpp

Right now all I know is that this is some sort of linker error, but I have no idea what to do about it. Everyone I've asked has said my code is correct. But clearly SOMETHING is wrong.

Any help would be greatly appreciated. :D

Was it helpful?

Solution

Okay. Making a new, clean directory, and copy/pasting the code into new files (with the same filenames as the old files allowed it to finally compile. For some reason, I guess the compiler just didn't like my old files.

OTHER TIPS

I think maybe this can help

Try the makefile as follow:

square: test1.o square.o
        g++ test1.o square.o -o square

test1.o: test1.cpp square.h
        g++ -c test1.cpp

square.o: square.cpp square.h
        g++ -c square.cpp

EDIT if it doesn't work try to run preprocess only:

g++ -E test1.cpp

and

g++ -E square.cpp

check the outputs to see if it took the correct files

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top