The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

basic C++ help (include error??)

GrundlterrorGrundlterror Registered User regular
edited February 2008 in Help / Advice Forum
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.

steam_sig.png
Grundlterror on

Posts

  • Jimmy KingJimmy King Registered User regular
    edited February 2008
    Is it saying that the error is in the file polynomial.h included in polynomial.cpp? If that's what's going on, my guess is this line.
    #includ <vector> //include the vector class
    

    Jimmy King on
  • GrundlterrorGrundlterror Registered User regular
    edited February 2008
    Sorry, I should have updated that, heres my code now:
    //polynomial.h
    
    #ifndef POLYNOMIAL //if polynomial is not defined
    #define POLYNOMIAL //define polynomial
    
    #include <iostream> //include the input/output stream
    #include <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;
          int count_terms(); //counts the number of terms in the polynomial
          void 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)
    {
         polynomial.num.push_back(n);
         polynomial.exp.push_back(e);
    }
    
    int polynomial::count_terms() //counts the number of terms in the polynomial
    {
         return poly.size();
    }
    
    void reset() //empties a polynomial
    {
         poly.clear();
    }
    //     polynomial operator+ (polynomial &other_polynomial) //returns value of current polynomial added with 
    
    another polynomial
    

    I have no idea why I would be getting the error (or if it is even actually an error).

    But it won't compile.

    Grundlterror on
    steam_sig.png
  • DaenrisDaenris Registered User regular
    edited February 2008
    well... for one you're double including the iostream and vector files which may cause a problem... because they're included in both polynomial.h and polynomial.cpp. Does the error actually say anything other than that it's an error?

    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.

    Daenris on
  • falsedeffalsedef Registered User regular
    edited February 2008
    First off, some problems with your code:
    using namespace std;
    
    This is bad form and should be avoided.
    http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5

    #include <iostream>
    #include <vector>
    #include "polynomial.h"
    
    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:
    polynomial operator+ (polynomial &other_polynomial);
    
    I'm assuming you meant the binary operator:
    friend polynomial operator+( const polynomial & a, const polynomial  & b );
    



    Now, the likely cause of the given error is here:
    struct term
    {
          int num;
          int exp;
    }
    
    You're missing a semicolon after your struct declaration.

    falsedef on
  • GrundlterrorGrundlterror Registered User regular
    edited February 2008
    Not sure what I did, but I've managed to get rid of that original error after commenting out a bunch of functions that I haven't implemented yet. Thanks for all the advice in this thread.

    Now I have to write a function for the struct to take in polynomials for the following code:
    poly.push_back( term (n,e));
    

    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.

    Grundlterror on
    steam_sig.png
  • falsedeffalsedef Registered User regular
    edited February 2008
    Not sure what I did, but I've managed to get rid of that original error after commenting out a bunch of functions that I haven't implemented yet. Thanks for all the advice in this thread.

    Now I have to write a function for the struct to take in polynomials for the following code:
    poly.push_back( term (n,e));
    

    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:
    {
        polynomial.num.push_back(n);
        polynomial.exp.push_back(e);
    }
    
    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:
    poly.push_back( term (n,e));
    

    falsedef on
  • GrundlterrorGrundlterror Registered User regular
    edited February 2008
    the poly.push_back( term (n,e)); function has replaced the code that wouldnt compile.

    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:
    //polynomial.h
    
    #ifndef POLYNOMIAL //if polynomial is not defined
    #define POLYNOMIAL //define polynomial
    
    #include <iostream> //include the input/output stream
    #include <vector> //include the vector class
    struct term 
    {
           int num;
           int exp;
    };
    
    class polynomial
    {
    //      friend std::istream& operator>> (istream& stream, polynomial& poly); //polynomial has been read from input stream
    //      friend std::ostream& operator<< (ostream& stream, polynomial& poly); //polynomial has been printed to output stream
          friend polynomial operator+( const polynomial & a, const polynomial  & b ); //returns value of current polynomial added with another polynomial
           
          protected:
              std::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;
          int count_terms() const; //counts the number of terms in the polynomial
          void reset(); //empties a polynomial
             
    };
    
    #endif
    

    And for consistency my .cpp
    //polynomial.cpp
    
    #include "polynomial.h"
    
    polynomial::polynomial()
    {                     
         reset(); //perhaps poly.reset? resets the polynomial
    }
    
    polynomial::polynomial(int n, int e)
    {
         poly.push_back( term (n,e));
    }
    
    int polynomial::count_terms() const //counts the number of terms in the polynomial
    {
         return poly.size();
    }
    
    void reset() //empties a polynomial
    {
         poly.term.clear();
    }
    //     polynomial operator+ (polynomial &other_polynomial) //returns value of current polynomial added with another polynomial
    
    [/spoiler]
    

    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.

    Grundlterror on
    steam_sig.png
  • falsedeffalsedef Registered User regular
    edited February 2008
    Structs are used in C++ mostly for storage. They don't need any accessors (set and get functions) because everything in a struct is public. Think of them as groups of variables. You can add functions to structs, but in your case, there would be little to add at the moment.

    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.num = n;
    
    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:
    void set_term( int num, int exp );
    
    which would create a term and store it into the vector (like your constructor)
    term get_term( int term_index );
    
    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

    falsedef on
  • GrundlterrorGrundlterror Registered User regular
    edited February 2008
    Wow, thank you so much you've been so helpful!

    So without telling me exactly how to do it... should I use the
    poly.push_back( term (n,e));
    
    function in conjuction with the
    void set_term(int num, int exp);
    
    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:
    #include <iostream>
    #include <string>
    using namespace std;
     
    class person
    {
    public:
      string name;
      int age;
    };
     
    int main ()
    {
      person a, b;
      a.name = "Calvin";
      b.name = "Hobbes";
      a.age = 30;
      b.age = 20;
      cout << a.name << ": " << a.age << endl;
      cout << b.name << ": " << b.age << endl;
      return 0;
    }
    

    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

    Grundlterror on
    steam_sig.png
  • falsedeffalsedef Registered User regular
    edited February 2008
    Wow, thank you so much you've been so helpful!

    So without telling me exactly how to do it... should I use the
    poly.push_back( term (n,e));
    
    function in conjuction with the
    void set_term(int num, int exp);
    
    code?
    Yes, however you should rename the function to something like "add_term" as a more sensible name.
    #include <iostream>
    #include <string>
    using namespace std;
     
    class person
    {
    public:
      string name;
      int age;
    };
     
    int main ()
    {
      person a, b;
      a.name = "Calvin";
      b.name = "Hobbes";
      a.age = 30;
      b.age = 20;
      cout << a.name << ": " << a.age << endl;
      cout << b.name << ": " << b.age << endl;
      return 0;
    }
    
    That wikiarticle code is just an example for something. In Object Oriented style, that code is absolute trash.
    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.

    In your main function, you'd instance a polynomial, then use the polynomial's functions to assign terms and handle everything.

    falsedef on
  • GrundlterrorGrundlterror Registered User regular
    edited February 2008
    Awesome, falsedef you have been so amazingly helpful! Thank you!!!!

    Grundlterror on
    steam_sig.png
Sign In or Register to comment.