Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help figuring out this C++ compile-time error?

  1. May 12, 2014 #1
    My compiler doesn't like something about my enumerator declaration. Maybe something else too. I'm trying to figure it out.

    Code:

    Code (Text):

    #include <string>

    namespace CalcWizConsts
    {
        char varChars[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
        size_t varCharsLen = sizeof(varChars) / sizeof(const char);
        enum eqOps { ADDITION, SUBTRACTION, MULTIPLICATION, DIVISION, COMPOSITION, NONE };
    }

    class CalculusWizard
    {
       
    private:
        bool isVariable(const char &);
        void simplifyEquation(std::string &);
        std::string composeFunction(const std::string &, const char &, const std::string &);

    public:
        CalculusWizard();
        ~CalculusWizard();
        std::string derivative(std::string, const char &, unsigned);
           
    };

    bool CalculusWizard::isVariable(const char & c)
    {
        char * thisptr = CalcWizConsts::varChars;
        char * offend = CalcWizConsts::varChars + CalcWizConsts::varCharsLen;
        while (thisptr != offend)
            if (*thisptr++ == c)
                return true;
        return false;
        /*
        Alternatively, return c >= 'a' && c <= 'z'
        */
    }

    void CalculusWizard::simplifyEquation(std::string & eq)
    {
      // .... Simplify equation, e.g. "(x^2)*(x^6)" --> "x^8"

    }

    std::string CalculusWizard::composeFunction(const std::string & fx, const char & x, const std::string & gx)
    {
        /* Return fx compose gx, i.e. return a string that is gx with every instance of the character x replaced
           by the equation gx.
           E.g. fx="x^2", x="x", gx="sin(x)" ---> composeFunction(fx, x, gx) = "(sin(x))^2"
        */
        std::string hx("");

        // ...
       
        return hx;
    }

    CalculusWizard::CalculusWizard()
    {
        ;
    }

    CalculusWizard::~CalculusWizard()
    {
        ;
    }



    /*
    Returns the n-th derivative of f with respect to x
    */
    std::string CalculusWizard::derivative(std::string fx, const char & x, unsigned n = 1)
    {

        if (n == 0)
        {
            return fx;
        }
        else if (n > 1)
        {
            while (n-- > 1)
                fx = derivative(fx, x);
        }

        // If here, find and return the derivative of f ...

        std::string gx(""), hx("");
        CalcWizConsts::eqOps op = NONE;
        /*
            Partition fx as
                fx = gx op gx
            where op is either addition, subtraction, multiplication, division or composition.
            Return derivative fpx according to rules of calculus.
        */

        // .....

        if (fx.size() == 0)
        {
            throw ("ERROR: Attempted to find derivative of empty function.");
            // Need to create different type of error
        }

        // ....

        std::string fpx("");
        switch (op)
        {
            case ADDITION:
                // Sum Rule: f'x = g'x + h'x
                return fpx = derivative(gx, x) + "+" + derivative(hx, x);
                break;
            case SUBTRACTION:
                // Difference Rule: f'x = g'x - h'x
                return fpx = derivative(gx, x) + "-" + derivative(hx, x);
                break;
            case MULTIPLICATION:
                // Product Rule: f'x = gx * h'x + hx * g'x
                return fpx = gx + "*" + derivative(hx, x) + "+" + hx + "*" + derivative(gx, x);
                break;
            case DIVISION:
                // Quotient Rule: f'x = (g'x * hx - gx * h'x) / [(hx)^2]
                return fpx = "(" + derivative(gx, x) + "*" + hx + "-" + gx + "*" + derivative(hx, x) + ")/[(" + hx + ")^2]";
                break;
            case COMPOSITION:
                // Chain Rule: f'x = f'(gx) * g'x
                return fpx = derivative(composeFunction(gx, x, hx), x) + "*" + derivative(hx, x);
                break;
            default:
                break; // Continue on
        }


        // ....


        simplifyEquation(fpx);
        return fpx;

    }

     
    Errors:

    Error 1 error C2065: 'NONE' : undeclared identifier c:\users\me\documents\visual studio 2013\projects\calculustool\calculustool\calculuswizard.cpp 88 1 CalculusTool
    Error 2 error C2065: 'ADDITION' : undeclared identifier c:\users\me\documents\visual studio 2013\projects\calculustool\calculustool\calculuswizard.cpp 109 1 CalculusTool
    Error 3 error C2051: case expression not constant c:\users\me\documents\visual studio 2013\projects\calculustool\calculustool\calculuswizard.cpp 109 1 CalculusTool
    Error 4 error C2065: 'SUBTRACTION' : undeclared identifier c:\users\me\documents\visual studio 2013\projects\calculustool\calculustool\calculuswizard.cpp 113 1 CalculusTool
    Error 5 error C2051: case expression not constant c:\users\me\documents\visual studio 2013\projects\calculustool\calculustool\calculuswizard.cpp 113 1 CalculusTool
    Error 6 error C2065: 'MULTIPLICATION' : undeclared identifier c:\users\me\documents\visual studio 2013\projects\calculustool\calculustool\calculuswizard.cpp 117 1 CalculusTool
    Error 7 error C2051: case expression not constant c:\users\me\documents\visual studio 2013\projects\calculustool\calculustool\calculuswizard.cpp 117 1 CalculusTool
    Error 8 error C2065: 'DIVISION' : undeclared identifier c:\users\me\documents\visual studio 2013\projects\calculustool\calculustool\calculuswizard.cpp 121 1 CalculusTool
    Error 9 error C2051: case expression not constant c:\users\me\documents\visual studio 2013\projects\calculustool\calculustool\calculuswizard.cpp 121 1 CalculusTool
    Error 10 error C2065: 'COMPOSITION' : undeclared identifier c:\users\me\documents\visual studio 2013\projects\calculustool\calculustool\calculuswizard.cpp 125 1 CalculusTool
    Error 11 error C2051: case expression not constant c:\users\me\documents\visual studio 2013\projects\calculustool\calculustool\calculuswizard.cpp 125 1 CalculusTool
    Warning 12 warning C4065: switch statement contains 'default' but no 'case' labels c:\users\me\documents\visual studio 2013\projects\calculustool\calculustool\calculuswizard.cpp 131 1 CalculusTool
    13 IntelliSense: identifier "NONE" is undefined c:\Users\me\Documents\Visual Studio 2013\Projects\CalculusTool\CalculusTool\CalculusWizard.cpp 88 28 CalculusTool
    14 IntelliSense: identifier "ADDITION" is undefined c:\Users\me\Documents\Visual Studio 2013\Projects\CalculusTool\CalculusTool\CalculusWizard.cpp 109 8 CalculusTool
    15 IntelliSense: identifier "SUBTRACTION" is undefined c:\Users\me\Documents\Visual Studio 2013\Projects\CalculusTool\CalculusTool\CalculusWizard.cpp 113 8 CalculusTool
    16 IntelliSense: identifier "MULTIPLICATION" is undefined c:\Users\me\Documents\Visual Studio 2013\Projects\CalculusTool\CalculusTool\CalculusWizard.cpp 117 8 CalculusTool
    17 IntelliSense: identifier "DIVISION" is undefined c:\Users\me\Documents\Visual Studio 2013\Projects\CalculusTool\CalculusTool\CalculusWizard.cpp 121 8 CalculusTool
    18 IntelliSense: identifier "COMPOSITION" is undefined c:\Users\me\Documents\Visual Studio 2013\Projects\CalculusTool\CalculusTool\CalculusWizard.cpp 125 8 CalculusTool
     
  2. jcsd
  3. May 12, 2014 #2
    the class needs to be inside the name space. move the } before tha class statement to the end of the file.
     
  4. May 12, 2014 #3
    or type at top of file:

    using namespace CalcWizConsts;
     
  5. May 12, 2014 #4

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    All of your errors result from omitting the namespace in those enum values in namespace CalcWizConsts.

    There are two ways to solve this: The amateur solution, using namespace CalcWizConsts, and the professional solution, which is to prefix those enum values with CalcWizConsts:: .

    There's a good reason for putting those enum values in a namespace. Rhetorical question: How many other programmers use NONE as an enumeration value? The rhetorical answer is "a whole bunch." Placing enumerations in a namespace or in a class is the professional thing to do. That way they don't collide with other people who thought NONE is a good idea (and oftentimes, it is a good idea). The professional thing to do is to waste a tiny bit of your time typing CalcWizConsts::NONE as opposed to NONE. There's no confusion whose NONE you are using when you use CalcWizConsts::NONE.

    The unprofessional thing to do is to pull everything in the CalcWizConsts namespace into the global namespace. In many professional organizations, using namespace is absolutely verboten. The potential for confusion magnifies when you use using namespace foo; using namespace bar; using namespace baz; and later use an unqualified NONE.
     
  6. May 12, 2014 #5
    Alright, I went the professional route:

    Code (Text):

    #include <string>
    #include "CalculusWizard.h"

    namespace CalcWizConst
    {
        char varChars[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
        size_t varCharsLen = sizeof(varChars) / sizeof(const char);
        enum eqOps { ADDITION, SUBTRACTION, MULTIPLICATION, DIVISION, COMPOSITION, NONE };
    }

    bool CalculusWizard::isVariable(const char & c)
    {
        char * thisptr = CalcWizConst::varChars;
        char * offend = CalcWizConst::varChars + CalcWizConst::varCharsLen;
        while (thisptr != offend)
            if (*thisptr++ == c)
                return true;
        return false;
        /* Alternatively, return c >= 'a' && c <= 'z'
        */
    }

    void CalculusWizard::simplifyEquation(std::string & eq)
    {
      /* Simplify equation, e.g. "(x^2)*(x^6)" --> "x^8"
      */

    }

    std::string CalculusWizard::composeFunction(const std::string & fx, const char & x, const std::string & gx)
    {
        /* Return fx compose gx, i.e. return a string that is gx with every instance of the character x replaced
           by the equation gx.
           E.g. fx="x^2", x="x", gx="sin(x)" ---> composeFunction(fx, x, gx) = "(sin(x))^2"
        */
        std::string hx(""); // equation to return
         
        std::string lastString("");
        for (std::string::const_iterator it(fx.begin()), offend(fx.end()); it != offend; ++it)
        {
            if (*it == x)
            {
                hx += gx;
                lastString.erase(lastString.begin(), lastString.end());
            }
            else
            {
                lastString.push_back(*it);
            }
        }
       
        return hx;
    }

    CalculusWizard::CalculusWizard()
    {
        ;
    }

    CalculusWizard::~CalculusWizard()
    {
        ;
    }



    /*
    Returns the n-th derivative of f with respect to x
    */
    std::string CalculusWizard::derivative(std::string fx, const char & x, unsigned n = 1)
    {

        if (n == 0)
        {
            return fx;
        }
        else if (n > 1)
        {
            while (n-- > 1)
                fx = derivative(fx, x);
        }

        // If here, find and return the derivative of f ...

        std::string gx(""), hx("");
        CalcWizConst::eqOps op = CalcWizConst::NONE;
        /*
            Partition fx as
                fx = gx op gx
            where op is either addition, subtraction, multiplication, division or composition.
            Return derivative fpx according to rules of calculus.
        */

        // .....

        if (fx.size() == 0)
        {
            throw ("ERROR: Attempted to find derivative of empty function.");
            // Need to create different type of error
        }

        // ....

        std::string fpx("");
        switch (op)
        {
        case CalcWizConst::ADDITION:
                // Sum Rule: f'x = g'x + h'x
                return fpx = derivative(gx, x) + "+" + derivative(hx, x);
                break;
        case CalcWizConst::SUBTRACTION:
                // Difference Rule: f'x = g'x - h'x
                return fpx = derivative(gx, x) + "-" + derivative(hx, x);
                break;
        case CalcWizConst::MULTIPLICATION:
                // Product Rule: f'x = gx * h'x + hx * g'x
                return fpx = gx + "*" + derivative(hx, x) + "+" + hx + "*" + derivative(gx, x);
                break;
        case CalcWizConst::DIVISION:
                // Quotient Rule: f'x = (g'x * hx - gx * h'x) / [(hx)^2]
                return fpx = "(" + derivative(gx, x) + "*" + hx + "-" + gx + "*" + derivative(hx, x) + ")/[(" + hx + ")^2]";
                break;
        case CalcWizConst::COMPOSITION:
                // Chain Rule: f'x = f'(gx) * g'x
                return fpx = derivative(composeFunction(gx, x, hx), x) + "*" + derivative(hx, x);
                break;
            default:
                break; // Continue on
        }


        // ....


        simplifyEquation(fpx);
        return fpx;

    }

     
    It compiled successfully.

    By the way, do you guys have an idea of what I'm trying to make? I'm just starting to get into the hard part. I think right now I'll work on the equation simplifying function

    Code (Text):

    void CalculusWizard::simplifyEquation(std::string & eq)
    {
      /* Simplify equation, e.g. "(x^2)*(x^6)" --> "x^8"
      */

    }
     
    I'll let you know about any roadbumps I hit.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Help figuring out this C++ compile-time error?
Loading...