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

  • Thread starter Thread starter Jamin2112
  • Start date Start date
  • Tags Tags
    C++ Error
AI Thread Summary
The discussion revolves around resolving compilation errors related to enumerator declarations in a C++ program. The main issue stems from the use of enumerator values from the `CalcWizConsts` namespace without proper qualification, leading to undeclared identifier errors. The solution involves either using the namespace directly or prefixing the enumerator values with `CalcWizConsts::` to avoid naming conflicts. This approach is recommended to maintain clarity and prevent collisions with common names like `NONE`. The code compiles successfully after these adjustments. Additionally, the user expresses interest in developing an equation simplification function as part of their project.
Jamin2112
Messages
973
Reaction score
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
the class needs to be inside the name space. move the } before tha class statement to the end of the file.
 
or type at top of file:

using namespace CalcWizConsts;
 
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.
 
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
22
Views
3K
Replies
16
Views
4K
Replies
14
Views
34K
Replies
3
Views
3K
Replies
14
Views
7K
Replies
4
Views
2K
Back
Top