簡體   English   中英

函數指針“未在此范圍內聲明”

[英]Function pointer "was not declared in this scope"

在 C++ 腳本中main函數的頂部,我定義了一個基於命令行參數的函數指針,如下所示:

int main(int argc, char *argv[])
{

  // Set integration method.
  const char* method = argv[argc - 1];
  if (strcmp(method, "euler") == 0)
    {
      std::vector<double> (*get_V)(const std::vector<double> &, const double,
                                   const std::vector<double> &);
      get_V = euler;
    }
  else if (strcmp(method, "exact") == 0)
    {
      std::vector<double> (*get_V)(const std::vector<double> &, const double,
                                   const std::vector<double> &);
      get_V = exact;
    }
  else
    {
      throw std::invalid_argument("Invalid method supplied at command line.");
    }

我的目標是將變量get_V設置為指向函數euler或函數exact的指針,具體取決於命令行參數。

后來,還是在main函數里面,我調用get_V如下:

V = get_V(V, Delta_t, dV_dt);

當我嘗試編譯時,這一行 - 我在其中調用get_V - 引發以下錯誤:

(master)dbliss@nx3[dopa_net]> g++ -O3 hansel.cpp -o hansel.o
hansel.cpp: In function ‘int main(int, char**)’:
hansel.cpp:65: error: ‘get_V’ was not declared in this scope

這對我來說毫無意義。 確實在這個范圍內聲明了get_V 這是怎么回事? (如果有幫助,我可以發布我的整個主要功能,但它相當長。)

您聲明了兩個不同的變量,每個變量都命名為get_V ,它們都在if語句的范圍內本地。 一旦if語句中的代碼完成,那些變量就不再存在,它們已經超出了作用域。

簡單的解決方案? if語句之外聲明變量,並且只在if語句體中進行賦值。

暫無
暫無

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

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