Click to See Complete Forum and Search --> : Static Enumerated Variables
seanc91
January 25th, 2008, 07:35 PM
I have a class:
class aCard
{
public:
SUIT suitType;
CARD cardType;
static SUIT suitCount;
static CARD cardCount;
void print();
aCard();
};
Where CARD and SUIT are enumerated types.
However, when I compile, I get these compile errors on MS Visual Studio Exp. 2008.
1>DeckOfCards.obj : error LNK2001: unresolved external symbol "public: static enum CARD aCard::cardCount" (?cardCount@aCard@@2W4CARD@@A)
1>DeckOfCards.obj : error LNK2001: unresolved external symbol "public: static enum SUIT aCard::suitCount" (?suitCount@aCard@@2W4SUIT@@A)
1>C:\Documents and Settings\Sean\My Documents\Visual Studio 2008\Projects\DeckOfCards\Debug\DeckOfCards.exe : fatal error LNK1120: 2 unresolved externals
I have no clue what is going on here, but I assume there is something to do with using an enumerated type as a type of static variable, can anyone help me on this?
Plasmator
January 25th, 2008, 07:40 PM
This has nothing to do with enums, actually. You must define your static members outside of your class (in your c/cpp file). For example:
Whatever.h
struct Whatever
{
static int i;
};
Whatever.cpp
#include "Whatever.h"
int Whatever::i;
seanc91
January 25th, 2008, 07:46 PM
This has nothing to do with enums, actually. You must define your static members outside of your class (in your c/cpp file). For example:
Whatever.h
struct Whatever
{
static int i;
};
Whatever.cpp
#include "Whatever.h"
int Whatever::i;
But I can have normal static variables inside the class, as it is now. I was under the impression that that was legal, and the point of static variables (so that in this case, all "aCards" had acess to suitCount and cardCount).
Eg.
class aCard
{
public:
SUIT suitType;
CARD cardType;
static int test;
SUIT suitCount;
CARD cardCount;
void print();
aCard();
};
Perhaps I'm misinterpreting what you're trying to say?
Plasmator
January 25th, 2008, 07:50 PM
Perhaps I'm misinterpreting what you're trying to say? Look at the example that I posted. You still keep your data in the class; the only thing that's changed is that you have to go out of your way and define it in a compilation unit.
seanc91
January 25th, 2008, 08:11 PM
I'm not sure this is right, its giving me a tonne of compile errors:
class.h
#include "stdafx.h"
#include "string"
#include <iomanip>
#include <iostream>
enum SUIT{DIAMONDS,SPADES,HEARTS,CLUBS};
enum CARD{ACE,KING,QUEEN,JACK,TEN,NINE,EIGHT,SEVEN,SIX,FIVE,FOUR,THREE,TWO};
struct test
{
static SUIT suitCount;
static CARD cardCount;
}
And then in my .cpp
class aCard
{
public:
SUIT suitType;
CARD cardType;
SUIT class::suitCount;
CARD class::cardCount;
void print();
aCard();
};
I'm guessing this isnt right. What am I doing wrong?
Plasmator
January 25th, 2008, 08:26 PM
I'm not sure this is right, its giving me a tonne of compile errors:
class.h
#include "stdafx.h"
#include "string"
#include <iomanip>
#include <iostream>
enum SUIT{DIAMONDS,SPADES,HEARTS,CLUBS};
enum CARD{ACE,KING,QUEEN,JACK,TEN,NINE,EIGHT,SEVEN,SIX,FIVE,FOUR,THREE,TWO};
struct test
{
static SUIT suitCount;
static CARD cardCount;
}
And then in my .cpp
class aCard
{
public:
SUIT suitType;
CARD cardType;
SUIT class::suitCount;
CARD class::cardCount;
void print();
aCard();
};
I'm guessing this isnt right. What am I doing wrong?
This is what your header/source file should look like (more or less):
[NOTE: modify where necessary]
Header:
#ifndef <...> //include guard
#define <...>
#include <your headers>
//If I were you I'd put these in a separate namespace...
enum SUIT
{
<defs>
};
enum CARD
{
<defs>
};
class aCard
{
public:
SUIT suitType;
CARD cardType;
static SUIT suitCount; //You sure these should be enums?
static CARD cardCount; //Same with this...
void print();
aCard();
};
#endif
Source file:
#include "your header"
SUIT aCard::suitCount; //default constructed
CARD aCard::cardCount;
//<aCard function (+ctor, etc.) implementations go here...>
Paul McKenzie
January 25th, 2008, 08:39 PM
I'm not sure this is right, its giving me a tonne of compile errors:
// cardsuit.h
#ifndef CARDSUIT_H
#define CARDSUIT_H
enum SUIT{DIAMONDS,SPADES,HEARTS,CLUBS};
enum CARD{ACE,KING,QUEEN,JACK,TEN,NINE,EIGHT,SEVEN,SIX,FIVE,FOUR,THREE,TWO};
struct test
{
static SUIT suitCount;
static CARD cardCount;
};
#endif
#include "cardsuit.h"
class aCard
{
public:
SUIT suitType;
CARD cardType;
void print();
aCard();
};
SUIT test::suitCount = 0;
CARD test::cardCount = 0;
I'm guessing this isnt right. What am I doing wrong?
http://www.parashift.com/c++-faq-lite/index.html
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.10
Regards,
Paul McKenzie
seanc91
January 26th, 2008, 05:04 PM
Okay thanks guys, I got it. Thanks for the help.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.