Absolute Value Template - I already tried something but it didn't work.

Write a function template that accepts an argument and returns its absolute value.
The absolute value of a number is its value with no sign. For example, the absolute
value of -5 is 5, and the absolute value of 2 is 2. Test the template in a simple driver
program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
using namespace std;

template < class AV >
AV aValue(AV a)
{
	return (a > 0 ? a : a * -1);
}

int main()
{
	int x,y;
	cout << "this program returns the absolute value.\n"
	<< "****************************************************\n\n";
	cout << "enter a positive number : ";
	cin >> x;
	cout << "enter a negative number : ";
	cin >> y;
	cout << "absolute value of " << x << "is : ";
	cout << aValue(x) << endl;
	cout << "absolute value of " << y << "is : ";
	cout << aValue(y) << endl;
	

	cin.get();
	cin.get();
	return 0;
}


error:

1>------ Build started: Project: DataStructure, Configuration: Debug Win32 ------
1> Source1.cpp
1>Source1.obj : error LNK2005: _main already defined in Source.obj
1>C:\Users\Christian\Documents\DATA STRUCTURE\Excersices\Debug\DataStructure.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited on
There's actually nothing wrong with your source code (except you forgot to put a space at x << "is : ").

You need to change the "Project type"/"Application type" to Command Line Application.
https://msdn.microsoft.com/en-us/library/vstudio/s3awkzkh(v=vs.100).aspx

Whenever you start a new project, make sure to choose the correct Project type.

Hope this helps.
Thanks Duoas
Topic archived. No new replies allowed.