0

I wrote a c++ function what takes in an array and prints some stuff with no output, yet when I try and call it I get the error:

main.cpp:11:2: error: no matching function for call to 'render'
        render(board);
        ^~~~~~
./functions.hpp:1:6: note: candidate function not viable: requires 0 arguments, but 1 was
      provided
void render();


Here is my code:

I have a page for my functions (functions.hpp):

#include <iostream>
using namespace std;

void render(int board[]){
    cout << board[0] << "\n";
}

Then my main file:

#include <iostream>
#include "functions.hpp"
using namespace std;

int main() {
    string board[] = {"_", "_", "_", "_", "_", "_", " ", " ", " "};

    render(board); // This is where I am hitting the error
}

Can someone point out the error, I am very new to c++.

  • Your render method is declared as `void render(int board[])` but you're passing in strings. – Brian Rasmussen Jun 02 '20 at 05:31
  • I changed it to the string type yet still receive the exact same error. –  Jun 02 '20 at 05:43
  • Take several days to read a good [C++ programming](http://stroustrup.com/Programming/) book (it certainly is available at your university library), then read [more about C++](https://en.cppreference.com/w/cpp). Provide an [MRE] in your question – Basile Starynkevitch Jun 02 '20 at 05:46
  • The error message says the function takes 0 arguments. That would be if it was declared `void render()`, which doesn't match the code you've shown. – Barmar Jun 02 '20 at 05:47
  • 2
    Don't edit the question to incorporate the answers, then the answers make no sense. – Barmar Jun 02 '20 at 05:48

2 Answers2

2

Your render function accepts an array of integers, while you are trying to pass in an array of string.

You can fix this by changing your function to accept the type you are trying to pass.

void render(string board[]){
    cout << board[0] << "\n";
}

Your compiler output points to a function prototype for void render();, which does not accept any arguments. You might be dealing with multiple header files with the same name (perhaps only varying by case). You will need to determine which header file is being included by your C++ source file, and then troubleshoot your code accordingly.

jxh
  • 69,070
  • 8
  • 110
  • 193
  • What books did you read about C++ programming ? Read also the C++11 standard [n3337](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf).. StackOverflow is *not* a *do-my-homework* website – Basile Starynkevitch Jun 02 '20 at 05:49
  • 1
    https://tio.run/##dY4xC8IwEIX3/IpHXVpQ6t7q7u6mRWJytIE2KUmqQ@lvj4l20MHjeBzHx3tPjONO9Fy3IWyUFv0kCbUyzlviw5FNTukWmg/kRi4IzsuKsYdREpa0JJtHMiF3w628NMXMEEeYyaOu1@@@SXd21VnFFsaU9hi40nmBD/1rgQPm7JZt8V/wJUvsk0zWOm@TokJZ4twph7jPjizhBD6gU96nKN8RyFpj2RLCCw – jxh Jun 02 '20 at 05:50
  • I didn't read any books about c++ I am just using various online resources. –  Jun 02 '20 at 05:52
  • I have updated the question, and I think that the error is because I am using two files but I still do not know how to fix it. –  Jun 02 '20 at 05:55
  • No, I have only been doing this for a day or two now and am stuck on this issue. –  Jun 02 '20 at 05:57
  • 1
    Solve the problem with a single file first. Then, when you split the file contents into the header file, you can conclude the problem is with how you are dealing with the header file. Then, you can ask a coherent question. – jxh Jun 02 '20 at 05:57
  • Yes, I can confirm that it works with one file but when I split it it stops working. I'll ask the coherent question then. Thanks. –  Jun 02 '20 at 05:59
  • Your understanding of the role of the [linker](https://en.wikipedia.org/wiki/Linker_(computing)) is wrong. You may be interested in reading about [ELF](https://en.wikipedia.org/wiki/Executable_and_Linkable_Format) – Basile Starynkevitch Jun 02 '20 at 06:01
  • See also [this](https://stackoverflow.com/a/62145150/841108) answer – Basile Starynkevitch Jun 02 '20 at 06:04
1

That should be in function.hpp:

#pragma once
#include <iostream>
using namespace std;
void render(string board[])
{
    cout << board[0] << "\n";
}

and the .cpp file:

#include <iostream>
#include "function.hpp"
using namespace std;

int main() {
    string board[] = { "_", "_", "_", "_", "_", "_", " ", " ", " " };

    render(board); // This is where I am hitting the error
}

everything begins from wrong type of function argument but you still have int in you function in .hpp file

NixoN
  • 661
  • 1
  • 6
  • 19
  • I changed it to use the string type but the error still persists. –  Jun 02 '20 at 05:45
  • Alright, I updated the question. Now it is more accurate. I think that the error is because I am using two files. –  Jun 02 '20 at 05:51