Jump to content
  • Advertisement
Sign in to follow this  
supercoder74

static member troubles

This topic is 4831 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 am having some trouble with using static members. here is what my class bassicly is:
class instruction
{
static map<string,int> variables;
};


If I dont declare the static map it provides the usual error mesage:
  [Linker error] undefined reference to `instruction::variables'


but I did declare it, like this:
map<string,int> instruction::variables;
//and I get redeclaration errors!
74 C:\Dev-Cpp\script.cpp redeclaration of `std::map<std::string, int, std::less<std::string>, std::allocator<std::pair<const std::string, int> > > instruction::variables' 

52 C:\Dev-Cpp\script.cpp `std::map<std::string, int, std::less<std::string>, std::allocator<std::pair<const std::string, int> > > instruction::variables' previously declared here 

74 C:\Dev-Cpp\script.cpp [Warning] declaration of `std::map<std::string, int, std::less<std::string>, std::allocator<std::pair<const std::string, int> > > instruction::variables' outside of class is not definition 

how can I fix this?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
put in cpp file this:

instruction::map variables();

if i remember correctly

Share this post


Link to post
Share on other sites
now it gives me this error(in addition to the others):

74 C:\Dev-Cpp\script.cpp [Warning] `static' may not be used when defining (as opposed to declaring) a static data member
//EDIT: I defined it like this:
static map<string,int> instruction::variables;

Share this post


Link to post
Share on other sites
Ugh, brain fart. You don't need the static identifier outside the class. (You'd think I'd know that after all these years...)

Anyway, your initial solution should work -- and does, for me, when I compile it. There must be something about your implementation that's not showing which is causing this problem...

Share this post


Link to post
Share on other sites
well here is the full source to my program:

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
using namespace std;
enum keyword
{
key_if,
key_var,
key_print,
key_printvar,
key_endl,
key_exit,
key_getchar,
key_input
};

class instruction
{
public:
instruction(keyword k,const vector<string> &perams):key(k),perameters(perams){}
instruction(keyword k):key(k),perameters(0){}
void process()
{
switch(key)
{
case key_print:
cout<<perameters[0];
break;

case key_endl:
cout<<endl;
break;

case key_exit:

exit(atoi(perameters[0].c_str()));
break;

case key_getchar:
cin.get();
break;

case key_var:
variables[perameters[0]]=atoi(perameters[1].c_str());
break;
}
}
static map<string,int> variables;
private:
vector<string> perameters;
keyword key;
};
class script
{
public:
script(const vector<instruction>& instructions_):instructions(instructions_){}
void execute()
{
for(int i=0;i<instructions.size();i++)
{
instructions.process();
}
}
private:
vector<instruction> instructions;
};
int main()
{
vector<instruction> i;
map<string,int> instruction::variables;
i.push_back(instruction(key_print,vector<string>(1,"hi")));
i.push_back(instruction(key_getchar));
vector<string> perams;
perams.push_back("i");
perams.push_back("5");
i.push_back(instruction(key_var,perams));
script s(i);
s.execute();

}


Share this post


Link to post
Share on other sites
Ahhh, I see. You need to declare "variables" globally (outside of any function) rather than within main().

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!