0
#include <iostream>
using namespace std; 

//defining function
double distance(double,double);
int main() {
  
    //where im having issues i think
    cout << distance();

    return 0; 
}
//attempting to start the function with rate*time=distance and returning the value
double distance(double rate, double time)
{
    time = 10; 
    rate = 10; 
    return time*rate; 
}
main.cpp:9:11: error: no matching function for call to 'distance'
  cout << distance();
          ^~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/stl_iterator_base_funcs.h:138:5: note: 
      candidate function template not viable: requires 2 arguments, but 0
      were provided
    distance(_InputIterator __first, _InputIterator __last)
    ^
main.cpp:5:8: note: candidate function not viable: requires 2 arguments,
      but 0 were provided
double distance(double,double);
       ^
1 error generated.
compiler exit status 1

This is what I got when I attempted to run it through. I understand this is pretty rudimentary but I'd like to have an understanding of what I did wrong before I continue

eglease
  • 2,445
  • 11
  • 18
  • 28
rei
  • 39
  • 1
  • 4
    You need to give two parameters in your *call* to the function; like, say, `cout << distance(5.0, 10.0);` . – Adrian Mole Jun 28 '20 at 11:07
  • @Unslander Monica I don't know? Maybe I'm a beginner with minimal understanding? Crazy concept I know. – rei Jun 28 '20 at 11:18

2 Answers2

7

You should pass arguments to the function because you've defined as: double distance(double, double);

So, the solution would be:

#include <iostream>
using namespace std;    

double distance(double, double);

int main() 
{
   cout << distance(10, 10);
   return 0;
}

double distance(double rate, double time)
{
   return time * rate;
}

Also, it would be better to use std:: before everything that's part of the standard library of C++, instead of typing using namespace std; at the top of your code.

Read more: Why is "using namespace std;" considered bad practice?

JeJo
  • 30,635
  • 6
  • 49
  • 88
Dawid
  • 148
  • 9
  • 2
    Worth to mention here that the difficult to read error message is in part to blame on the use of using namespace std;, because there is a function std::distance, which accidentally got pulled in here. using namespace std is considered evil for a reason. – Strick Jun 28 '20 at 11:11
  • thank you, it was pretty simple I know but I'm glad you took the time! – rei Jun 28 '20 at 11:12
  • @Strick Yes. It would be better to use `std::` instead of the whole namespace at the top. – Dawid Jun 28 '20 at 11:12
  • 1
    @Dawid Maybe include that in your answer? Its kinda an important point. – Strick Jun 28 '20 at 11:14
0

At least pass the parameters when you are calling the function since your function needs to params