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() 将其声明为 im 以链接到另一个 .cpp 文件。我知道拥有单独的 .cpp 文件而不是标题不是标准做法。请有人用非常简单的术语解释一下,我现在像s***一样厚

#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包含。惯例是使用.h.hpp用于头文件,它通常包含相应.cpp实现的前向声明、类等。

所以,我会检查:

  • 代码不是命名空间的。
  • 你包括正确的东西。
  • 您正在使用正确的名称调用该函数。
于 2012-04-29T22:03:57.820 回答