Mends.One

1 final compiler error :( can someone explain in real simple for me

Calculator, Linear Algebra, C++, Compiler Errors

C:\Users\George\Desktop\linear_equation_calc\main.cpp||In function 'int main(int, const char**)':| C:\Users\George\Desktop\linear_equation_calc\main.cpp|101|error: 'calcparallelplugin' was not declared in this scope| ||=== Build finished: 1 errors, 0 warnings ===|

this is the error that i keep getting. i understand what declaring means but i jsut really dont understand how im supposed to declare it as im using the calcparallelplugin() to link in another .cpp file. i know its not standard practice to have seperate .cpp files and not headers. Someone explain in really simple terms please, i am as thick as s*** at the moment

#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;
}
0
G
georgeherby
Jump to: Answer 1

Answers (1)

Error not declared in this scope means exactly that. After all the #include<...> files have been included in your main file, the compiler couldn't find that function, so it doesn't know what to do.

However, this also applies to another case:

#include <iostream>

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

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

In this case, the reason for the problem is that the compiler needs functions to be forward-declared - i.e. it needs function prototypes. This will work:

#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;
}

There's another case with regards to scoping, too. Namespaces affect scope, so for example:

#include <iostream>

void testfunc();

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

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

Will also fail. For your prototype, you need void test::testfunc();. This is because the inside of the namespace is a scope in its own right, as opposed to the global scope ::. By writing using namespace std; in your code, you're making functions available in std available in the global namespace.

I also notice you used .cpp for your includes. The convention is to use .h or .hpp for header files, which often comprise forward declarations, classes etc for a corresponding .cpp implementation.

So, I would check that:

  • The code isn't namespace'd.
  • You're including the right thing.
  • You are calling the function with the right name.
3
U
user257111

Related Questions