Hello,
Working with structures and C++. Our teacher in class attempted to show us how to over load an "<<" operator. I've tried to do that here in the program below without much luck. I have commented out some parts of the code while I am trying to debug this program. I am trying to get to get cout to print each part of each structure named. And I am attempting to do that with the overloaded cout statement. When I compile this I get an error of:

lab6 ch7 pc 10.cpp(68): error C2275: 'CorpData' : illegal use of this type as an expression

cpp(16) : see declaration of 'CorpData'

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;




struct CorpData
{
    string Name; 
    double firstQ, secondQ, thirdQ, fourthQ, average; 
    CorpData (string N, double q1,double q2, double q3, double q4)
    {
        Name=N;
        firstQ=q1;
        secondQ=q2;
        thirdQ=q3;
        fourthQ=q4;
    };
};  



void PrintCorpData (const CorpData &Corp)
{
    const int x=1000000;
    cout.imbue(std::locale(""));
    /*cout<<Corp.Name<<" Division"<<endl;
    cout<<"Quarter One Sales:$ "<<Corp.firstQ <<endl;
    cout<<"Quarter Two Sales:$ "<<Corp.secondQ <<endl;
    cout<<"Thrid Quarter Sales:$ "<<Corp.thirdQ <<endl;
    cout<<"Forth Quarter Sales:$ "<<Corp.fourthQ << endl;*/
    double total= Corp.firstQ + Corp.secondQ + Corp.thirdQ + Corp.fourthQ;
    cout<< "Total Sales:$ "<< fixed <<setprecision(2)<<total <<endl;
    cout<<"Avg sales:$ " << total/4 << endl;
    cout <<endl;

}

ostream &operator<<(ostream &lhs, CorpData rhs)
{
    lhs << rhs.Name << " "
        << rhs.firstQ << " "
        << rhs.secondQ << " "
        << rhs.thirdQ << " "
        << rhs.fourthQ << " ";

    return lhs;
}
int main()

{


    CorpData North ("North", 100000,1100200,102000,10201230);
    CorpData South ("South", 14562200,145521120,98751120,19778525430);
    CorpData East ("East", 987456,4223651120,55222120,11112230);
    CorpData West ("West", 95636660,1458455120,11545456420,1545454545430);


    cout << CorpData;


    //PrintCorpData(North);
    //PrintCorpData(South);
    //PrintCorpData(East);
    //PrintCorpData(West);


    int key;

    cout << "Press any key to continue"<< endl;
    cin >> key;

    return 0;
}

At line 63 you have cout << CorpData;
You need to use an instance of CorpData, for example cout << West;

You'll also need to set the precision when you output the data seeing as it contains double precision values. If you add limits to your includes, then the function could be written as:

ostream &operator<<(ostream &lhs, const CorpData &rhs)
{
    lhs << std::setprecision(std::numeric_limits<double>::digits10 + 1)
        << rhs.Name << " "
        << rhs.firstQ << " "
        << rhs.secondQ << " "
        << rhs.thirdQ << " "
        << rhs.fourthQ << " ";

    return lhs;
}
commented: good :) +14

Thank you for the reply, that clears it up. That then leads to another question that I did not understand quite what I needed to print in the statement. In the area of the print function where calls out the individual quarters of each structure, that is what is what I was trying to get to print with the overloaded cout. So now I have two redesigned the overload, and reconfigure the print statement.

I guess I have found the limit of a overload.

'CorpData::CorpData' : no overloaded function takes 5 arguments

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;

struct CorpData
{
    string Name; 
    double firstQ, secondQ, thirdQ, fourthQ, total; 
    CorpData (string N, double q1,double q2, double q3, double q4,double total)
    {
        Name=N;
        firstQ=q1;
        secondQ=q2;
        thirdQ=q3;
        fourthQ=q4;
        total= q1 + q2 + q3 + q4;
    };
};  



ostream &operator<<(ostream &lhs, CorpData rhs)
{

     cout.imbue(std::locale(""));
    lhs << std::setprecision(std::numeric_limits<double>::digits10 + 1)
        << rhs.Name << " "<<endl
        << "$"<< rhs.firstQ << " "<<endl
        << "$"<< rhs.secondQ << " "<<endl
        << "$"<< rhs.thirdQ << " "<<endl
        << "$"<< rhs.fourthQ << " "<<endl
        << "Total Sales:$ " << rhs.total <<endl;
        //<<"Avg sales:$ " << rhs.total/4 << endl;

    return lhs;
}
int main()
{


    CorpData North ("North", 100000,1100200,102000,10201230);
    CorpData South ("South", 14562200,145521120,98751120,19778525430);
    CorpData East ("East", 987456,4223651120,55222120,11112230);
    CorpData West ("West", 95636660,1458455120,11545456420,1545454545430);


    cout << North;
    cout << South,
    cout << East;
    cout << West;

    /*PrintCorpData(North);
    PrintCorpData(South);
    PrintCorpData(East);
    PrintCorpData(West);
    */


    int key;

    cout << "Press any key to continue"<< endl;
    cin >> key;

    return 0;
}

I guess I have found the limit of a overload.

That's not the problem. Lines 43-46 pass 5 arguments, line 11 wants 6. Remove the last argument on line 5 and make total local to that function. But what is that function doing with total? It's a useless variable that disappears as soon as the function returns.

My idea for the overloaded see out statement and the reason total is in that structure is so that with one see out statement I get all variables to print. If you look back there was a function that called out a print statement with the same type of output. My thoughts was to put total an average into the structure and with it each iteration of the structure it within output the variables along with total an average. I see your point about the uneven amount of variables and arguments between line 11 in lines 43 and 46. It may very well that I need a separate print function for total an average.

The variable total on line 11 hides the variable total declared on line 10. Just delete parameter total on line 11.

Ancient Dragon – thank you for that bit of insight, that solve the problem and now my program functions the way I desired. Would you care to elaborate on "hides the variable". As I'm new to programming in C++ I would like to understand how these things interact. Is it because of each iteration the variable gets re-created thus is treated as separate from the main declaration?

"hides the variable" means that if you have two variables with the same name, one global and another local, then the compiler will always use the local variable and ignore the global variable. In your case there is one variable named "total" that is a member of the class, and another variable with the same name that is declared in the function (or as a function parameter). This is one very good reason to never, ever have two different variables with the same name -- it just causes lots of bugs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.