3 Replies - 2587 Views - Last Post: 16 October 2012 - 06:15 AM Rate Topic: -----

#1 oomjos  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 32
  • Joined: 09-September 12

Deprecated Conversion from string constant to char *

Posted 15 October 2012 - 11:52 PM

//Symbol.h
ifndef SYMBOL_H
#define SYMBOL_H
#include <string>

class Symbol{
private:
	const string symbol;
	const string name;

public:
	Symbol()
	{
	}
	Symbol(const string s, const string n)
	{ symbol = s;
	name = n; }

	const string getName() const
	{ return name; }

	const string getSymbol() const
	{ return symbol; }

	void setName(const string n)
	{ name = n; }

	void setSymbol(const string s)
	{ symbol = s; }
};
#endif




//Equation.cpp
#include "Expression.h"
#include "Equation.h"
#include "Symbol.h"
#include <cmath>
#include <string>

char *mult = "*";
char *mult1 = "Times";

Symbol plus("+", "Plus");
Symbol minus("-", "Minus");
Symbol times("*", "Times);
Symbol divide("/", "Divide");
Symbol sqrts("sqrt", "SquareRoot");
Symbol abs("abs", "AbsoluteValue");
Symbol max("max", "Maximum");
Symbol logs("log", "Logarithm");

Symbol *symArr = new Symbol[8];

symArr[0] = plus;
symArr[1] = minus;
symArr[2] = times;
symArr[3] = divide;
symArr[4] = sqrts;
symArr[5] = abs;
symArr[6] = max;
symArr[7] = logs;

Equation :: Equation(string eq){
	Expression LHS;
	Expression RHS;
	
	for(int i=0; i<eq.length(); i++){
		if(eq[i] == '='){
			LHS.setExpression(eq.substr(0,(i-1));
			RHS.setExpression(eq.substr((i+1), (eq.length()-1));
		}
	}
}	

Equation :: Equation(string x){
	Equation temp;
	
	for(int i =0; i<x.length(); i++)
	{
	  if(x[i] == '=')
	  {
	    temp.left.setExpression(x.substr(0,i));
	    temp.right.setExpression(x.substr(i+1, x.length());
	  }
	}
		
}

Equation :: ~Equation()
{
}

Equation& :: Equation operator=(const Equation& other){
	Equation temp;
	temp.left = other.left;
	temp.right = other.right;
	return temp;
}
//Equation.cpp
int Equation :: evaluateRHS(string Eq){
int ans = 0;
for(int i = 0; i<Eq.lenght()-2; i++)
{
 for(int j =0; j<7; j++)
 {
  if(Eq[i] == symArr[i+1].getSymbol())
  {
   switch(j)
   {
    case 0 : ans = evalAdd((Eq.substr(0, i-1) , (i+1, Eq.length()));
    break;
    case 1 : ans = evalSub((Eq.substr(0, i-1) , (i+1, Eq.length()));
    break;
    case 2 : ans = evalMult((Eq.substr(0, i-1) , (i+1, Eq.length()));
    break;
    case 3 : ans = evalDiv((Eq.substr(0, i-1) , (i+1, Eq.length()));
    break;
    case 4 : ans = evalSqrt((i+1, Eq.length()));
    break;
    case 5 : ans = evalAbs((Eq.substr(0, i-1) , (i+1, Eq.length()));
    break;
    //case 6 : ans = evalMax((Eq.substr(0, i-1) , (i+1, Eq.length())); [This line of code is suspect]
    case 6:	for(int i = 0; i<Eq.length(); i++)
			{
				if(Eq[i] == 'm')
				{
					if(Eq[i+1] == 'a')
					{
						if(Eq[i+2] == 'x')
						{
							num1 = substr(i+4, i+5);
							num2 = substr(i+5, i+6);
						}
						else
							break;
					}
					else
						break;
				}
				else
					break;
			}
			ans = evalMax(num1, num2);
    break;
    case 7 : ans = evalLog((Eq.substr(0, i-1) , (i+1, Eq.length()));
   }
  }
 }

}
}

int Equation :: evaluateLHS(string Eq){
	
}

void Equation :: instantiateVariable(char name, int value){
	for(int x =0; x<eq.length(); i++)
	{
		if(name == eq[i])
		{
			eq[i] = name;
		}
	}
}

int Equation :: RHSdistanceFromLHS(){
	return (evaluateLHS() - evaluateRHS());
}

int Equation :: evalAdd(int lhs, int rhs){
return lhs + rhs;
}

int Equation ::  evalSub(int lhs, int rhs){
return lhs - rhs;
}

int Equation ::  evalLog(int lhs, int rhs){
return log(lhs)/log(rhs);
}

int Equation :: evalMult(int lhs, int rhs){
return lhs * rhs;
}

int Equation :: evalDiv(int lhs, int rhs)}
return lhs / rhs;
}

int Equation :: evalNeg(int num){
return -num;
}

int Equation :: evalAbs(int lhs, int rhs){
return -num;
}																							  

int Equation :: evalMax(int LHS, int RHS){
return max(LHS, RHS);
}	

int Equation :: evalSqrt(int rhs){
return (rhs)^(0.5);
}






Solutions that I have tried:

-> Symbol(const string s, const string n)

-> Symbol(const char* s, const char* n)

Is This A Good Question/Topic? 0
  • +

Replies To: Deprecated Conversion from string constant to char *

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,405
  • Joined: 20-March 10

Re: Deprecated Conversion from string constant to char *

Posted 16 October 2012 - 12:58 AM

Hi,

You cant declare a variable as being constant and then later on change the value...

dont declare it as a constant....

class Symbol{
private:
	 string symbol;
	 string name;




You have other errors

Regards

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5881
  • View blog
  • Posts: 12,759
  • Joined: 16-October 07

Re: Deprecated Conversion from string constant to char *

Posted 16 October 2012 - 04:56 AM

Perhaps:
class Symbol{
private:
	const string symbol;
	const string name;

public:
	Symbol(const string &s, const string &n) : symbol(s), name(n) { }
	const string &getName() const { return name; }
	const string &getSymbol() const { return symbol; }

	// No.
	// void setName
	// void setSymbol
};



Also:
const Symbol SymArr[] = { 
	Symbol("+", "Plus"),
	Symbol("-", "Minus"),
	Symbol("*", "Times"),
	Symbol("/", "Divide"),
	Symbol("sqrt", "SquareRoot"),
	Symbol("abs", "AbsoluteValue"),
	Symbol("max", "Maximum"),
	Symbol("log", "Logarithm"),
};
const int SymArraySize = sizeof(SymArr)/sizeof(SymArr[0]);



No need for dynamic memory allocation, magic numbers, or lots of extra variables.
Was This Post Helpful? 1
  • +
  • -

#4 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Deprecated Conversion from string constant to char *

Posted 16 October 2012 - 06:15 AM

Just to point out, your original error line was about this:
char *mult = "*";
char *mult1 = "Times";


To cite the beloved standard (n3376: 2.14.5 String literals):

Quote

9. Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration.

I wont cite the standard on how an array can be converted to a pointer, but what happens here is that const char[] (e.g. "Times" string constant) is converted on the fly to const char*, and then you try to assign that to char* variable. See the problem? When you drop const like that, you can get in all sorts of trouble.

Correct code would look like this:
const char *mult = "*";
const char *mult1 = "Times";


But that would be the least of your problems. ;) baavgai gave you an idea what you could do to clean this up. Good luck. :)

This post has been edited by Xupicor: 16 October 2012 - 06:18 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1