Jump to content
  • Advertisement
Sign in to follow this  
Alpha_ProgDes

undefined reference.... static functions/variables

This topic is 4840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have two classes, but I'll show one because they're both similar:
#ifndef LOCALCALL
#define LOCALCALL

#include "PhoneCall.h"

class LocalCall : public PhoneCall
  {
public:
  LocalCall(Interval<Time> interval);
  Money computeCost() const;
  int rank() const;
  void displayType(ostream& out) const;
  static void initialize(Money costPerMinute, int freeMinutesLeft);
  static Money computeTotal();
private:
  static Money costPerMinute;  
  static int freeMinutesLeft;
  static Money total;
  };

#endif
more...
#include "LocalCall.h"
using namespace std;


LocalCall::LocalCall(Interval<Time> interval) 
    :PhoneCall(interval) {
        
}

Money LocalCall::computeCost() const {
    
    enum LimitTrigger {NO, YES};
    static LimitTrigger OverLimit = NO; 
    
    int minutesUsed = callLength();
    freeMinutesLeft -= minutesUsed;
    Money costOfLocal;
    if (freeMinutesLeft <= 0 && OverLimit == NO) {
        
       Money overage = (costPerMinute * -freeMinutesLeft);
       costOfLocal = overage;
       OverLimit = YES;
    }
    else if (freeMinutesLeft <= 0 && OverLimit == YES) {
        
       Money overage = (costPerMinute * minutesUsed);
       costOfLocal = overage;
    }
    else {
        Money overage (0,0);
        costOfLocal = overage;
    }
    
    total += costOfLocal;
    return costOfLocal;
}

int LocalCall::rank () const {
    
    int LOCAL = 1;
    
    return LOCAL;
}

void LocalCall::displayType (ostream& out) const {
 
    out.precision(4);
    
    //out << "The final bill is: " ;
    //out << longDistanceTotal << " for long distance." << endl;
    //out << longDistanceCalls << endl; 
    out << " and " << computeTotal() << " for local calls." << endl;
    out << computeCost() << endl;
    //out << "Your total is: " << showpoint << longDistanceTotal + localTotal << "." << endl; 
}

void LocalCall::initialize(Money costPerMinute, int freeMinutesLeft) {
    
    LocalCall::costPerMinute = costPerMinute;
    LocalCall::freeMinutesLeft = freeMinutesLeft;   
}

Money LocalCall::computeTotal() {
 
    return total;
   
}
i keep getting: [Linker error] undefined reference to `LocalCall::freeMinutesLeft' [Linker error] undefined reference to `LocalCall::freeMinutesLeft' [Linker error] undefined reference to `LocalCall::costPerMinute' [Linker error] undefined reference to `LocalCall::total' actually I get several of these. can someone give a BIG clue on why it's doing that. i'm using Dev-C++ 4.9.9.2, if that's important.

Share this post


Link to post
Share on other sites
Advertisement
static class variables, in addition to being declared in the header, need to be defined in a source file. Place

int LocalCall::freeMinutesLeft;


at namespace scope (not inside a class or funciton) in one source file. Repeat for the other static class variables.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
static class variables, in addition to being declared in the header, need to be defined in a source file. Place

int LocalCall::freeMinutesLeft;


at namespace scope (not inside a class or funciton) in one source file. Repeat for the other static class variables.


We really need to put a system in where I can rate more than once for very helpful information.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!