![]() |
|
#1
|
|||
|
|||
Need help with my C++ class (polynomial)I have a few different files so I will post them, then say the problem.
Here is my polynomial.h, it is the header file for my ADT CPP / C++ / C Code:
Here is my polynomial.cpp, which is where the constructors are defined CPP / C++ / C Code:
And here is my main file that tests it, test_polynomial.cpp It is just ment to make sure my polynomials initialize correctly...havn't done much since I can't get my class to work properly CPP / C++ / C Code:
Someone said my error might be in the polynomial.h, at the bottom under private, that I have a[], they said something about how I can't declare an initializer list? He was busy so he couldn't help me further. I am looking at a black hole right now...please assist me with fixing this program. Thanks in advance! Kris |
|
#2
|
|||
|
|||
Re: Need help with my C++ class (polynomial)Quote:
OK, so your friend is too busy. You can work through this on your own. Maybe someone here can help, but it may take some time to get a helpful response. First of all, forget what your busy friend said (in fact: maybe he's right, but forget that for now). What does your compiler say? It gave you some error messages, right? Different compilers have different ways of reporting errors, and sometimes the messages are confusing the first time you see them. Post the exact message the compiler gave you. Tell us what compiler you are using (sometimes it makes a difference for someone trying to help). Better yet, look at the message the compiler gave you. Then look in your book or class notes or whatever examples you can find. See if any of them have anything like the part of your program that the error message is referring to. If you don't understand what it's telling you, ask specific questions. Regards, Dave |
|
#3
|
|||
|
|||
Re: Need help with my C++ class (polynomial)Here is my error log:
Code:
I can't get rid of the "parse error before ]" and I have no idea what is wrong. I know that means the error is where I have my parameter as an array with no size specified, ie. someArray[], but I don't see anything wrong with that. And the other error, the "no matching function" is also confusing me. I have 3 constructors for my ADT, but it doesn't recognize it when I am passing a polynomial to it. Bah, I really am lost on these errors. |
|
#4
|
|||
|
|||
Re: Need help with my C++ class (polynomial)Quote:
"Parse error" means that the compiler has encountered something that is not a legal C++ language construct. Here is the context of the offending code: CPP / C++ / C Code:
Now, I am not going to look at any class definitions or anything else about what you are trying to do. Let's just get past the "parse error". The statement a[] = {0}; is not legal in any C++ context. Period. I'm don't care what you want to do here, I'm just telling you what the compiler has already told you: You can't do it this way. You could say something like CPP / C++ / C Code:
This is legal C++, and looks to me like this constructor gives you a polynomial of degree zero whose value is zero. (I think.) I don't know if this is what you had in mind, or whether it is useful in your application, but at least the compiler can get past this point. If this behavior is satisfactory, then compile again and go on to the next error. If not, then re-think and re-design and then re-compile. Regards, Dave |
|
#5
|
|||
|
|||
Re: Need help with my C++ class (polynomial)Okay, I fixed it as you said to, and I think that is what the other person was telling me about, he said something about how I couldn't use a list to define an array.
Anyways, I fixed it and I still get the exact same error messages, and am still clueless on what is wrong here...I've seriously spent like 2 hours just checking my program (which is pretty short, so yeah, i've examined and re-examined it). Anyone see something wrong that I can't pick out? |
|
#6
|
|||
|
|||
Re: Need help with my C++ class (polynomial)Quote:
Are you saying that you still get the "test_polynomial.cpp:17: error: parse error before `]' token" error after changing the first constructor? I didn't, bit I did get other fatal errors. Let's try something different: Look at your declaration for the class. The a[] thing is bothersome. I think that is trying to declare a zero-length array. Even if you could do that, I can't see where it would be useful. I'm guessing that you wanted to define the class so that different instances (objects) could be declared with different lengths. One way is to make the elements vectors of ints instead of arrays of ints. Another way is to use arrays of ints by making the member element a pointer to int, and then letting the constructor allocate storage for as much as it needs. (Another thing you could do is just to define the class with some fixed length array that is larger than you would ever need. But that's just ugly. We can do better.) Let's stick with arrays for now. Also, let's just to one constructor at a time, get it right, and move on. Also, just for now, let's make n and a public so that we can look at the innards of the objects without having to write a Print() member function. This is just temporary until we get things running. Here's the header: CPP / C++ / C Code:
Here's the Class definition file: CPP / C++ / C Code:
Here's the main program: CPP / C++ / C Code:
Now, try these together. Then, maybe the next thing is to make a Print() member function that prints out the degree of the polynomial (n) and the n+1 coefficients a[ i ], as i goes from zero to n. Then make the data members private as they should be, and go on to the next constructors. (The interesting ones.) My point is this: start with something simple that works. If it doesn't work, then it is simple enough that you can make it work. I know how frustrating it is when nothing works, and I also know how it feels when I get impatient and don't want to "waste time" doing something something so seemingly trivial. It takes some self-discipline to make myself take things one step at a time, but saves time in the end (for me at least). Then go on to the next things, one at a time. At each step test what you have done so far (and save a copy of the most recent successful file so that if further changes really screw it up, you have something to fall back on.) Regards, Dave |
|
#7
|
|||
|
|||
Re: Need help with my C++ class (polynomial)CPP / C++ / C Code:
CPP / C++ / C Code:
I have not yet learned what "a = new int[1]" does, because I havn't learned how to use pointers. I also dont know what you mean by that ~polynomial thing? Is it meant to erase the polynomial from memory? As for what these constructors are for: My professor always wants us to build all these useless functions, lots of the time we don't even use them. For instance, the first constructor I made is just supposed to make a zero polynomial....I won't even need to use it but he specifically asks for it. The 2nd one is to take an array of integers and a degree, and make a polynomial. The third one is to take a polynomial and copy it. I just read a bit about pointers and tried what you said, and I still am getting the same error messages... I am pretty much running in circles here Then in my print function I will have the part to change it from an array of say {1, 2, 3} with degree 2, to "x^2 + 2x + 3" and print that out. -Kris |
|
#8
|
|||
|
|||
Re: Need help with my C++ class (polynomial)Quote:
What error messages are you getting? What do you mean "the same error messages?" I gave you the header file the class definition file and the main program file. Are you saying that you compiled them together and you got "the same error messages" that you reported with your original code? I kind of doubt that. The example that I gave you should work without giving error or warning messages. I know how frustrating it is when you are just getting started, and, well you just can't get started. We will try to get one success under your belt, and you can build on that. So, let's move on... If you can't use pointers, then just make the array declaration fixed. The thing with the ~ in front of it is called a destructor. If you don't allocate storage in a constructor (that is if you don't use the new[] thingie), you might not need a destructor, but I find it hard to believe that Classes were presented as a topic without mention being made of constructors and destructors,... If you can't use pointers, then you obviously aren't ready for vectors, so try making the array declaration in the Class be a fixed length array. If the instructor wants you to do something different, then he will have to tell you (and you will have to tell whoever is trying to help you). I have given you an example that works (the idea was to get something that works), but I don't blame you for not wanting to use things that you haven't had and don't understand. That is a Good Thing for you to do (actually a Good Thing to not do). As far as "useless" functions: if you can get it to work (compile and execute without errors), then it's a useful exercise, even if you don't see any practical application. (And, by the way, I can think of times when I want a zero polynomial to be part of my toolkit, but that's kind of off the subject.) CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
Now, I respectfully suggest that you compile these together and execute the program before you try anything else. If you get errors from this attempt, be sure and tell us exactly what the error messages are. If you have had something different in class, then, after you get this simple example working, try whatever you think you need to do. If the results are still puzzling, then post the code and post the error messages or tell what bad behavior resulted, and tell us specifically it is that you don't understand. Regards, Dave |
|
#9
|
|||
|
|||
Re: Need help with my C++ class (polynomial)When I said I tried it and didn't work, I didn't put the destructor into my program, I just made the pointer and then added the a = new int[1] part, but anyways, lets move on.
I tried what you just told me to, and it compiled correctly. I just don't see any benefits in having the array set at a limit. I tried changing your program so that it doesn't have a certiain # in the polynomial.h for the size of the array, and then I get the following errors: Code:
I really would like to get this working without having a specified array size. My friends who are in this class both told me they didn't have any problems with theirs, and they said they didnt put a specific array size in theirs. |
|
#10
|
|||
|
|||
Re: Need help with my C++ class (polynomial)EDIT:
got the first 2 constructors to work, now will make the third. The problem was, that in the first one, where i had Polynomial poly1();, it worked for when I only had my 1 constructor, then when i made the 2nd one, it didnt work. Someone told me to take out the () and now it works. there is why C++ frustrates me beyond belief.. |
Recent GIDBlog
First week of IA training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Introduction to .NET | LuciWiz | .NET Forum | 5 | 09-Aug-2007 04:53 |
| polynomial program | jake_jeckel | CPP / C++ Forum | 0 | 29-Oct-2005 14:16 |
| Error C2146: syntax error : missing ',' before identifier 'C4' | mattchew008 | CPP / C++ Forum | 2 | 19-Dec-2004 06:06 |
| hashing help | saiz66 | CPP / C++ Forum | 1 | 06-Jul-2004 06:16 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The