473,748 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compile time error in overloading '+' operator

45 New Member
Can anyone please tell me how can I rectify my compile time error in this program.
Expand|Select|Wrap|Line Numbers
  1. /********************************HEADER FILE***********************************/
  2.  
  3. #ifndef POLYNOMIAL_H            // conditional compilation
  4. #define POLYNOMIAL_H
  5.  
  6.  
  7. # include <iostream>
  8. using namespace std;
  9.  
  10. class polynomial   // rational class
  11.  
  12. {
  13.  
  14. public :
  15.  
  16.     // Constructors
  17.  
  18.     polynomial(); // default constructor
  19.  
  20.     polynomial( int sizeValue, double* coeffArrayValues );/* this should take the size of the polynomial  
  21.                                          and should prompt the user to enter the value
  22.                                          ( depending on the size ) and also store all 
  23.                                          the values in the coeff array*/
  24.  
  25.  
  26.     polynomial( polynomial& copy );/* here u r passing the address of the polynomial so 
  27.                                       that u can save different polynomials : copy constructor */
  28.  
  29.     void display( ) ;
  30.  
  31.     const polynomial add(const polynomial& otherPoly ) const;
  32.     const polynomial difference(const polynomial& otherPoly ) const;
  33.     const polynomial multiply(const polynomial& otherPoly ) const;
  34.     polynomial& operator =( const polynomial& expression );
  35.     ~polynomial( );
  36.  
  37. public:
  38.  
  39.     int size;
  40.     double *coeffArray; 
  41. };
  42.  
  43. polynomial n_add( polynomial poly1, polynomial poly2 );
  44. polynomial n_difference( polynomial poly1, polynomial poly2 );
  45. polynomial n_multiply( polynomial poly1, polynomial poly2 );
  46.  
  47.  
  48. polynomial operator +( polynomial ploy1, polynomial poly2 );
  49. polynomial operator -( polynomial ploy1, polynomial poly2 );
  50. polynomial operator *( polynomial ploy1, polynomial poly2 );
  51. void n_display( polynomial result );
  52. // End of class interface.
  53.  
  54. #endif
  55.  
  56. /*********************IMPLEMENTATION****************************/
  57.  
  58. #include <iostream>
  59. #include <cmath>
  60. #include "polynomial.h"
  61. using namespace std;
  62.  
  63. polynomial::polynomial()
  64.  
  65. {
  66.     //initializing the size of the array to 10.
  67.     size  = 0;
  68.     //creating the dynamic array.
  69.     coeffArray = NULL;
  70.  
  71. }
  72.  
  73. polynomial::polynomial( int sizeValue, double* coeffArrayValues )// here u can also give double coeffArrayValues[]
  74.  
  75. {
  76.  
  77.     sizeValue = size;
  78.     coeffArray = new double[ sizeValue ];
  79.  
  80.     for( int initial = sizeValue; sizeValue > 0; sizeValue-- )
  81.  
  82.     {
  83.  
  84.         cout<<"Please enter x^";
  85.         cout<<sizeValue;
  86.         cout<<" coefficient : ";
  87.         cin>>coeffArray[ sizeValue ];
  88.         cout<<endl;
  89.  
  90.     }
  91.  
  92. }
  93.  
  94. polynomial::polynomial( polynomial& copy )
  95.  
  96. {
  97.  
  98.     copy.size = size;
  99.     copy.coeffArray = new double[ copy.size ];
  100.  
  101. }
  102.  
  103.  
  104. const polynomial polynomial::add(const polynomial& otherPoly ) const
  105.  
  106. {
  107.     int localSize;
  108.  
  109.     if( otherPoly.size > this->size )// also you can use *this in place of 'this->'
  110.         localSize = otherPoly.size ;
  111.  
  112.     else
  113.         localSize = this->size ;
  114.  
  115.     double *resultCoeff;
  116.  
  117.     resultCoeff = new double[ localSize ];
  118.  
  119.     for( int count1 = 0; count1 < localSize; count1++ )
  120.         resultCoeff[ count1 ] = 0;
  121.  
  122.     polynomial poly( localSize, resultCoeff );//to store the result sum
  123.  
  124.     for( int count2 = 0; count2 < localSize; count2++ )
  125.         resultCoeff[ count2 ] = resultCoeff[ count2 ] + this->coeffArray[ count2 ];
  126.  
  127.     for( int count3 = 0; count3 < localSize; count3++ )
  128.         resultCoeff[ count3 ] = resultCoeff[ count3 ] + otherPoly.coeffArray[ count3 ];
  129.  
  130.     return polynomial( size, resultCoeff );
  131.  
  132. }
  133.  
  134. const polynomial polynomial::difference(const polynomial& otherPoly ) const
  135.  
  136. {
  137.     int localSize;
  138.  
  139.     if( otherPoly.size > this->size )// also you can use *this in place of 'this->'
  140.         localSize = otherPoly.size ;
  141.  
  142.     else
  143.         localSize = this->size ;
  144.  
  145.     double *resultCoeff;
  146.  
  147.     resultCoeff = new double[ localSize ];
  148.  
  149.     for( int count1 = 0; count1 < localSize; count1++ )
  150.         resultCoeff[ count1 ] = 0;
  151.  
  152.     polynomial poly( localSize, resultCoeff );//to store the result sum
  153.  
  154.     for( int count2 = 0; count2 < this->size; count2++ )
  155.         resultCoeff[ count2 ] = resultCoeff[ count2 ] + this->coeffArray[ count2 ];
  156.  
  157.     for( int count3 = 0; count3 < localSize; count3++ )
  158.         resultCoeff[ count3 ] = resultCoeff[ count3 ] - otherPoly.coeffArray[ count3 ];
  159.  
  160.     return polynomial( size, resultCoeff );
  161.  
  162. }
  163.  
  164. const polynomial polynomial::multiply(const polynomial& otherPoly ) const
  165.  
  166. {
  167.     int localSize;
  168.  
  169.     if( otherPoly.size > this->size )// also you can use *this in place of 'this->'
  170.         localSize = otherPoly.size ;
  171.  
  172.     else
  173.         localSize = this->size ;
  174.  
  175.     double *resultCoeff;
  176.  
  177.     resultCoeff = new double[ localSize ];
  178.  
  179.  
  180.     for( int count1 = 0; count1 < localSize; count1++ )
  181.         resultCoeff[ count1 ] = 0;
  182.  
  183.     polynomial poly( localSize, resultCoeff );//to store the result 
  184.  
  185.     for( int count2 = 0; count2 < localSize; count2++ )
  186.         resultCoeff[ count2 ] = resultCoeff[ count2 ] + this->coeffArray[ count2 ];
  187.  
  188.     for( int count3 = 0; count3 < localSize; count3++ )
  189.         resultCoeff[ count3 ] = resultCoeff[ count3 ] * otherPoly.coeffArray[ count3 ];
  190.  
  191.     return polynomial( size, resultCoeff );
  192.  
  193. }
  194.  
  195. void polynomial::display()
  196.  
  197. {
  198.  
  199.     if( this->size > 0 )
  200.  
  201.     {
  202.  
  203.         for( int i = ( this->size - 1 ); i >=0 ; i-- )
  204.  
  205.         {
  206.  
  207.             if( this->coeffArray[ i ] != 0 )
  208.  
  209.             {
  210.  
  211.                 cout<<"resultant polygon = ";
  212.                 cout<<this->coeffArray[ i ];
  213.                 cout<<"x^"<<i;
  214.  
  215.                 if( i != 0 )
  216.                     cout<<"+";
  217.  
  218.             }
  219.  
  220.         }
  221.  
  222.     }
  223.  
  224.     else
  225.  
  226.         cout<<"resultant polygon = 0";
  227.  
  228. }
  229.  
  230.  
  231. polynomial& polynomial::operator =( const polynomial& otherPoly )
  232.  
  233. {
  234.  
  235.     if( this->size != otherPoly.size ) //(u can also give only size )
  236.  
  237.     {
  238.  
  239.         delete [] coeffArray;
  240.  
  241.         coeffArray = new double[ otherPoly.size ];
  242.  
  243.     }
  244.  
  245.     this->size = otherPoly.size;
  246.  
  247.     for( int count = 0; count < this->size; count++ )
  248.         coeffArray[ count ] = otherPoly.coeffArray[ count ];
  249.  
  250.     return *this;
  251.  
  252. }
  253.  
  254. polynomial::~polynomial( )
  255.  
  256. {
  257.     delete [] coeffArray;
  258. }
  259.  
  260. /*****************NON-MEMBER FUNCTIONS******************/
  261.  
  262. polynomial n_add( polynomial poly1, polynomial poly2 )
  263.  
  264. {
  265.  
  266.     int localSize;
  267.  
  268.     if( poly1.size > poly2.size )
  269.         localSize = poly1.size;
  270.  
  271.     else
  272.         localSize = poly2.size;
  273.  
  274.     double *resultCoeff;
  275.     resultCoeff = new double[ localSize ];
  276.  
  277.     for( int count = 0; count < localSize; count++ )
  278.         resultCoeff[ count ] = 0;
  279.  
  280.     for( int count1 = 0; count1 < localSize; count1++ )
  281.         resultCoeff[ count1 ] = resultCoeff[ count1 ] + poly1.coeffArray[ count1 ];
  282.  
  283.     for( int count2 = 0; count2 < localSize; count2++ )
  284.         resultCoeff[ count2 ] = resultCoeff[ count2 ] + poly2.coeffArray[ count2 ];
  285.  
  286.     return polynomial( localSize, resultCoeff );
  287.  
  288. }
  289.  
  290. polynomial n_multiply( polynomial poly1, polynomial poly2 )
  291.  
  292. {
  293.  
  294.     int localSize;
  295.  
  296.     if( poly1.size > poly2.size )
  297.         localSize = poly1.size;
  298.  
  299.     else
  300.         localSize = poly2.size;
  301.  
  302.     double *resultCoeff;
  303.     resultCoeff = new double[ localSize ];
  304.  
  305.     for( int count = 0; count < localSize; count++ )
  306.         resultCoeff[ count ] = 0;
  307.  
  308.     for( int count1 = 0; count1 < localSize; count1++ )
  309.         resultCoeff[ count1 ] = resultCoeff[ count1 ] + poly1.coeffArray[ count1 ];
  310.  
  311.     for( int count2 = 0; count2 < localSize; count2++ )
  312.         resultCoeff[ count2 ] = resultCoeff[ count2 ] * poly2.coeffArray[ count2 ];
  313.  
  314.     return polynomial( localSize, resultCoeff );
  315.  
  316. }
  317.  
  318. polynomial n_difference( polynomial poly1, polynomial poly2 )
  319.  
  320. {
  321.  
  322.     int localSize;
  323.  
  324.     if( poly1.size > poly2.size )
  325.         localSize = poly1.size;
  326.  
  327.     else
  328.         localSize = poly2.size;
  329.  
  330.     double *resultCoeff;
  331.     resultCoeff = new double[ localSize ];
  332.  
  333.     for( int count = 0; count < localSize; count++ )
  334.         resultCoeff[ count ] = 0;
  335.  
  336.     for( int count1 = 0; count1 < localSize; count1++ )
  337.         resultCoeff[ count1 ] = resultCoeff[ count1 ] + poly1.coeffArray[ count1 ];
  338.  
  339.     for( int count2 = 0; count2 < localSize; count2++ )
  340.         resultCoeff[ count2 ] = resultCoeff[ count2 ] - poly2.coeffArray[ count2 ];
  341.  
  342.     return polynomial( localSize, resultCoeff );
  343.  
  344. }
  345.  
  346. polynomial operator +( polynomial ploy1, polynomial poly2 )
  347.  
  348. {
  349.     return n_add( poly1, poly2 );
  350. }
  351.  
  352. polynomial operator -( polynomial ploy1, polynomial poly2 )
  353.  
  354. {
  355.     return n_difference( poly1, poly2 );
  356. }
  357.  
  358. polynomial operator *( polynomial ploy1, polynomial poly2 )
  359.  
  360. {
  361.     return n_multiply( poly1, poly2 );
  362. }
  363.  
  364. void n_display( polynomial result )
  365.  
  366. {
  367.  
  368.     if( result.size > 0 )
  369.  
  370.     {
  371.  
  372.         for( int i = ( result.size - 1 ); i >=0 ; i-- )
  373.  
  374.         {
  375.  
  376.             if( result.coeffArray[ i ] != 0 )
  377.  
  378.             {
  379.  
  380.                 cout<<"resultant polygon = ";
  381.                 cout<<result.coeffArray[ i ];
  382.                 cout<<"x^"<<i;
  383.  
  384.                 if( i != 0 )
  385.                     cout<<"+";
  386.  
  387.             }
  388.  
  389.         }
  390.  
  391.     }
  392.     else
  393.  
  394.         cout<<"resultant polygon = 0";
  395. }
I am getting an error : " error C2065: 'poly1' : undeclared identifier ".

How to solve it?

Thanks in advance.
Apr 15 '07 #1
2 2083
Savage
1,764 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. polynomial operator *( polynomial ploy1, polynomial poly2 )
You have declared ploy1 instead of poly1.

Savage
Apr 15 '07 #2
KiranJyothi
45 New Member
Expand|Select|Wrap|Line Numbers
  1. polynomial operator *( polynomial ploy1, polynomial poly2 )
You have declared ploy1 instead of poly1.

Savage
Thank You for the reply. I finished it and submitted.
Apr 19 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
4103
by: Laxman | last post by:
==C.H <== #include <fstream.h> class C { public: C(int i); friend ofstream& operator<<(ofstream& os, const C&); }; ------------------------------------------------------------ ==B.H <==
6
3331
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for my particular application. One of the advantages is that the _compiler_ can force inner matrix dimensions used in multiplication to agree. A _complie-time_ error will be triggered if you write A * B and the number of coluns in A does not equal the...
5
2147
by: xuatla | last post by:
Hi, I encountered the following compile error of c++ and hope to get your help. test2.cpp: In member function `CTest CTest::operator+=(CTest&)': test2.cpp:79: error: no match for 'operator=' in '*this = CTest::operator+(CTest&)((+t2))' test2.cpp:49: error: candidates are: CTest CTest::operator=(CTest&) make: *** Error 1
4
2098
by: user | last post by:
I am trying to port a simple vector/matrix class library to mingw (v3.4.2). I have used this library with no problem on MSVC++ v6 for a number of years. My difficulty is that it does not compile under g++! Two issues: Firstly, if I define x to be a CColumnVector, y to be a CRowVector and M to be a CMatrix (of the right size!) then: M = x * y;
8
2594
by: andrew browning | last post by:
I am getting an istream overloading error that looks like this: error: no match for 'operator>>' in 'ins >> target->abrowning_rational::Rational::numerator' Below is the .h file: #ifndef RATIONAL #define RATIONAL #include <iostream>
15
4848
by: steve yee | last post by:
i want to detect if the compile is 32 bits or 64 bits in the source code itself. so different code are compiled respectively. how to do this?
11
3819
by: jakester | last post by:
I am using Visual C++ 2007 to build the code below. I keep getting linkage error. Could someone please tell me what I am doing wrong? The code works until I start using namespace for my objects. Error 1 error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char & __cdecl graph::operator<<(class std::basic_ostream<char,struct std::char_traits<char &,class graph::Node &)" (??6graph@@YAAAV?...
22
3620
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float v);
4
2262
by: Chris | last post by:
Hello all, Using g++ 3.3.1 on Linux ("Linux From Scratch") I have a data structure SCSIParams_t that I wish to print out, field-by-field. Rather than code a long line of the form std::cout << "fieldname = " << basepointer->fieldname << std::endl; for every field, I'd like to do it with a short macro, e.g.
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9381
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9332
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8252
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.