[英]How to pass multi-dimensional array to header file in C++
我正在嘗試使用多維數組。 我的目標是為矩陣函數創建一個單獨的文件,但是在設置V
的值時遇到了麻煩。
錯誤: 'V' was not declared in this scope
由於此錯誤說明的范圍很廣,因此我在搜索中找不到滿意的答案。
這就是我要實現的。
main.cpp
#include <bits/stdc++.h>
using namespace std;
#include "prims.h"
int main()
{ int V = 5;
int graph[V][V] = { {... },
{... },
{... },
{... },
{... } };
func1(graph);
func2(graph);
return 0;
}
prims.h
#ifndef PRIMS_H
#define PRIMS_H
#include <bits/stdc++.h>
using namespace std;
int func1(int graph[V][V]);
int func2(int graph[V][V]);
#endif
prims.cpp
#include <bits/stdc++.h>
using namespace std;
#include "prims.h"
int func1(int graph[V][V])
{
// function
}
int func2(int graph[V][V])
{
// function
}
如果需要更多說明,請在下面發表評論。 謝謝。
由於要從main設置值,因此一種替代方法是將V
聲明為main
全局變量,並聲明為prims.h中的extern const int
,以便在prmis.cpp中也可見。
prims.h
extern const int V;
main.cpp
const int V = 5; //declared as global in main
int main()
{
/* */
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.