2

I want to create a class that collects information from many other classes and the end of a simulation. For that, it has to be independent from all other classes and completely accessible. For that I chose a static approach, a static struct indeed.

This is my Foo.h

class Foo
{
 public:
   static int app_counter;

   typedef struct
   {
       double eed;
       int bits;  
   }APPLayer;

   static APPLayer applayer_metric;
public:
   Foo(){};
   ~Foo();
};

This is the Foo.cpp

#include "Foo.h"
int Foo::app_counter=0;
//How do I set all internal members to zero?
Foo::APPLayer applayer_metric;
Foo::~Foo()
{
  std::cout << app_counter << std::endl; 
  //Which is the way to access to the values of my members?
  std::cout << applayer_metric.bits << std::endl;
}

This is the error I get in my Foo.CPP

Foo.cc:38: undefined reference to `Foo::applayer_metric'

If for instance I change the line in Foo.cpp to this one

std::cout << Foo::applayer_metric.bits << std::endl;

I get exactly the same error.

My questions are:

  1. How can I initialize all members of a static struct variable to zero?
  2. How do I access to these members in other functions in Foo.cpp?

FYI, there is no complain if I initialize and access the variable "app_counter"

What am I doing wrong?

Thanks in advance.

Humberto
  • 65
  • 1
  • 8
  • possible duplicate of [c++ undefined reference to to static member](http://stackoverflow.com/questions/9110487/c-undefined-reference-to-to-static-member) – PlasmaHH Feb 22 '13 at 10:24
  • 1
    You have a trailing `e` in `int Foo::app_counter=0;e` – ta.speot.is Feb 22 '13 at 10:24
  • possible duplicate of [What does it mean to have an undefined reference to a static member?](http://stackoverflow.com/questions/7092765/what-does-it-mean-to-have-an-undefined-reference-to-a-static-member) – Mankarse Feb 22 '13 at 10:27
  • @PlasmaHH it is not a duplicate because I define and declare the struct. I want to initialize them. The typo 'e' is now corrected – Humberto Feb 22 '13 at 10:28
  • +1 to counter unexplained downvote – Cheers and hth. - Alf Feb 22 '13 at 10:29

1 Answers1

4

Your definition of Foo::applayer_metric is missing a scope resolution operator. Change it to this:

Foo::APPLayer Foo::applayer_metric;
Mankarse
  • 39,818
  • 11
  • 97
  • 141
  • sorry for the last comment. It was a typo. I think this was the last option to test! Now I can initialize the struct at the constructor, thanks a lot. I am reading your post, if I could have found it, I would have not posted this question here. :) – Humberto Feb 22 '13 at 11:04