5

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;
}
Community
  • 1
  • 1
Anj Jo
  • 137
  • 2
  • 8
  • Is this all your code? You don't have `double BillItem::total;` outside the struct in the header file do you? – NathanOliver Jul 28 '17 at 13:35
  • Remove `double` from the the line `double BillItem::total = 10;` in Bill.cpp. Type names are only used in variable declarations, not every time they are referenced. – patatahooligan Jul 28 '17 at 13:37
  • No. Should I add it outside struct in the same header file? Java is my first language. I'm new to C++. Could you suggest how to implement? – Anj Jo Jul 28 '17 at 13:38
  • @patatahooligan When I remove double i get the following error. error LNK2001: unresolved external symbol "public: static double BillItem::total" (?total@BillItem@@2NA) – Anj Jo Jul 28 '17 at 13:39
  • Possible duplicate of [C++ static constant string (class member)](https://stackoverflow.com/questions/1563897/c-static-constant-string-class-member) – underscore_d Jul 28 '17 at 13:40
  • I saw in an example in StackOverflow and the have used variable type again. So I repeated in my code. – Anj Jo Jul 28 '17 at 13:40
  • @underscore_d The issue is not same as the question you mentioned. That is about assigning value inside the class itself. – Anj Jo Jul 28 '17 at 13:45
  • @AnjJo If I understand your problem, that answer mentions the solution to it: _"You have to define your static member outside the class definition and provide the initializer there."_ The definition needs to be in a source file, not a header. – underscore_d Jul 28 '17 at 13:46
  • I did define it outside the struct but inside the function which is not inside a class. Solution to my problem seem to be I have to declare the static member outside a function scope. – Anj Jo Jul 28 '17 at 13:49

1 Answers1

5

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;
}
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • This worked. I thought I can double BillItem::total = 0; inside a function. So i should write it outside a function like a global variable. Is that right? – Anj Jo Jul 28 '17 at 13:44
  • 1
    @AnjJo Exactly. Because it *is* a global variable. There is only one and it's global. – nvoigt Jul 28 '17 at 13:55
  • Why do we need to declare it outside the function scope? – skpro19 Jul 06 '23 at 08:17
  • @skpro19 Because it is defined that way. Why? Probably because it is a gobal variable, so declaring it inside a function makes no sense. What if you never call it? Or call another one first? – nvoigt Jul 06 '23 at 08:50