Solved

What do these errors mean when I try and compile my program.

Posted on 2004-03-23
6
257 Views
Last Modified: 2010-04-01
$ g++ -c ass1.cpp StockItem.cpp SalesRegister.cpp

ass1.cpp: In function `int main(int, char**)':
ass1.cpp:40: error: no matching function for call to `SalesRegister::
   SalesRegister(SalesRegister)'
SalesRegister.h:16: error: candidates are:
   SalesRegister::SalesRegister(SalesRegister&)
SalesRegister.h:15: error:
   SalesRegister::SalesRegister(std::basic_string<char, std::char_traits<char>,

   std::allocator<char> >, int, int, int)

Here are the contents of the SalesRegister.h
--------------------------------------------------------------------------------------------

#include <string>
using namespace std;
#include "StockItem.h"

class SalesRegister
{
      private:
            string Description;
            int TotalCostPrice;
            int TotalSellPrice;
            int ItemCount;

      public:
            SalesRegister(string Description, int TotalCostPrice, int TotalSellPrice, int ItemCount);
            SalesRegister(SalesRegister &Source);
            SalesRegister operator+(SalesRegister &Parameter);
            void RecordSale(StockItem &Item);
            void PrintReport();
            string GetDescription();
            int GetTotalCostPrice();
            int GetTotalSellPrice();
            int GetItemCount();
            void SetDescription(string NewDescription);
            void SetTotalCostPrice(int NewTotalCostPrice);
            void SetTotalSellPrice(int NewTotalSellPrice);
            void SetItemCount(int NewItemCount);
};
-------------------------------------------------------------------------------------------------------------

Here is the SalesRegister.cpp file
---------------------------------------------------------------------------------------------------------------
#include "SalesRegister.h"
#include <iomanip>
#include <iostream>
using namespace std;

SalesRegister::SalesRegister(string Description, int TotalCostPrice, int TotalSellPrice, int ItemCount)
{
      Description = Description;
      TotalCostPrice = TotalCostPrice;
      TotalSellPrice = TotalSellPrice;
      ItemCount = ItemCount;
}

SalesRegister::SalesRegister(SalesRegister &Source)
{
      Description = Source.GetDescription();
      TotalCostPrice = Source.GetTotalCostPrice();
      TotalSellPrice = Source.GetTotalSellPrice();
      ItemCount = Source.GetItemCount();
}

SalesRegister SalesRegister::operator +(SalesRegister &Parameter)
{
      SalesRegister temp(Parameter);
      temp.TotalCostPrice = this->TotalCostPrice + Parameter.TotalCostPrice;
      temp.TotalSellPrice = this->TotalSellPrice + Parameter.TotalSellPrice;
      return (temp);
}

void SalesRegister::RecordSale(StockItem &Item)
{
      cout << "$" << Item.GetSellPrice() << Item.GetAuthor() << Item.GetTitle() << Item.GetPublisher() << Item.GetYear() << endl;
      
      SetTotalCostPrice(GetTotalCostPrice() + Item.GetCostPrice());
      SetTotalSellPrice(GetTotalSellPrice() + Item.GetSellPrice());
      ItemCount = ItemCount + 1;
}

void SalesRegister::PrintReport()
{
      float Profit = 0;

      Profit = ((TotalSellPrice - TotalCostPrice) / ItemCount);
      
      cout << "Report for " << Description << endl;
      cout << "\t Total Sales:\t\t$" << TotalSellPrice << endl;
      cout << "\t Total Cost:\t\t$" << TotalSellPrice << endl;
      cout.setf(ios::fixed);
      cout.setf(ios::showpoint);
      cout << "\t Average Profit:\t$" << setprecision(2) << Profit << endl;

}

string SalesRegister::GetDescription()
{
      return (Description);
}

int SalesRegister::GetTotalCostPrice()
{
      return (TotalCostPrice);
}

int SalesRegister::GetTotalSellPrice()
{
      return (TotalSellPrice);
}

int SalesRegister::GetItemCount()
{
      return (ItemCount);
}

void SalesRegister::SetDescription(string NewDescription)
{
      Description = NewDescription;
}

void SalesRegister::SetTotalCostPrice(int NewTotalCostPrice)
{
      TotalCostPrice = NewTotalCostPrice;
}

void SalesRegister::SetTotalSellPrice(int NewTotalSellPrice)
{
      TotalSellPrice = NewTotalSellPrice;
}

void SalesRegister::SetItemCount(int NewItemCount)
{
      ItemCount = NewItemCount;
}
-----------------------------------------------------------------------------------------------------

Here is the ass1.cpp file
-----------------------------------------------------------------------------------------------------------
#include <iostream>
#include <string>

using namespace std;

#include "SalesRegister.h"

int main(int argc, char *argv[])
{
      StockItem text1("W. Savitch", "Absolute C++ 1/e", "Addison/Wesley", 2002, 75, 90);
      StockItem text2("L. A. Robertson", "Simple Program Design 4/e", "Thomson", 2003, 70, 80);
      StockItem text3("A. Silberschatz, P. B. Galvin, and G. Gagne", "Operating System Concepts 6/e", "John Wiley & Sons", 2003, 85, 120);

      SalesRegister monday("Monday March 1, 2004", 0, 0, 0);
      cout << monday.GetDescription() << endl;
      monday.RecordSale(text1);
      monday.RecordSale(text1);

      SalesRegister tuesday("Tuesday March 2, 2004", 0, 0, 0);
      cout << endl << tuesday.GetDescription() << endl;
      tuesday.RecordSale(text3);
      tuesday.RecordSale(text2);
      tuesday.RecordSale(text1);

      SalesRegister wednesday("Wednesday March 2, 2004", 0, 0, 0);
      cout << endl << wednesday.GetDescription() << endl;
      wednesday.RecordSale(text1);
      wednesday.RecordSale(text3);

      SalesRegister thursday("Thursday March 2, 2004", 0, 0, 0);
      cout << endl << thursday.GetDescription() << endl;
      thursday.RecordSale(text3);
      thursday.RecordSale(text2);

      SalesRegister friday("Friday March 2, 2004", 0, 0, 0);
      cout << endl << friday.GetDescription() << endl;
      friday.RecordSale(text1);
      friday.RecordSale(text2);

      SalesRegister total(monday + tuesday + wednesday + thursday + friday);
      total.SetDescription("Total Sales");

      monday.PrintReport();
      tuesday.PrintReport();
      wednesday.PrintReport();
      thursday.PrintReport();
      friday.PrintReport();
      total.PrintReport();

      return(0);
}
----------------------------------------------------------------------------------------------------------------

















0
Comment
Question by:Broken_Arrow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 250 total points
ID: 10664480
1) ass1.cpp: In function `int main(int, char**)':

This is a warning


2) ass1.cpp:40: error: no matching function for call to `SalesRegister::  SalesRegister(SalesRegister)'
This happens at the following line
        SalesRegister total(monday + tuesday + wednesday + thursday + friday);
Here, your operator+ function comes into picture and returns a temporary object after addition of all the days' objects
Since temporaries in C++ are always const, thus the compiler cannot use the Copy Constructor u provided as const cannot be passed as reference unless the reference(formal) argument is also declared as const
     SalesRegister(SalesRegister &Source);
Thus the compiler tries to pass it as
     SalesRegister(SalesRegister Source);
but doesn't find any such declaration

You can solve this problem by having  the Copy Constructor like the following
     SalesRegister(const SalesRegister &Source);

Do this and compiling would give u errors
u will have to make your Get.... functions const

HTH
Amit
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10664488
Below is my version of the modified code

          SalesRegister  operator+(SalesRegister &Parameter);
          string GetDescription() const ;
          int GetTotalCostPrice() const ;
          int GetTotalSellPrice() const ;
          int GetItemCount() const ;

Amit
0
 

Author Comment

by:Broken_Arrow
ID: 10664702
Thanks That got rid of my errors
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10664713
U r welcome

Amit
0
 

Author Comment

by:Broken_Arrow
ID: 10664736
Now I have another Problem my program does not output the required results. It is suppsose to output something like this

Monday March 1, 2004
      $90      W. Savitch, Absolute C++ 1/e, Addison/Wesley, 2002
      $90      W. Savitch, Absolute C++ 1/e, Addison/Wesley, 2002

Tuesday March 2, 2004
      $120      A. Silberschatz, P. B. Galvin, and G. Gagne, Operating System Concepts 6/e, John Wiley & ons, 2003
      $80      L. A. Robertson, Simple Program Design 4/e, Thomson, 2003
      $90      W. Savitch, Absolute C++ 1/e, Addison/Wesley, 2002

Wednesday March 2, 2004
      $90      W. Savitch, Absolute C++ 1/e, Addison/Wesley, 2002
      $120      A. Silberschatz, P. B. Galvin, and G. Gagne, Operating System Concepts 6/e, John Wiley & Sons, 2003

Thursday March 2, 2004
      $120      A. Silberschatz, P. B. Galvin, and G. Gagne, Operating System Concepts 6/e, John Wiley & Sons, 2003
      $80      L. A. Robertson, Simple Program Design 4/e, Thomson, 2003

Friday March 2, 2004
      $90      W. Savitch, Absolute C++ 1/e, Addison/Wesley, 2002
      $80      L. A. Robertson, Simple Program Design 4/e, Thomson, 2003

Report for Monday March 1, 2004
      Total Sales:      $180
      Total Cost:      $150
      Average Profit:      $15.00

Report for Tuesday March 2, 2004
      Total Sales:      $290
      Total Cost:      $230
      Average Profit:      $20.00

Report for Wednesday March 2, 2004
      Total Sales:      $210
      Total Cost:      $160
      Average Profit:      $25.00

Report for Thursday March 2, 2004
      Total Sales:      $200
      Total Cost:      $155
      Average Profit:      $22.50

Report for Friday March 2, 2004
      Total Sales:      $170
      Total Cost:      $145
      Average Profit:      $12.50

Report for Total Sales
      Total Sales:      $1050
      Total Cost:      $840
      Average Profit:      $19.09

And my output is as follows

Campbell Star@campbell ~
$ ./ass1

$2011670320, , , , 1627947726
$2011670320, , , , 1627947726


$2147348480, , , , 6877184
$2011676203, , , , 16
$2011670320, , , , 1627947726


$2011670320, , , , 1627947726
$2147348480, , , , 6877184


$2147348480, , , , 6877184
$2011676203, , , , 16


$2011670320, , , , 1627947726
$2011676203, , , , 16
Report for
         Total Sales:    $-269333280
         Total Cost:     $-269333280
         Average Profit: $0.00
Report for
         Total Sales:    $1875729639
         Total Cost:     $1875729639
         Average Profit: $-236006064.00
Report for
         Total Sales:    $1875721602
         Total Cost:     $1875721602
         Average Profit: $0.00
Report for
         Total Sales:    $-133649157
         Total Cost:     $-133649157
         Average Profit: $0.00
Report for
         Total Sales:    $-271620727
         Total Cost:     $-271620727
         Average Profit: $0.00
Report for Total Sales
         Total Sales:    $-1218119219
         Total Cost:     $-1218119219
         Average Profit: $-1.00

I don't know why this is happening, I havn't used C++ before so any help would be appreciated.
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 10664775
The problem lies in your constructor

SalesRegister::SalesRegister(string Description, int TotalCostPrice, int TotalSellPrice, int ItemCount)
{
     Description = Description;
     TotalCostPrice = TotalCostPrice;
     TotalSellPrice = TotalSellPrice;
     ItemCount = ItemCount;
}

The above constructor has arguments with the same variable name as the class data members
Thus
     Description = Description;
would effectively do nothing to your class data member,
Thus the assignment happens to local variables
Thus the names of parameters should not same as the class data members

Example

SalesRegister::SalesRegister(string Description1, int TotalCostPrice1, int TotalSellPrice1, int ItemCount1)
{
     Description = Description1;
     TotalCostPrice = TotalCostPrice1;
     TotalSellPrice = TotalSellPrice1;
     ItemCount = ItemCount1;
}



Amit
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question