簡體   English   中英

打電話麻煩 c++ function

[英]Trouble calling c++ function

我寫了一個 c++ function 什么在一個數組中並打印一些沒有 output 的東西,但是當我嘗試調用它時,我得到了錯誤:

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();


這是我的代碼:

我有一個功能頁面(functions.hpp):

#include <iostream>
using namespace std;

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

然后我的主文件:

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

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

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

有人能指出錯誤嗎,我對 c++ 很陌生。

您的render function 接受一個整數數組,而您試圖傳入一個string數組。

您可以通過更改 function 以接受您嘗試傳遞的類型來解決此問題。

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

您的編譯器 output 指向void render(); ,它不接受任何 arguments。 您可能正在處理多個具有相同名稱的 header 文件(可能僅因大小寫而異)。 您需要確定 C++ 源文件中包含哪個 header 文件,然后相應地對代碼進行故障排除。

那應該在 function.hpp 中:

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

和 .cpp 文件:

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

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

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

一切都從錯誤類型的 function 參數開始,但你仍然有 int function in.hpp 文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM