So I'm trying to create a class polynomial and implement it for class. I haven't programmed in four years so I'm forgetting a lot of the little things. Heres the problem I'm having right now.
I have a polynomial.h and a polynomial.cpp file so far. I'm attempting to compile the two in a project using bloodshed dev-c++. I don't have a main yet, I just want to see if they compile to discover if I've made any obvious errors etc etc.
The problem I'm getting is that when I include polynomial.h in the polynomial.cpp file I'm getting an error. It's saying "In file included from polynomial.cpp" and highlighting the include polynomial.h line.
Heres the two files that I have so far:
//polynomial.h
#ifndef POLYNOMIAL //if polynomial is not defined
#define POLYNOMIAL //define polynomial
#include <iostream> //include the input/output stream
#includ <vector> //include the vector class
using namespace std;
struct term {
int num;
int exp;
}
class polynomial
{
friend istream& operator>> (istream& stream, polynomial& poly); //polynomial has been read from input stream
friend ostream& operator<< (ostream& stream, polynomial& poly); //polynomial has been printed to output stream
protected:
vector<term> poly; //creates a vector of term structs
polynomial(); //default constructor, creates an empty polynomial
polynomial(int n, int e); //constructor, creates a polynomial where num is n and exp is e;
count_terms(); //counts the number of terms in the polynomial
reset(); //empties a polynomial
polynomial operator+ (polynomial &other_polynomial) //returns value of current polynomial added with another polynomial
//^^ possibly make this const?? ^^
};
#endif
//polynomial.cpp
#include <iostream>
#include <vector>
#include "polynomial.h"
using namespace std;
polynomial::polynomial()
{
reset(); //perhaps poly.reset? resets the polynomial
}
polynomial::polynomial(int n, int e)
{
poly.num.push_back(n);
poly.exp.push_back(e);
}
int polynomial::count_terms() //counts the number of terms in the polynomial
{
return poly.size();
}
reset(); //empties a polynomial
{
poly.clear();
}
// polynomial operator+ (polynomial &other_polynomial) //returns value of current polynomial added with another polynomial
Can anyone tell me why I am getting this error?
I have tried to rename polynomial.h to poly.h and change the include and define options but then it said that it couldnt find poly.h. They are in the same folder and they are part of the same project.
Sorry for such an amateur question, but I really don't know anyone who is a programmer.
I know there are errors elsewhere -- but I want to figure out what this means first.
Posts
I have no idea why I would be getting the error (or if it is even actually an error).
But it won't compile.
Also, you may want to write a simple main function in another file, include polynomial.h in it, and compile to see if it compiles with a main.
This is bad form and should be avoided.
http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5
Always place your class header file first in your implementation.
There's also no reason to re-include headers that have already appeared in your class header file. ( Typically you want to avoid including stuff in the header file, and placing includes in the implementation only. This isn't always possible and isn't something you should worry about for now.)
edit: actually, this shouldn't be a problem, so ignore it:
Now, the likely cause of the given error is here: You're missing a semicolon after your struct declaration.
Now I have to write a function for the struct to take in polynomials for the following code:
Now do I declare this in struct term part of the .h or the class polynomial part of the .h? Any ideas for a better way to do this? Where does the implementation go? I'm not asking you to do my homework for me, just point me in the right direction.
This isn't due until Tuesday, I'm just trying to get used to coding again.
Implementation goes inside the implementation file, unless you're using something like inline functions.
For the specific case of that line, think about it. Where does the object poly reside? polynomial of course. So it goes inside a polynomial member function. It would look similar to your constructor polynomial(int n, int e).
I think you're confusing your own class structure, as your constructor is off, too: The above code is incorrect and shouldn't compile. You need to access poly, because poly is the vector you're storing stuff in, using your own code:
Now poly.push_back should be basic vector stuff, so the only thing I need to implement to make this code work would be a term function that takes two variables... n and e. It adds a term to the back of the vector and the term consists of two parts. Do I declare this as a function of term or a function of polynomial? Heres my .h again:
And for consistency my .cpp
Heres the process (more for my sake then anyone elses)
I'm going to get 2 ints somehow from cin... a number and an exponent
I'm going to put those into polynomial(n, e)
polynomial(n,e) is going to call the vector of struct term poly and push_back (or add as the last item in the vector) a term with (n, e)
So somehow I have to implement this... since it seems like the only thing that has to be implemented is the term (n,e) part of the function it would seem to me that this has nothing to do with polynomial since even though the struct was declared in the .h file that is called polynomial.h and the polynomial class, the struct term was declared outside of the brackets for the polynomial class. So it would make sense to me that I would somehow have to declare the function inside the struct instead of the class.
Can I do this? Should I do this? Or is it ok to declare the function inside of the polynomial class brackets?
Also, I remember reading something about structs in that the way to assign a value to one part in the struct would be:
classname.structpart = blah blah
so in my case it would be something like
polynomial.num = n;
But I don't think that is correct..
man this is a bunch of rambling.. sorry being thrown into the deep end like this is really screwing with my brain its all over the place.
I think you're still confusing your struct with your class. I think you need to look up the difference between a polynomial and a term. polynomial is NOT a term, it is a group of terms (hence a vector of terms). Polynomial does not posses a "num" or a "exp". You need to write a function that accesses the poly vector and pulls out a term and returns it.
A member function in polynomial would look something like: which would create a term and store it into the vector (like your constructor) would retreive a term from the vector, using the term_index. This would require bounds checking. Or you can leave out the index parameter, and just pop the last term from your vector.
http://cplus.about.com/od/glossar1/g/accessor.htm
So without telling me exactly how to do it... should I use the function in conjuction with the code?
The reason I'm confused on the struct thing (other the being away from programming for so long) is when i look on wikipedia or something i see code like this:
which makes me think that I should be using the polynomial i declared in the .h file (poly) and inside the set_term function I should declare that poly.num = whatever.... however from what you are saying it seems like I would call the function with poly.set_term(n, e) and then set the term to something with term.num = blahblah.
I guess my problem is that I really don't see the logic where I would be actually adding the term to the end of the vector.
Which I suppose would be easier to test if I could figure out how to overload the input and output operators but thats a different demon entirely. :P
That wikiarticle code is just an example for something. In Object Oriented style, that code is absolute trash.
In your main function, you'd instance a polynomial, then use the polynomial's functions to assign terms and handle everything.