Solved

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

Posted on 2004-03-23
6
243 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
  • 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now