2

The thing is I am keeping the main code in one file and calling functions from the other file. But it gives me LNK 2005 error every time I try to compile it. Can anyone please help me what I am doing wrong here? I am a newbie in C++ so please help me.

main file

#include "stdafx.h"
#include "forwards.cpp" // All the functions are forwarded in this file.

int main()
{
    using namespace std;
    cout << "Approximate Age Calculator till 31-12-2015" << endl;
    cout << endl;
    cout << "Type your birth year: ";
    cin >> yy;
    cout << "Type your birth month: ";
    cin >> mm;
    cout << "Type your day of birth: ";
    cin >> dd;
    cout << "Your approximate age is ";
    cout << yearCalculator() << " years, ";
    cout << monthCalculator() << " months and ";
    cout << daysCalculator() << " days" << endl;
    cout << endl;
}

forwards.cpp

#include "stdafx.h"
int dd;
int mm;
int yy;
int a;

int daysCalculator()
{
    int a = 31 - dd;
    return a;
}
int monthCalculator()
{
    int a = 12 - mm;
    return a;
}
int yearCalculator()
{
    int a = 2015 - yy;
    return a;
}
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
  • Do you have the file `stdafx.h` in the same directory as the other two files? – sameera sy Jan 11 '16 at 11:13
  • 4
    Please include the code in the question. Linking to images, will get you nothing but downvotes!! – Paul Rooney Jan 11 '16 at 11:13
  • The issue is that you have a cpp file included in another cpp file. But visual studio tries to build each source file in the project as a separate translation unit and then link it all together. So hence it compiles `forward.cpp` twice. Once as part of main and once by itself. This is the reason for the duplications in the error messages. Simplest fix, is probably to remove the #include. – Paul Rooney Jan 11 '16 at 11:23
  • What you should really do is create a `forward.h` that includes prototypes for the functions in forward.cpp at least and probably `extern` statements for the variables. – Paul Rooney Jan 11 '16 at 11:24
  • okay I should try that first. I will let you know what happens. Thanks. –  Jan 11 '16 at 11:29
  • Thank you Paul, It helped. The other file was getting compiled twice and that's why it was not working. But creating a header file worked. –  Jan 11 '16 at 11:37
  • Related: http://stackoverflow.com/a/10046514/1566267 – John_West Jan 11 '16 at 11:56

1 Answers1

2

The issue is that you have a cpp file included in another cpp file. But visual studio tries to build each source file in the project as a separate translation unit and then link it all together. So hence it compiles forwards.cpp twice. Once as part of main and once by itself. This is the reason for the duplications in the error messages. Simplest fix, is probably to remove the #include.

What you should really do is create a forwards.h that includes prototypes for the functions in forwards.cpp at least and probably extern statements for the variables.

Another thing you should maybe also consider is using a class to encapsulate the variables you have. It's not normally good form to export variables on their own from another file. I can give you an example of this.

#include <iostream>

using std::cout;
using std::cin;

// The class could be in another file
class AgeCalculator
{
    int year_;
    int month_;
    int day_;

public:
    AgeCalculator(int year, int month, int day)
        : year_(year), month_(month), day_(day)
    {}

    int GetYear() { return (2015 - year_); }
    int GetMonth() { return (12 - month_); }
    int GetDay() { return (31 - day_); }
};

int main()
{
    int y, m, d;

    cout << "enter year :";
    cin >> y;
    cout << "enter month :";
    cin >> m;
    cout << "enter day :";
    cin >> d;

    AgeCalculator ac(y, m, d);

    cout << "Your approximate age is " << 
        ac.GetYear() << " years " << 
        ac.GetMonth() << " months and " << 
        ac.GetDay() << " days.\n";
}
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61