LNK 2005 "already defined in .obj" error during compilation

I am a beginner and practicing OOPS concepts on VS Code. I am trying to solve a simple problem where the program involves taking two complex numbers and printing out the sum, difference and product.

This is my header file

#pragma once

#include <string>

class Complex
{
private:
    float real1, img1, real2, img2;
public:
    Complex();
    Complex(float r1, float i1, float r2, float i2);
    std::string calc_sum();
    std::string calc_diff();
    float calc_prod();
};

Implementation file:

#include "Complex.h"
#include <iostream>
#include <string>
using namespace std;

Complex::Complex() : real1{ 0.0 }, img1{ 0.0 }, real2{ 0.0 }, img2{ 0.0 } {}

Complex::Complex(float r1,float img1,float r2, float img2): 
    real1{r1}, img1 {img1}, real2 {r2}, img2 {img2}{}

std::string Complex::calc_sum() {
    float real_sum = real1 + real2;
    float img_sum = img1 + img2;

    string r_sum, i_sum;
    r_sum = to_string(real_sum);
    i_sum = to_string(img_sum);

    string sum = r_sum + " + i(" + i_sum + ")";
    return sum;
}

std::string Complex::calc_diff() {
    float real_diff = real1 - real2;
    float img_diff = img1 - img2;

    string r_diff, i_diff;
    r_diff = to_string(real_diff);
    i_diff = to_string(img_diff);

    string diff = r_diff + " + i(" + i_diff + ")";
    return diff;
}

float Complex::calc_prod() {
    float real_prod = real1 * real2;
    float img_prod = img1 * img2;
    return (real_prod - img_prod);
}

Main.cpp

#include <iostream>
#include "Complex.cpp"
#include <string>
using namespace std;

int main() {

    float real_1,real_2, img_1, img_2, prod;
    string sum, diff;

    cout << "Gathering input for 1st complex number..." << endl;
    cout << "Enter real part: " << endl;
    cin >> real_1;
    cout << "Enter imaginary part: " << endl;
    cin >> img_1;
    cout << "Gathering input for 2nd complex number..." << endl;
    cout << "Enter real part: " << endl;
    cin >> real_2;
    cout << "Enter imaginary part: " << endl;
    cin >> img_2;

    Complex obj;
    Complex obj1(real_1, img_1, real_2, img_2);

    cout << "Sum Diff and Product are: " << endl;
    sum = obj.calc_sum();
    diff = obj.calc_diff();
    prod = obj.calc_prod();
    cout << "Sum: " << " " << "Diff: " << " " << "Product: " << endl;

    cout << "Sum, Diff, Prod of entered numbers are: " << endl;
    sum = obj1.calc_sum();
    diff = obj1.calc_diff();
    prod = obj1.calc_prod();
    cout << "Sum: " << " " << "Diff: " << " " << "Product: " << endl;

    return 0;   
}

These are the errors I get:

image

I saw a similar question, but the suggestion there was to remove the inclusion of the .cpp in the main.cpp file. I tried that, but I get compilation errors saying "Complex: is an undeclared identifier".

Could anyone tell me where I am going wrong?

>Solution :

Your main.cpp‘s contents are:

#include<iostream>
#include "Complex.cpp"
#include<string>

An #include in C++ is equivalent to physically inserting the contents of the included file into the including file.

Your main.cpp contains everything, every last bit of what’s in complex.cpp.

Everything in complex.cpp gets compiled and linked twice, once by itself, once when inserted into main.cpp.

This is a clear fat-finger. You obviously meant to #include "Complex.h", instead of Complex.cpp, in order to include only the class declaration instead of the entire kit-and-caboodle.

Leave a Reply