Hey, I keep getting this error, but from its description, I can't really tell how to fix it besides from what I've already tried, so any explanation would be great. The error I get is: clientrectlib.cpp(32) : error C2664: 'calcMinMaxValue' :cannot convert parameter 1 from 'overloaded-function' to 'float &'.

The code is the following: (This is clientrectlib.cpp)

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


int main() {
	cout << "Client to test the Rect library\n";

	cout << "Creating some variable of type Rect ...." << endl;
	RectStruct *rect01 = new RectStruct;
	RectStruct *rect02 = new RectStruct;
	RectStruct rect03;

	cout << "Assigning values to them ...." << endl;
	assignRectValues(rect01,10.51,20,40.77,50);
	assignRectValues(rect02,20.83,30,80.99,10);
	assignRectValues(&rect03,10.7,8.9,10,100);

	cout << "Printing out their fields .. " << endl;
	printRectValues(rect01);
	printRectValues(rect02);
	printRectValues(&rect03);

	cout << "Printing out their areas .. " << endl;
	cout << "Area of rect01: " << calcRectArea(rect01) << endl;
	cout << "Area of rect02: " << calcRectArea(rect02) << endl;
	cout << "Area of rect03: " << calcRectArea(&rect03) << endl;

	cout << "Comparing the area of rect01 and rect03... " << endl;
	cout << "The area of rect01 is " << calcRectArea(rect01) << endl;
	cout << "and area of rect03 is " << calcRectArea(&rect03) << endl;
	calcMinMaxValue(min, max);

	return 0;
}

and (This is Rect.cpp)

#include "Rect.h" 

void assignRectValues(RectStruct *r, float xcoord, float ycoord, float widthValue, float heightValue) {
	r->x = xcoord;
	r->y = ycoord;
	r->width = widthValue;
	r->height = heightValue;
	return;
}

void printRectValues(const RectStruct *r) {
	cout << "(x,y) = (" << r->x << ", " << r->y << ")" << endl;
	cout << "(width,height) = (" << r->width << ", " << r->height << ")" << endl;
	return;
}

float calcRectArea(const RectStruct *r){
	return r->height * r->width;
}

void calcMinMaxValue(float &min, float &max){
	RectStruct *rect01 = new RectStruct;
	RectStruct rect03;
	
	if(calcRectArea(rect01) < calcRectArea(&rect03)){
		min = calcRectArea(rect01);
		max = calcRectArea(&rect03);
	}
	else{
		min = calcRectArea(&rect03);
		max = calcRectArea(rect01);
	}

	cout << "The minimum value is " << min << endl;
	cout << "and the maximum value is " << max << endl;

	return;
}

Recommended Answers

All 5 Replies

First off you haven't give us Rect.h, and that is important.

However, guessing that Rect.h looks something like this

struct RectStruct
{
  float x;
  float y;
  float height;
  float width;
};

First off, I really like what you are trying to do with this code, you have almost all the main aspects of basic functions in this code. Sure, it is not correct, but once you are finished, you effectively have a single example, which covers are large number of techniques. Additionally, you have a lot of output, that is a really good debugging/understanding tool.

However, we have a few problems to discuss:

Firstly, you have decided to make RectStruct a simple structure, but then added a whole host of assignment and calculation functions. That is ok, but think about if a class with public methods and private data would have been better.

Second, calcMinMaxValue, is a truely horrible function. The reason is that you
allocate new memory to rect01, then don't delete it. You seem to be using rect01
and rect03 without any initialization. [or did you ad a special default constructor
to RectStruct in Rect.h??]

So calcMinMaxValue function should be like this:

void calcMinMaxValue(RectStruct& A,RectStruct& B,float& min,float& max)
{
   // your stuff here
}

Thirdly, int main(), you allocate two rectangles rect01 and rect02. That is fine,but for every new there should be a delete e.g. put a delete rect01; etc at the end of the code.

The error that your compiler is complaining about stems from a poor decision that all beginners seem to do. You have added using namespace std; . However, what you have forgotten is that in namespace std there is a max and a min function. So when you use the variables min and max, all would have been ok IF you had declared them, but you forgot. Normally, if you have used another name e.g. maxA, you would have seem an error message like this: minA was not declared in this scope, and your would have known what to do. If you had NOT put using namespace std; , then you would have had the same error message.
So it is a little more work to write std::cout etc, but it saves a LOT of effort, confusion etc. [You can also just pick out the bits you want with something like, using namespace std::cout; Beyond that you have the basics of a working program, it took me only a couple of minutes to get it to compile and run [successfully].

So recode calcMinMax, and fix the few errors and you should be ok.

Ohh, allright. Thanks, but the reason why I didn't use a class is because the professor wanted to use struct instead, and just in case, here's the Rect.h:

#pragma once

#include <iostream> 

struct Rect {
	float x;
	float y;
	float width;
	float height;
};

typedef struct Rect RectStruct;

void assignRectValues(RectStruct *, float, float, float, float);
void printRectValues(const RectStruct *); 
float calcRectArea(const RectStruct *); 
void calcMinMaxValue(float &, float &);

using namespace std;

Well if your professor said use a struct, not a problem you just do this:

struct Rect
{
  private:
     float x;
     float y;
     // etc
  public:
    Rect(const float,const float,const float,const float);
};

Yes I know that is not what was intended ;)

The only difference between a struct and a class is that a class is by default private and a struct is by default public. If you add private: and public: statements they are the same.

Three minor points, the typedef serves no purpose but to give you two names for the struct, e.g. Rect and RectStruct, surely just call the struct RectStruct.

You might want to consider using a reference to RectStruct in printRectValue/assignRectValue/calcRectArea etc, e.g. float calcRectArea(const RectStruct&);

NEVER put using namespace std; at the end of an include file like that!!

What is the purpose of the A and B in the caclMinMaxValue function as parameters??

The A and the B are the two different RectStruct that you are comparing. If you don't pass that then there is no point to the function?

The function takes any two rectangles and returns (in variables min/max) the areas such that the largest area is in max and the smallest area is in min.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.