LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-13-2005, 12:50 AM   #1
Kenji Miyamoto
Member
 
Registered: Dec 2004
Distribution: Mandrake 10.1; Fedora Core 3; FreeBSD 5.3; Slackware 10.1 (2.6.10);
Posts: 234

Rep: Reputation: 30
First Time with C++, Unknown Errors


I can't figure out the few remaining errors in this program I'm writing:
Code:
#include <iostream>
#include <math.h>
using namespace std;
float quadratic(int mode);
float median(float y1, float y2);
float yVal(float median);


class equation{
	private:
		int x1, x2, x3, choose;
		float y, mid;
	public:
		equation(); // constructor
		~equation(); // destructor
		void setEqu(int x1, int x2, int x3);
		int getVar(int choose);
		float solve(int mode);
		void setVal(float mid);
		float getMidY();
};

equation::equation()
{
	x1 = 0;
	x2 = 0;
	x3 = 0;
	cout << "Equations initilazed" << endl;
}

equation::~equation()
{
	cout << "Memory cleared" << endl;
}

void equation::setEqu(int x1, int x2, int x3)
{
	this->x1 = x1;
	this->x2 = x2;
	this->x3 = x3;
}

int equation::getVar(int choose)
{
	switch(choose)
	{
		case 1:
			return x1;
			break;
		case 2:
			return x2;
			break;
		case 3:
			return x3;
			break;
	}
}

float equation::solve(int mode)
{
	if(mode == 0)
	{
		return (-x2 + sqrt( pow(x2, 2) - 4 * (x1 * x3)))/(2 * x1);
	}
	else if(mode == 1)
	{
		return (-x2 - sqrt( pow(x2, 2) - 4 * (x1 * x3)))/(2 * x1);
	}
}

void equation::setVal(float mid)
{
	this->mid = mid;
}

float equation::getMidY()
{
	return (x1 * pow(mid, 2)) + (x2 * mid) + x3;
}

equation quad;

int main()
{
	int x1, x2, x3;
	float y1, y2, med, val;
	cout << "Welcome to my quadratic program, written in C++." << endl;
	cout << "x^2:";
	cin >> x1;
	cout << "\nx:";
	cin >> x2;
	cout << "\n+:";
	cin >> x3;
	equation.setEqu(x1, x2, x3);
	y1 = quadratic(0);
	y2 = quadratic(1);
	med = median(y1, y2);
	val = equation.getMidY();
	cout << x1 << "x^2 + " << x2 << "x + " << x3 << ";" << endl;
	cout << "f(" << y1 << ") = 0;" << endl;
	cout << "f(" << y2 << ") = 0;" << endl;
	cout << "Vertex = (" << med << ", " << val << ");" << endl;
	return 0;
}

float quadtratic(int mode)
{
	float result;
	if(mode == 0)
	{
		result = equation.solve(0);
	}
	else if(mode == 0)
	{
		result = equation.solve(1);
	}
	
	return result;
}

float median(float y1, float y2)
{
	float result;
	result = (y1 + y2)/2;
	equation.setVal(result);
	return result;
}
Could someone point out the problems relating to "error: expected primary-expression before '.' token".
 
Old 01-13-2005, 02:05 AM   #2
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
Alright, here we go:

You've created a global instance of the equation class called "quad", but you're getting errors because
you're referencing this class using the class name itself and not the instance. To cite an example:

line #111 reads:
Code:
result = equation.solve(0);
This causes a compilation error because equation is the name of the class, not an instance of that class.
Changing equation to quad (the name of your instance) fixes this:
Code:
result = quad.solve(0);
You must make this change to the following lines in order for the code to compile: 94, 98, 111, 115, 125

Also, I'm not sure which compiler you're using, but lines 63 and 67 will not compile under ANSI standard (g++ and other sane compilers will reject it. I don't know about microsoft, they use their own "standard".) If those lines are giving you errors, too, you need to add (double) before x2 in each function call to pow(). (Because there is no pow(int, int) function in ANSI! There is, however a pow(double, int)).

Also, there is a slight typo which will prevent the program from linking: you define the function prototype for "quadratic" but the later occuring definition itself uses the spelling "quadtratic". Change "quadtratic" to "quadratic" and you'll fix that error.

After making those changes, it will compile. I've tested that code under g++ after making the previously described changes and it compiles flawlessly. I don't know if there's any logic errors, though... I just quickly went through it and fixed the problems.

IN SUMMARY:
Lines 94, 98, 111, 115, 125: Change equation to quad
Line 106: Change "quadtratic" to "quadratic"
Lines 63, 67: Insert (double) before x2 in the pow(...) function call.

Hope that helps. Good luck. =)

- Zhang Mai Ke
 
Old 01-13-2005, 07:44 AM   #3
Kenji Miyamoto
Member
 
Registered: Dec 2004
Distribution: Mandrake 10.1; Fedora Core 3; FreeBSD 5.3; Slackware 10.1 (2.6.10);
Posts: 234

Original Poster
Rep: Reputation: 30
I don't think it liked the double being placed there, because the G++ (What I was using) returned this:
Code:
./calcCPP.cpp: In member function `float equation::solve(int)':
./calcCPP.cpp:63: error: expected primary-expression before "double"
./calcCPP.cpp:67: error: expected primary-expression before "double"
 
Old 01-13-2005, 06:20 PM   #4
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
Hmmm... Well, copying and pasting EXACTLY what you posted and then compiling it gives me the following errors:

Code:
test.cpp: In member function `float equation::solve(int)':
test.cpp:63: error: call of overloaded `pow(int&, int)' is ambiguous
/usr/include/bits/mathcalls.h:154: error: candidates are: double pow(double,
   double)
/usr/include/c++/3.3.4/cmath:512: error:                 long double
   std::pow(long double, int)
/usr/include/c++/3.3.4/cmath:508: error:                 float std::pow(float,
   int)
/usr/include/c++/3.3.4/cmath:504: error:                 double
   std::pow(double, int)
/usr/include/c++/3.3.4/cmath:495: error:                 long double
   std::pow(long double, long double)
/usr/include/c++/3.3.4/cmath:486: error:                 float std::pow(float,
   float)
test.cpp:67: error: call of overloaded `pow(int&, int)' is ambiguous
/usr/include/bits/mathcalls.h:154: error: candidates are: double pow(double,
   double)
/usr/include/c++/3.3.4/cmath:512: error:                 long double
   std::pow(long double, int)
/usr/include/c++/3.3.4/cmath:508: error:                 float std::pow(float,
   int)
/usr/include/c++/3.3.4/cmath:504: error:                 double
   std::pow(double, int)
/usr/include/c++/3.3.4/cmath:495: error:                 long double
   std::pow(long double, long double)
/usr/include/c++/3.3.4/cmath:486: error:                 float std::pow(float,
   float)
test.cpp: In function `int main()':
test.cpp:94: error: parse error before `.' token
test.cpp:98: error: parse error before `.' token
test.cpp: In function `float quadtratic(int)':
test.cpp:111: error: parse error before `.' token
test.cpp:115: error: parse error before `.' token
test.cpp: In function `float median(float, float)':
test.cpp:125: error: parse error before `.' token
After adding the (double) or replaceing "using namespace std;" the errors are reduced to:
Code:
test.cpp: In function `int main()':
test.cpp:94: error: parse error before `.' token
test.cpp:98: error: parse error before `.' token
test.cpp: In function `float quadtratic(int)':
test.cpp:111: error: parse error before `.' token
test.cpp:115: error: parse error before `.' token
test.cpp: In function `float median(float, float)':
test.cpp:125: error: parse error before `.' token
And after making the remaining changes, no errors are reported.

I've never seen the "primary-expression" error you report from g++ before, although it resembles the "parse error" I got when I compiled your code.

Last edited by zhangmaike; 01-13-2005 at 06:44 PM.
 
Old 01-13-2005, 06:26 PM   #5
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
I just double-checked, and apparently pow(int, int) is fine ANSI-wise... but there are still errors on those lines. The source of that problem is the "using namespace std;" line. If you replace that with "using std::cout;", "using std::cin;", and "using std::endl;" you'll be fine there.

Here's a copy of the code which compiles fine:
Code:
#include <iostream>
#include <math.h>

// Replaced using namespace std;
using std::cout;
using std::cin;
using std::endl;

float quadratic(int mode);
float median(float y1, float y2);
float yVal(float median);


class equation{
	private:
		int x1, x2, x3, choose;
		float y, mid;
	public:
		equation(); // constructor
		~equation(); // destructor
		void setEqu(int x1, int x2, int x3);
		int getVar(int choose);
		float solve(int mode);
		void setVal(float mid);
		float getMidY();
};

equation::equation()
{
	x1 = 0;
	x2 = 0;
	x3 = 0;
	cout << "Equations initilazed" << endl;
}

equation::~equation()
{
	cout << "Memory cleared" << endl;
}

void equation::setEqu(int x1, int x2, int x3)
{
	this->x1 = x1;
	this->x2 = x2;
	this->x3 = x3;
}

int equation::getVar(int choose)
{
	switch(choose)
	{
		case 1:
			return x1;
			break;
		case 2:
			return x2;
			break;
		case 3:
			return x3;
			break;
	}
}

float equation::solve(int mode)
{
	if(mode == 0)
	{
		return (-x2 + sqrt( pow(x2, 2) - 4 * (x1 * x3)))/(2 * x1);
	}
	else if(mode == 1)
	{
		return (-x2 - sqrt( pow(x2, 2) - 4 * (x1 * x3)))/(2 * x1);
	}
}

void equation::setVal(float mid)
{
	this->mid = mid;
}

float equation::getMidY()
{
	return (x1 * pow(mid, 2)) + (x2 * mid) + x3;
}

equation quad;

int main()
{
	int x1, x2, x3;
	float y1, y2, med, val;
	cout << "Welcome to my quadratic program, written in C++." << endl;
	cout << "x^2:";
	cin >> x1;
	cout << "\nx:";
	cin >> x2;
	cout << "\n+:";
	cin >> x3;
	// Made change here: equation -> quad
	quad.setEqu(x1, x2, x3);
	y1 = quadratic(0);
	y2 = quadratic(1);
	med = median(y1, y2);
	// Made change here: equation -> quad
	val = quad.getMidY();
	cout << x1 << "x^2 + " << x2 << "x + " << x3 << ";" << endl;
	cout << "f(" << y1 << ") = 0;" << endl;
	cout << "f(" << y2 << ") = 0;" << endl;
	cout << "Vertex = (" << med << ", " << val << ");" << endl;
	return 0;
}

// Made change here: quadtratic -> quadratic
float quadratic(int mode)
{
	float result;
	if(mode == 0)
	{
	   	// Made change here: equation -> quad
		result = quad.solve(0);
	}
	else if(mode == 0)
	{
	   	// Made change here: equation -> quad
		result = quad.solve(1);
	}
	
	return result;
}

float median(float y1, float y2)
{
	float result;
	result = (y1 + y2)/2;
	// Made change here: equation -> quad
	quad.setVal(result);
	return result;
}

Last edited by zhangmaike; 01-13-2005 at 06:41 PM.
 
Old 01-13-2005, 08:35 PM   #6
Kenji Miyamoto
Member
 
Registered: Dec 2004
Distribution: Mandrake 10.1; Fedora Core 3; FreeBSD 5.3; Slackware 10.1 (2.6.10);
Posts: 234

Original Poster
Rep: Reputation: 30
What version og the GCC / G++ do you have? I have 3.4.2 20041017 (Red Hat 3.4.2-6.fc3).

Anyway, it didn't seem to work when running, since it got the "nan" thing:
Code:
5x^2 + 2x + 3;
f(nan) = 0;
f(nan) = 0;
Vertex = (nan, nan);
Is there a reason why?

Last edited by Kenji Miyamoto; 01-13-2005 at 08:42 PM.
 
Old 01-13-2005, 08:57 PM   #7
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
Yeah. The function 5x^2 + 2x + 3 has no real zeroes. Try solving an equation yourself, first, then test the program to see if you get the same answer. And make sure your equation has real zeroes.

My GCC version is 3.3.4.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
atkbd.c "unknown keypress" errors LloydM Fedora 2 09-09-2005 01:22 PM
SYSCTL errors on net.ipv4.route.flush with unknown error 1 Electro Linux - Networking 1 08-01-2005 12:48 AM
Modem connected but unknown host errors from applications. ruse Linux - Distributions 7 09-11-2004 11:25 PM
Sound in kernel 2.6: Lots of "Unknown Symbol" errors jemenake Linux - General 0 10-22-2003 09:09 PM
Boot Time Errors DeadPuddle Slackware 3 10-09-2003 06:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:07 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration