0

I am trying to import the "Exponent" function from library.cpp to main.cpp, but I am getting

error: no matching function for call to 'Exponent'

  else if (op == "**") {result =  Exponent(x,y);}

main.cpp

#include <iostream>
#include <cmath>
#include "library.cpp"
using namespace std;



int main() {
  int x;
  int y;
  int result;
  string op;
  
  cout << "CALCULATOR" << endl;
  cout <<"Enter two numbers then an operator" << endl;
  cout << "1st Number: ";
  cin >> x;
  cout << "2nd Number: ";
  cin >> y;
  cout << "Operator(+,-,*,/,**): ";
  cin >> op;

  if (op == "+") {result = x + y;}
  else if (op == "-") {result = x - y;}
  else if (op == "*") {result = x * y;}
  else if (op == "/") {result = x / y;}
  else if (op == "**") {result =  Exponent(x,y);}
  
  cout << x << " " << op << " " << y << " = " << result <<endl;
  
  return 0;
}

library.cpp


int Exponent(int x,int y, int result) {
 for(int i = 0; i < y; ++i)
{
    return result = result + x * x;
}
   
}
Mat
  • 202,337
  • 40
  • 393
  • 406
  • 7
    As a rule of thumb, `#include "foo.cpp"` is nearly always wrong. You should define a header file `library.h` with just the function signature and include that. – Botje Apr 18 '23 at 19:09
  • 2
    You define `Exponent` with three arguments and call it with two. What do you expect the compiler to do for the third one? – Botje Apr 18 '23 at 19:10

1 Answers1

2

Lots of things wrong here.

Here's an exponent function that works

int Exponent(int x, int y) {
    int result = 1;
    for(int i = 0; i < y; ++i)
    {
        result *= x;
    }
    return result;
}

Note that this problem has nothing to do with 'importing'. Your Exponent function would not have worked even if it had been in main.cpp. Breaking large problems into smaller ones is a key part of programming. A better approach here would have been getting the function to work in main.cpp first, and only then trying to move it to a different cpp file. Try to solve one problem at a time, then when it doesn't work you have a better idea of where the problem is.

john
  • 85,011
  • 4
  • 57
  • 81