GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Nov-2005, 01:20
krisopotamus krisopotamus is offline
New Member
 
Join Date: Sep 2005
Posts: 21
krisopotamus is on a distinguished road

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:
//polynomial.h
//

#ifndef __POLYNOMIAL_H__
#define __POLYNOMIAL_H__

class Polynomial
{
public:
  Polynomial();
  Polynomial(const int intArray[], const int numTerms);
  Polynomial(const Polynomial& p);

private:	//can't be changed
  int n;	//degree
  int a[];	//array of coefficients

};
#endif


Here is my polynomial.cpp, which is where the constructors are defined
CPP / C++ / C Code:
//polynomial.cpp
//

#include <iostream>
#include <cassert>
#include "polynomial.h"

using namespace std;

Polynomial::Polynomial()
{
  n = 0;
  a[] = {0};
}

Polynomial::Polynomial(const int intArray[], const int numTerms)
{
  n = numTerms;
  int i = 0;
  for(; i < numTerms; i++)
    a[i] = intArray[i];
  assert(a[i] != 0);
}

Polynomial::Polynomial(const Polynomial& p)
{
  n = p.n;
  int i = 0
  for(; i < p.n; i++)
    a[i] = p.n[i];
  assert(a[i] != 0);
}

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:
// test_polynomial.cpp
//

#include <iostream>
#include "polynomial.h"
#include "managed_array.h"
#include "guarded_array.h"

using namespace std;

int main() 
{
  int coeff[] = {1, 2, 3, 4};
  Polynomial poly1();
  Polynomial poly2(coeff[], 4);
  Polynomial poly3(poly2);
return 0;
} 



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  
Old 01-Nov-2005, 06:25
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,621
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need help with my C++ class (polynomial)


Quote:
Originally Posted by krisopotamus
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.

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  
Old 01-Nov-2005, 09:59
krisopotamus krisopotamus is offline
New Member
 
Join Date: Sep 2005
Posts: 21
krisopotamus is on a distinguished road

Re: Need help with my C++ class (polynomial)


Here is my error log:

Code:
hercules[2]% make clean check rm -fr *.o *.out test_polynomial g++ -c test_polynomial.cpp test_polynomial.cpp: In function `int main()': test_polynomial.cpp:17: error: parse error before `]' token test_polynomial.cpp:20: error: no matching function for call to `Polynomial:: Polynomial(Polynomial (&)(...))' polynomial.h:12: error: candidates are: Polynomial::Polynomial(const Polynomial&) polynomial.h:11: error: Polynomial::Polynomial(const int*, int) polynomial.h:10: error: Polynomial::Polynomial() *** Error code 1 make: Fatal error: Command failed for target `test_polynomial.o'

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  
Old 01-Nov-2005, 10:54
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,621
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need help with my C++ class (polynomial)


Quote:
Originally Posted by krisopotamus
Here is my error log:

Code:
hercules[2]% make clean check rm -fr *.o *.out test_polynomial g++ -c test_polynomial.cpp test_polynomial.cpp: In function `int main()': test_polynomial.cpp:17: error: parse error before `]' token

I can't get rid of the "parse error before ]"

"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:
Polynomial::Polynomial()
{
  n = 0;
  a[] = {0};
}

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:
Polynomial::Polynomial()
{
  n = 0;
  a[0] = 0;
}


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  
Old 01-Nov-2005, 15:31
krisopotamus krisopotamus is offline
New Member
 
Join Date: Sep 2005
Posts: 21
krisopotamus is on a distinguished road

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  
Old 01-Nov-2005, 16:33
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,621
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need help with my C++ class (polynomial)


Quote:
Originally Posted by krisopotamus
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?

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:
//polynomial.h
//

#ifndef POLYNOMIAL_H__
#define POLYNOMIAL_H__

class Polynomial
{
public:
  Polynomial();
  ~Polynomial();

//private:	//temporarily public for debugging
  int n;	//number of coefficients
  int *a;	//Point to array of coefficients

};
#endif

Here's the Class definition file:
CPP / C++ / C Code:
//polynomial.cpp
//

#include <iostream>
#include "polynomial.h"

using namespace std;
Polynomial::Polynomial()
{
  n = 0; // degree of polynomial
  a = new int[1];
  a[0] = 0; // initial value of coefficient
}

Polynomial::~Polynomial()
{
  delete [] a;
}

Here's the main program:

CPP / C++ / C Code:
#include <iostream>
#include "polynomial.h"

using namespace std;

int main() 
{

  Polynomial poly1;
  cout << "Here's poly1:" << endl;
  cout << "poly1.n    = " << poly1.n << endl;
  cout << "poly1.a[0] = " << poly1.a[0] << endl;

  return 0;
}

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  
Old 01-Nov-2005, 16:53
krisopotamus krisopotamus is offline
New Member
 
Join Date: Sep 2005
Posts: 21
krisopotamus is on a distinguished road

Re: Need help with my C++ class (polynomial)


CPP / C++ / C Code:
class Polynomial
{
public:
  Polynomial();
  ~Polynomial();
and
CPP / C++ / C Code:
Polynomial::Polynomial()
{
  n = 0; // degree of polynomial
  a = new int[1];
  a[0] = 0; // initial value of coefficient
}

Polynomial::~Polynomial()
{
  delete [] a;
}

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  
Old 01-Nov-2005, 17:33
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,621
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need help with my C++ class (polynomial)


Quote:
Originally Posted by krisopotamus
[
I just read a bit about pointers and tried what you said, and I still am getting the same error messages...
-Kris
What code are you running?
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:
//polynomial.h
//

#ifndef POLYNOMIAL_H__
#define POLYNOMIAL_H__

class Polynomial
{
public:
  Polynomial();

//private:	//temporarily public for debugging
  int n;	//degree of polynomial
  int a[100];	//can never have a polynomial greater than degree 99

};
#endif

CPP / C++ / C Code:
//polynomial.cpp
//

#include <iostream>
#include "polynomial.h"

using namespace std;
Polynomial::Polynomial()
{
  n = 0; // degree of polynomial
  a[0] = 0;
}

CPP / C++ / C Code:
#include <iostream>
#include "polynomial.h"

using namespace std;

int main() 
{

  Polynomial poly1;
  cout << "Here's poly1:" << endl;
  cout << "poly1.n    = " << poly1.n << endl;
  cout << "poly1.a[0] = " << poly1.a[0] << endl;
  return 0;
}

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  
Old 01-Nov-2005, 18:09
krisopotamus krisopotamus is offline
New Member
 
Join Date: Sep 2005
Posts: 21
krisopotamus is on a distinguished road

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:
test_polynomial.cpp: In function `int main()': test_polynomial.cpp:11: error: request for member `n' in `poly1()', which is of non-class type `Polynomial ()()' test_polynomial.cpp:12: error: request for member `a' in `poly1()', which is of non-class type `Polynomial ()()' *** Error code 1 make: Fatal error: Command failed for target `test_polynomial.o'

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  
Old 01-Nov-2005, 19:13
krisopotamus krisopotamus is offline
New Member
 
Join Date: Sep 2005
Posts: 21
krisopotamus is on a distinguished road

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 GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 01:37.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.