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

  • C/C++
  • Thread starter Jamin2112
  • Start date
  • Tags
    C++ Error
In summary: 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
  • #1
Jamin2112
986
12
My compiler doesn't like something about my enumerator declaration. Maybe something else too. I'm trying to figure it out.

Code:

Code:
#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
 
Technology news on Phys.org
  • #2
the class needs to be inside the name space. move the } before tha class statement to the end of the file.
 
  • #3
or type at top of file:

using namespace CalcWizConsts;
 
  • #4
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.
 
  • #5
Alright, I went the professional route:

Code:
#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:
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.
 

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

1. What is a compile-time error?

A compile-time error, also known as a syntax error, is an error that occurs during the compilation of a program. It indicates that there is an error in the syntax or structure of the code, preventing it from being successfully compiled into an executable program.

2. How do I identify a compile-time error?

Compile-time errors are usually indicated by error messages from the compiler, which will point to the specific line or area of code where the error occurred. These error messages typically include information about the type of error and may offer suggestions on how to fix it.

3. What causes compile-time errors in C++?

Compile-time errors in C++ can be caused by a variety of factors, including syntax errors, missing or incorrect library declarations, and mismatched data types. They can also occur when attempting to use variables that have not been declared or initialized.

4. How do I fix a compile-time error?

To fix a compile-time error, you will need to carefully examine the error message and identify the specific issue in your code. This may involve making changes to the syntax, data types, or library declarations. Once the error has been fixed, the code should be able to successfully compile.

5. Is it possible to prevent compile-time errors?

While it is not possible to completely eliminate compile-time errors, there are steps you can take to reduce the likelihood of encountering them. This includes writing clean and well-structured code, using comments to document your code, and regularly testing and debugging your code as you write it.

Similar threads

  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
10
Views
8K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
14
Views
7K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
5
Views
891
Back
Top