0

C:\Users\George\Desktop\linear_equation_calc\main.cpp||関数内 'int main(int, const char**)':| C:\Users\George\Desktop\linear_equation_calc\main.cpp|101|エラー: 'calcparallelplugin' はこのスコープで宣言されていません| ||=== ビルドが終了しました: 1 エラー、0 警告 ===|

これは私が取得し続けるエラーです。宣言の意味は理解していますが、calcparallelplugin() を使用して別の .cpp ファイルにリンクするように宣言する方法を本当に理解していません。ヘッダーではなく.cppファイルを個別に作成することは、標準的な慣行ではないことを私は知っています。誰か本当に簡単な言葉で説明してください、私は今のところめちゃくちゃ太っています

#include <iostream>
#include <string.h>

using namespace std;

// Function includes
// I try to keep them in the order they appear in the
// output below for organization purposes
#include "calc.m.xy12plugin.cpp"
#include "calc.b.xymplugin.cpp"
#include "calc.m.xybplugin.cpp"
#include "calc.point.xymplugin.cpp"
#include "calc.parallelplugin.cpp"

// The above one would be here, too

int main(int argc, const char* argv[]) {
  int i;
  i = 0;
  cout << "Linear Equation Calculator" << endl << "Copyright (c) 2011 Patrick Devaney" << endl
  << "Licensed under the Apache License Version 2" << endl;
  // This loop makes the code a bit messy,
  // but it's worth it so the program doesn't
  // crash if one enters random crap such as
  // "zrgxvd" or "54336564358"
  while(i < 1) {
    cout << "Type:" << endl
    << "0 to calculate a slope (the M value) based on two points on a line" << endl
    << "1 to calculate the Y-intercept (the B value) based on two points and a slope" << endl
    << "2 to calculate the slope (the M value) based on the Y-intercept and X and Y" << endl <<
    "plug-ins" << endl
    << "3 to find the next point up or down a line based on the slope (M) and X and Y"
    << endl << "plug-ins" << endl
    << "4 to find a point x positions down the line based on the slope (M) and X and Y"
    << endl << "plug-ins" << endl
    << "5 to find the equation of a parallel line in form y=mx+c"
    << endl << "plug-ins" << endl;

    string selection;
    cin >> selection;
    if(selection == "0") {
      mcalcxyplugin();
      i++;
    }
    else if(selection == "1") {
      calcbxymplugin();
      i++;
    }
    else if(selection == "2") {
      calcmxybplugin();
      i++;
    }
    else if(selection == "3") {
      calcpointxymplugin(1);
      i++;
    }
    else if(selection == "4") {
      int a;
      cout << "How many points up/down the line do you want? (Positive number for points" << endl
      << "further up, negative for previous points" << endl;
      cin >> a;
      calcpointxymplugin(a);
      i++;
    }
    else if(selection == "5"){

      calcparallelplugin();
      i++;
    }
    else {
      i = 1;
    }
    // End of that loop below
  }
  return 0;
}
4

1 に答える 1

3

このスコープで宣言されていないエラーは、まさにそれを意味します。すべての#include<...>ファイルがメイン ファイルにインクルードされた後、コンパイラはその関数を見つけることができなかったため、何をすべきかわかりません。

ただし、これは別のケースにも当てはまります。

#include <iostream>

int main(int argc, char** argv)
{
    testfunc();
}

void testfunc()
{
    std::cout << "test!" << std::endl;
}

この場合、問題の理由は、コンパイラが関数を前方宣言する必要があるためです。つまり、関数プロトタイプが必要です。これはうまくいきます:

#include <iostream>

void testfunc(); // the compiler sees this and knows the linker
                 // has the responsibility of finding this symbol.

int main(int argc, char** argv)
{
    testfunc();
}

void testfunc()
{
    std::cout << "test!" << std::endl;
}

スコーピングに関しては、別のケースもあります。名前空間はスコープに影響するため、たとえば次のようになります。

#include <iostream>

void testfunc();

int main(int argc, char** argv)
{
    testfunc();
}

namespace test
{
    void testfunc()
    {
        std::cout << "test!" << std::endl;
    }
}

も失敗します。プロトタイプには、 が必要void test::testfunc();です。これは、名前空間の内部がグローバル スコープではなく、それ自体がスコープであるため::です。using namespace std;コードを記述することで、関数stdをグローバル名前空間で使用できるようにします。

また、インクルードに使用されていることにも気づき.cppました。規則では、対応する実装の前方宣言、クラスなどを含むことが多いヘッダー ファイルに対して.horを使用します。.hpp.cpp

だから、私はそれを確認します:

  • コードは名前空間化されていません。
  • あなたは正しいものを含めています。
  • 正しい名前で関数を呼び出しています。
于 2012-04-29T22:03:57.820 に答える