d2jsp
d2jsp Forums > Programmer's Haven > C/C++/C# > Redefinition Of Formal Parameters > C++
Add Reply New Topic New Poll
Darnderin
#1 Dec 7 2016 06:35pm
Group: Member
Posts: 1,801
Joined: Nov 5 2016
Gold: 0.00
Warn: 30%
Quote
#include <iostream>
#include <ctime> // for random
using namespace std;

#include "rect.h"

int random(int min, int max);

template<class T>
void _sort(T * a, int n);

// **** additional functions ****

const int ARR_SIZE = 10;

int main()
{
rect arr[ARR_SIZE]; // initialize this array of pointers

for (int i = 0; i < ARR_SIZE; i++)
{
arr[ I ].set(random(1, 200), random(1, 200));
}

cout << "Current array contents: " << endl;
for (int i = 0; i < ARR_SIZE; i++)
{
cout << "arr[" << i << "] area = " << arr[ I ].area() << endl;
}

_sort(arr, ARR_SIZE);

cout << "Sorted array contents: " << endl;

for (int i = 0; i < ARR_SIZE; i++)
{
cout << "arr[" << i << "] area = " << arr[ I ].area() << endl;
}

return 0;
}

int random(int min, int max)
{
static bool ft = true;

if (ft)
{
srand((unsigned int)time(0));
ft = false;
}

return (rand() % (max - min + 1)) + min;
}

template<class T>
void _sort(T * a, int n)
{
int rh, lh, i = 0;

for (lh = 0; lh < n; lh++)
{
if (a[lh] < a[rh])
{
lh = i;
}
swap(a[lh], a[rh]);
}
}
void swap(int a, int b)
{
int temp = a;
int a = b;
int b = temp;
}


header file

Quote
#ifndef _RECT_H_
#define _RECT_H_

class rect {
private:
double _height;
double _width;

public:

rect() : _height(0), _width(0)
{
}

rect(int width, int height) : _height(height), _width(width)
{
}

void set(double width, double height)
{
_height = height;
_width = width;
}

double height() const
{
return _height;
}

double width() const
{
return _width;
}

double area() const
{
return _height * _width;
}

// ***** additional functions *****
};


#endif


error c2082 redefinition of formal paramater a/b

any help appreciated

This post was edited by Darnderin on Dec 7 2016 06:38pm
garrettthegreat
#2 Dec 7 2016 06:53pm
Group: Member
Posts: 735
Joined: Jan 23 2007
Gold: 725.94
Code
void swap(int a, int b)
{
int temp = a;
int a = b;
int b = temp;
}


Should be

Code
void swap(int a, int b)
{
int temp = a;
a = b;
b = temp;
}


Your current code declares a new int named "a", when a parameter named "a" already exists. The same with "b".
garrettthegreat
#3 Dec 7 2016 08:32pm
Group: Member
Posts: 735
Joined: Jan 23 2007
Gold: 725.94
Also, your parameters for swap() should also be passed by reference. Passing the value of a and b will leave a and b unchanged.

This post was edited by garrettthegreat on Dec 7 2016 08:32pm
Go Back To C/C++/C# Topic List
Add Reply New Topic New Poll