c++ - How to access static member of a 'struct' in another source file -
i'm creating small program billing. i'm trying access static member static double total declared in header file, in source file. java first language having trouble in sorting out in c++.
when try 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 make available. googling error didn't help.
what want implement create static double variable in struct common struct instances. need access static variable in source file doing 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; }
you have not declared total. well, have, inside function. needs outside function scope:
#include<iostream> #include "item.h" #include "bill.h" double billitem::total = 0; void createbill() { billitem::total = 10; cout << billitem::total << endl; }
Comments
Post a Comment