1

I keep getting this error...

lab11a.cpp: In function ‘int main()’:
lab11a.cpp:120:34: error: no matching function for call to ‘SafeArray::GetElement(int&, double)’
lab11a.cpp:120:34: note: candidate is:
lab11a.cpp:48:6: note: bool SafeArray::GetElement(int, float&) const
lab11a.cpp:48:6: note:   no known conversion for argument 2 from ‘double’ to ‘float&’
lab11a.cpp:121:38: error: no matching function for call to ‘SafeArray::GetElement(int&, double)’
lab11a.cpp:121:38: note: candidate is:
lab11a.cpp:48:6: note: bool SafeArray::GetElement(int, float&) const
lab11a.cpp:48:6: note:   no known conversion for argument 2 from ‘double’ to ‘float&’

This is the code that I have in my interface for this method:

bool GetElement(const int i, float & Value) const;

This is the code in my implementation:

bool SafeArray::GetElement(const int i, float & Value) const 
{
    bool Success = false;
    if ((i >= 0) && (i < SIZE))
    {
        Success = true;
        Value = Array[i];
    }
    return Success;
}

This is the code in my main program:

for (int i = -5; i < 24; i++)
{
    data1.GetElement(i, i * 0.1);
    if (data1.GetElement(i, i * 0.1) == true)
        cout << "get " << i << " " << i * 0.1 << endl;
}
savannaalexis
  • 55
  • 1
  • 1
  • 10
  • In your interface and implementation change the parameter `value` to type float - your compiler may have a high warning level. –  Apr 15 '14 at 20:04

3 Answers3

4

Your function GetElement takes a reference to a float and writes a value there. However, you call it with i * 0.1, this is an unnamed temporary which cannot be passed for parameter float & Value. Think about it: Within GetElement you write something into Value, where should this number end up when you pass i * 0.1 as Value? Doesn't make sense and won't work. You have to pass a named variable of the correct type here, so the function can write something into it.

pentadecagon
  • 4,717
  • 2
  • 18
  • 26
  • I don't understand what the type should be. It is a float in the method so I don't see why it says it is a double. – savannaalexis Apr 15 '14 at 20:19
  • 1
    @savannaalexis You do call it with a `double`, `i*0.1`, but you should call it with a `float &`. What the hell are you doing here anyway? What's this `i*0.1` supposed to do? – pentadecagon Apr 15 '14 at 20:23
  • It says it cannot convert `double` to `float&`. `i * 0.1` evaluates to a double by default, but this is irrelevant. You are trying to take the address of a temporary, which is what the `&` operator does. Not allowed. – Gareth Apr 15 '14 at 20:23
  • @pentadecagon I am just trying to print certain values in a range if the method returns Success as true. – savannaalexis Apr 15 '14 at 20:27
  • Well, even if it would compile, somehow, that value `i*0.1` you pass into that function would be ignored. This function `GetElement` never actually *reads* what you pass as `Value`, it only writes there. – pentadecagon Apr 15 '14 at 20:32
  • 2
    Do this: `float f = ((float) i) * 0.1` and then call your function using `f` as the second parameter. – Henrique Barcelos Apr 15 '14 at 20:43
2

Your GetElement modifies its second parameter. How are you expecting it to assign to Value when Value is i * 0.1?

You could make the parameter float const &, but then you couldn't modify it in the function.

I think you need to think about what you're actually trying to do.

Alan Stokes
  • 18,815
  • 3
  • 45
  • 64
2

It seems that you want your main loop to look like this:

for (int i = -5; i < 24; i++)
{
    float f;
    if (data1.GetElement(i, f) == true)
        cout << "get " << i << " " << f << endl;
}

This way you can pass f by reference to your GetElement method. You can get the address of f (which is what the & symbol does), but you cannot get the address of a temporary (i*0.1).

Gareth
  • 3,502
  • 4
  • 20
  • 21