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:
An attempt to #include a .cpp file or
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
catthe files to check they really have the content you think. Create a new directory, copy only those files above there and try to compile them again...-o squarefirst, but that shouldn't change your outcome as-is. This code works and builds as expected on my rig, so you likely have stale content somewhere from before you factored out theSquarecode and header.g++ test1.cpp square.cpp -o square && make #Makefile2works for me.