How to access a static member of a 'structure' in another source file

I am creating a small billing program. I am trying to get a static static double sum declared in a header file in a different source file. Java is my first language, so I am having trouble sorting it in C ++.

When I try to get the following error.

bill.cpp (16): error C2655: 'BillItem :: total': defining or reusing is illegal in the current scope

bill.h (8): note: see declaration "BillItem :: total"

bill.cpp (16): error C2086: 'double BillItem :: total': redefinition

bill.h (8): note: see declaration "total"

How can I make this available. Google error didn't help.

What I want to implement is create a static double variable in the structure that will be common to all instances of the structure. I need to access this static variable in another source file where I will be doing the calculations.

Bill.h

#pragma once

struct BillItem
{
public:
    static double total;
    int quantity;
    double subTotal;
};

      

Bill.cpp

#include<iostream>
#include "Item.h"
#include "Bill.h" 

void createBill() {
    double BillItem::total = 10;
    cout << BillItem::total << endl;
}

      

MainCode.cpp

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

int main() {
    createBill();
    return 0;
}

      

+3


source to share


1 answer


You did not enter your total amount. Well, you have, but inside a function. It must be out of scope:



#include<iostream>
#include "Item.h"
#include "Bill.h" 

double BillItem::total = 0;

void createBill() {
    BillItem::total = 10;
    cout << BillItem::total << endl;
}

      

+4


source







All Articles