Reputation: 137
I'm creating a small program to do billing. I'm trying to access a static member static double total declared in a header file, in another source file. Java is my first language so having trouble in sorting it out in C++.
When I try i get the following error.
bill.cpp(16): error C2655: 'BillItem::total': definition or redeclaration illegal in current scope
bill.h(8): note: see declaration of 'BillItem::total'
bill.cpp(16): error C2086: 'double BillItem::total': redefinition
bill.h(8): note: see declaration of 'total'
How can I make it available. Googling the error didn't help.
What i want is to implement is to create a static double variable in a struct which will be common to all struct instances. I need to access this static variable in another source file where I will be doing the calculation.
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;
}
Upvotes: 5
Views: 1162
Reputation: 77304
You have not declared your total. Well, you have, but inside a function. It needs to be outside the function scope:
#include<iostream>
#include "Item.h"
#include "Bill.h"
double BillItem::total = 0;
void createBill() {
BillItem::total = 10;
cout << BillItem::total << endl;
}
Upvotes: 5