Hey guys, I keep getting a link error in my code. It says this:

1>prob4.main.obj : error LNK2005: "private: static int HotDogStand::totalSales" (?totalSales@HotDogStand@@0HA) already defined in prob4.func.obj

Here's my code, maybe someone can help me out.

#pragma once

#include <iostream>
using namespace std ;

class HotDogStand
{
public:
    HotDogStand ( int , int ) ;
    HotDogStand ( ) ;

    void justSold ( ) ;
    int idSales ( ) ;
    static int totSales ( ) ;
private:
    static int totalSales ;
    int id ;
    int sales ;
} ;

int HotDogStand:: totalSales = 0 ;


-----------------------------------------------------------------------------------

#include "prob4.h"

int main ( )
{
    HotDogStand jumbo ( 1 , 10 ) ;
    HotDogStand franks ( 2 , 20 ) ;
    HotDogStand porkies ( 3 , 5 ) ;
    HotDogStand bunzRus ;

   int totalSales ;

    int jumboSales ;
    int franksSales ;
    int porkiesSales ;
    int bunzRusSales ;

    int x ;

    for ( x = 0 ; x < 5 ; x++ )
    {
        jumbo.justSold ( ) ;
    }

    for ( x = 0 ; x < 10 ; x++ )
    {
        franks.justSold ( ) ;
    }

    for ( x = 0 ; x < 20 ; x++ )
    {
        porkies.justSold ( ) ;
    }

    for ( x = 0 ; x < 15 ; x++ )
    {
        bunzRus.justSold ( ) ;
    }

    jumboSales = jumbo.idSales ( ) ;
    franksSales = franks.idSales ( ) ;
    porkiesSales = porkies.idSales ( ) ;
    bunzRusSales = bunzRus.idSales ( ) ;

    totalSales = HotDogStand::totSales ( ) ;

    cout << jumboSales << endl << franksSales << endl << porkiesSales << endl << bunzRusSales << endl ;
   /cout << endl << totalSales << endl ;

    return 0 ;
}


--------------------------------------------------------------------------------------


#include "prob4.h"

HotDogStand::HotDogStand ( int x , int y )
{
    id = x ;
    sales = y ;
}

HotDogStand::HotDogStand ( )
{
    id = 0 ;
    sales = 0 ;
}

void HotDogStand::justSold ( )
{
    sales++ ;
    totalSales++ ;
}

int HotDogStand::idSales ( )
{
    return sales ;
}

int HotDogStand::totSales ( )
{
    return totalSales ;
}

Recommended Answers

All 3 Replies

> int HotDogStand:: totalSales = 0 ;
This should be in ONE .cpp file only.

commented: thanks! +4

>>int HotDogStand:: totalSales = 0 ;

this line goes into a *.cpp file, not the *.h file because it actually declares an object.

Oh ok! The book I'm going by doesn't use seperate files for showing sample code... so they had something like

header stuff

class
{
...
};

static initializations

int main ( )
{
...
}

etc. etc.

So I assumed the init went in the header file.
I know, more information than you needed; thanks for the help!

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.