I don't dabble in C++ very often, but I'm learning about data structures and the book uses c++ as it language. I'm currently going over setting up classes.
My issues are:
Visual Studio 2012 is barking about unidentified variables. I have the variables declared in my header, so I'm not quite sure why I'm having the issue.
I'm trying to overload the addition and multiplication operators (as non-member functions) but it is still trying to use it as if I'm only allowed to have one parameter for the overload.
Here is some code for what I'm doing:
1. Unidentified variables
/* Quadratic.h */
#include <iostream>
using namespace std;
class Quadratic
{
public:
// constructors
Quadratic::Quadratic()
// accessors
Quadratic::getA();
Quadratic::getB();
Quadratic::getC();
// mutators
Quadratic::setCoefficients(float coA, float coB, float coC);
private:
float a, b, c, x;
};
Quadratic.cpp:
/* Quadratic.cpp */
#include <iostream>
#include "Quadratic.h"
using namespace std;
class Quadratic
{
// Default constructor
Quadratic::Quadratic()
{
a = 0; // Visual Studio is complaining about a, b, & c
b = 0;
c = 0;
}
/* Mutators */
void Quadratic::setCoefficients(float coA, float coB, float coC)
{
a = coA;
b = coB;
c = coC;
}
/* Accessors */
float Quadratic::getA() const {
return a;
}
float Quadratic::getB() const {
return b;
}
float Quadratic::getC() const {
return c;
}
};
So that is what the first issue is about. I'm not quite sure why it isn't able to find those variables. Can someone point out what I'm doing wrong?
2. Overloading Operator (mismatch on the parameters)
/* Quadratic.h */
/* Overloading Operators */
Quadratic & operator+(const Quadratic & q1, const Quadratic & q2);
Quadratic & operator*(double r, const Quadratic & q);
It is simply telling me that I have too many parameters. I'm thinking it is expecting to do something like q1.operater+(q2)
where as I'm wanting to be able to do something like q3 = q1 + q2
Any pointers would be great for fixing these small issues.
Edit
Compiler errors as requested:
error C2804: binary 'operator +' has too many parameters
error C2804: binary 'operator *' has too many parameters
Basically what I mentioned above, perhaps I wasn't clear about it though.
Edit 2
Not sure why it was downvoted, but if you're going to downvote it, at least state why... If it was because the question was novice? Because the question was poorly worded or explained, or just because your ego is too high? No need to put someone down when they are attempting to learn something new.
Other than that, thank you legend2k, john, steve, salda and basile for all taking the time to help me out. I really do appreciate it. C++ is a lot more hands on than Java.