Conditional debugging

I am using MSVC 2010 express edition. I am trying out conditional compilation program. I have the following files

FILE 1
******
myfunctions.h
=============
#ifndef MYFUNCTIONS_H
#define MYFUNCTIONS_H
#include <iostream>
#include <conio.h>
using namespace std;

const float PI=3.14f;
inline float areaOfCircle(int);
const int someFunction12();

#endif


FILE 2
******
functions.cpp
=============
#include "myfunctions.h"
inline float areaOfCircle(int r)
{
float area = PI * r * r;
return area;
}

const int someFunction12()
{
int i= 20;
return i;
}

FILE 3
******
chap2.cpp
=========
#include "functions.cpp"

void main()
{
int radius;

cout << "Enter the radius of a circle : ";
cin >> radius;

float area = areaOfCircle(radius);
cout << "The area of a circle is :" << area;

int x = someFunction12();
x = 200;
cout << "X value is :" << x;
getch();
}

When I build the solution it gives the following linker error

functions.obj : error LNK2005: "int const __cdecl someFunction12(void)" (?someFunction12@@YA?BHXZ) already defined in chap2.obj
1>C:\Users\Usha\Desktop\Seed Infotech\Examples\C++language\Debug\Chapter2.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

CAN ANYONE HELP ME WITH THIS PROGRAM?

Thanks
Usha
Do not include source files such as "functions.cpp". functions.cpp should be a part of your project. chap2.cpp should include myfunctions.h.

There is no reason to include iostream or conio.h in myfunctions.h

main returns type int.
You should never include the line using namespace std; in a header file.
There is no reason for areaOfCircle to be qualified as inline unless it is defined in the header file.
Thanks for the reply.

I removed inclusion of "functions.cpp" from chap2.cpp and included the .h file. But still it was giving error. Then i removed the inline keyword from the .h and functions.cpp file and then it worked. Why is it so?

Thanks

Usha
But still it was giving error.

What error?

In functions.cpp, when you declared areaOfCircle as inline, that made it have a translation unit scope (only visible with functions.cpp). When you remove the inline, it became visible to other translation units at link time.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
Topic archived. No new replies allowed.